check isUpsert header in POST document request (#5)

* check isUpsert header in POST document request

* Verify response code on "CreateItem that already exists" test

---------

Co-authored-by: Pijus Kamandulis <pikami@users.noreply.github.com>
This commit is contained in:
erikzeneco
2024-11-01 20:11:59 +01:00
committed by GitHub
parent a6b5d32ff7
commit 2834f3f641
2 changed files with 94 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
jsonpatch "github.com/evanphx/json-patch/v5"
"github.com/gin-gonic/gin"
@@ -223,6 +224,11 @@ func DocumentsPost(c *gin.Context) {
return
}
isUpsert, _ := strconv.ParseBool(c.GetHeader("x-ms-documentdb-is-upsert"))
if isUpsert {
repositories.DeleteDocument(databaseId, collectionId, requestBody["id"].(string))
}
createdDocument, status := repositories.CreateDocument(databaseId, collectionId, requestBody)
if status == repositorymodels.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})