mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-19 08:50:46 +00:00
Implement document PATCH operation
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
jsonpatch "github.com/evanphx/json-patch/v5"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/constants"
|
||||
"github.com/pikami/cosmium/internal/logger"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
@@ -100,6 +103,82 @@ func ReplaceDocument(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func PatchDocument(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
documentId := c.Param("docId")
|
||||
|
||||
document, status := repositories.GetDocument(databaseId, collectionId, documentId)
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
var requestBody map[string]interface{}
|
||||
if err := c.BindJSON(&requestBody); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
operations := requestBody["operations"]
|
||||
operationsBytes, err := json.Marshal(operations)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": "Could not decode operations"})
|
||||
return
|
||||
}
|
||||
|
||||
patch, err := jsonpatch.DecodePatch(operationsBytes)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
currentDocumentBytes, err := json.Marshal(document)
|
||||
if err != nil {
|
||||
logger.Error("Failed to marshal existing document:", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to marshal existing document"})
|
||||
return
|
||||
}
|
||||
|
||||
modifiedDocumentBytes, err := patch.Apply(currentDocumentBytes)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var modifiedDocument map[string]interface{}
|
||||
err = json.Unmarshal(modifiedDocumentBytes, &modifiedDocument)
|
||||
if err != nil {
|
||||
logger.Error("Failed to unmarshal modified document:", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"message": "Failed to unmarshal modified document"})
|
||||
return
|
||||
}
|
||||
|
||||
if modifiedDocument["id"] != document["id"] {
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"message": "The ID field cannot be modified"})
|
||||
return
|
||||
}
|
||||
|
||||
status = repositories.DeleteDocument(databaseId, collectionId, documentId)
|
||||
if status == repositorymodels.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, modifiedDocument)
|
||||
if status == repositorymodels.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositorymodels.StatusOk {
|
||||
c.IndentedJSON(http.StatusCreated, createdDocument)
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
func DocumentsPost(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
@@ -19,35 +19,8 @@ func Authentication() gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
var resourceType string
|
||||
parts := strings.Split(requestUrl, "/")
|
||||
switch len(parts) {
|
||||
case 2, 3:
|
||||
resourceType = parts[1]
|
||||
case 4, 5:
|
||||
resourceType = parts[3]
|
||||
case 6, 7:
|
||||
resourceType = parts[5]
|
||||
}
|
||||
|
||||
databaseId, _ := c.Params.Get("databaseId")
|
||||
collId, _ := c.Params.Get("collId")
|
||||
docId, _ := c.Params.Get("docId")
|
||||
var resourceId string
|
||||
if databaseId != "" {
|
||||
resourceId += "dbs/" + databaseId
|
||||
}
|
||||
if collId != "" {
|
||||
resourceId += "/colls/" + collId
|
||||
}
|
||||
if docId != "" {
|
||||
resourceId += "/docs/" + docId
|
||||
}
|
||||
|
||||
isFeed := c.Request.Header.Get("A-Im") == "Incremental Feed"
|
||||
if resourceType == "pkranges" && isFeed {
|
||||
resourceId = collId
|
||||
}
|
||||
resourceType := urlToResourceType(requestUrl)
|
||||
resourceId := requestToResourceId(c)
|
||||
|
||||
authHeader := c.Request.Header.Get("authorization")
|
||||
date := c.Request.Header.Get("x-ms-date")
|
||||
@@ -67,3 +40,43 @@ func Authentication() gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func urlToResourceType(requestUrl string) string {
|
||||
var resourceType string
|
||||
parts := strings.Split(requestUrl, "/")
|
||||
switch len(parts) {
|
||||
case 2, 3:
|
||||
resourceType = parts[1]
|
||||
case 4, 5:
|
||||
resourceType = parts[3]
|
||||
case 6, 7:
|
||||
resourceType = parts[5]
|
||||
}
|
||||
|
||||
return resourceType
|
||||
}
|
||||
|
||||
func requestToResourceId(c *gin.Context) string {
|
||||
databaseId, _ := c.Params.Get("databaseId")
|
||||
collId, _ := c.Params.Get("collId")
|
||||
docId, _ := c.Params.Get("docId")
|
||||
resourceType := urlToResourceType(c.Request.URL.String())
|
||||
|
||||
var resourceId string
|
||||
if databaseId != "" {
|
||||
resourceId += "dbs/" + databaseId
|
||||
}
|
||||
if collId != "" {
|
||||
resourceId += "/colls/" + collId
|
||||
}
|
||||
if docId != "" {
|
||||
resourceId += "/docs/" + docId
|
||||
}
|
||||
|
||||
isFeed := c.Request.Header.Get("A-Im") == "Incremental Feed"
|
||||
if resourceType == "pkranges" && isFeed {
|
||||
resourceId = collId
|
||||
}
|
||||
|
||||
return resourceId
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ func CreateRouter() *gin.Engine {
|
||||
router.GET("/dbs/:databaseId/colls/:collId/docs", handlers.GetAllDocuments)
|
||||
router.GET("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.GetDocument)
|
||||
router.PUT("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.ReplaceDocument)
|
||||
router.PATCH("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.PatchDocument)
|
||||
router.DELETE("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.DeleteDocument)
|
||||
|
||||
router.POST("/dbs/:databaseId/colls", handlers.CreateCollection)
|
||||
|
||||
@@ -3,10 +3,14 @@ package tests_test
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
@@ -49,7 +53,7 @@ func testCosmosQuery(t *testing.T,
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Documents(t *testing.T) {
|
||||
func documents_InitializeDb(t *testing.T) (*httptest.Server, *azcosmos.ContainerClient) {
|
||||
repositories.CreateDatabase(repositorymodels.Database{ID: testDatabaseName})
|
||||
repositories.CreateCollection(testDatabaseName, repositorymodels.Collection{
|
||||
ID: testCollectionName,
|
||||
@@ -65,7 +69,6 @@ func Test_Documents(t *testing.T) {
|
||||
repositories.CreateDocument(testDatabaseName, testCollectionName, map[string]interface{}{"id": "67890", "pk": "456", "isCool": true})
|
||||
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
|
||||
client, err := azcosmos.NewClientFromConnectionString(
|
||||
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.Config.AccountKey),
|
||||
@@ -76,6 +79,13 @@ func Test_Documents(t *testing.T) {
|
||||
collectionClient, err := client.NewContainer(testDatabaseName, testCollectionName)
|
||||
assert.Nil(t, err)
|
||||
|
||||
return ts, collectionClient
|
||||
}
|
||||
|
||||
func Test_Documents(t *testing.T) {
|
||||
ts, collectionClient := documents_InitializeDb(t)
|
||||
defer ts.Close()
|
||||
|
||||
t.Run("Should query document", func(t *testing.T) {
|
||||
testCosmosQuery(t, collectionClient,
|
||||
"SELECT c.id, c[\"pk\"] FROM c ORDER BY c.id",
|
||||
@@ -137,3 +147,61 @@ func Test_Documents(t *testing.T) {
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Documents_Patch(t *testing.T) {
|
||||
ts, collectionClient := documents_InitializeDb(t)
|
||||
defer ts.Close()
|
||||
|
||||
t.Run("Should PATCH document", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
expectedData := map[string]interface{}{"id": "67890", "pk": "456", "newField": "newValue"}
|
||||
|
||||
patch := azcosmos.PatchOperations{}
|
||||
patch.AppendAdd("/newField", "newValue")
|
||||
patch.AppendRemove("/isCool")
|
||||
|
||||
itemResponse, err := collectionClient.PatchItem(
|
||||
context,
|
||||
azcosmos.PartitionKey{},
|
||||
"67890",
|
||||
patch,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
assert.Nil(t, err)
|
||||
|
||||
var itemResponseBody map[string]string
|
||||
json.Unmarshal(itemResponse.Value, &itemResponseBody)
|
||||
|
||||
assert.Equal(t, expectedData["id"], itemResponseBody["id"])
|
||||
assert.Equal(t, expectedData["pk"], itemResponseBody["pk"])
|
||||
assert.Empty(t, itemResponseBody["isCool"])
|
||||
assert.Equal(t, expectedData["newField"], itemResponseBody["newField"])
|
||||
})
|
||||
|
||||
t.Run("Should not allow to PATCH document ID", func(t *testing.T) {
|
||||
context := context.TODO()
|
||||
|
||||
patch := azcosmos.PatchOperations{}
|
||||
patch.AppendReplace("/id", "newValue")
|
||||
|
||||
_, err := collectionClient.PatchItem(
|
||||
context,
|
||||
azcosmos.PartitionKey{},
|
||||
"67890",
|
||||
patch,
|
||||
&azcosmos.ItemOptions{
|
||||
EnableContentResponseOnWrite: false,
|
||||
},
|
||||
)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
var respErr *azcore.ResponseError
|
||||
if errors.As(err, &respErr) {
|
||||
assert.Equal(t, http.StatusUnprocessableEntity, respErr.StatusCode)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user