mirror of
https://github.com/pikami/cosmium.git
synced 2026-11-01 05:55:36 +00:00
Add ETag optimistic concurrency for document replace
Co-authored-by: Pijus Kamandulis <[email protected]>
This commit is contained in:
1 parent
05e8cd2842
commit
97dc9fb465
4 files changed
+101
-1
No files matched your search
@@ -47,6 +47,9 @@ func (h *Handlers) GetDocument(c *gin.Context) {
|
||||
|
||||
document, status := h.dataStore.GetDocument(databaseId, collectionId, documentId)
|
||||
if status == datastore.StatusOk {
|
||||
if etag, ok := document["_etag"].(string); ok {
|
||||
c.Header(headers.ETag, etag)
|
||||
}
|
||||
c.IndentedJSON(http.StatusOK, document)
|
||||
return
|
||||
}
|
||||
@@ -90,7 +93,24 @@ func (h *Handlers) ReplaceDocument(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
status := h.dataStore.DeleteDocument(databaseId, collectionId, documentId)
|
||||
existingDocument, status := h.dataStore.GetDocument(databaseId, collectionId, documentId)
|
||||
if status == datastore.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
|
||||
return
|
||||
}
|
||||
if status != datastore.StatusOk {
|
||||
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
|
||||
return
|
||||
}
|
||||
|
||||
if ifMatch := c.GetHeader(headers.IfMatch); ifMatch != "" {
|
||||
if existingDocument["_etag"] != ifMatch {
|
||||
c.JSON(http.StatusPreconditionFailed, constants.PreconditionFailedResponse)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
status = h.dataStore.DeleteDocument(databaseId, collectionId, documentId)
|
||||
if status == datastore.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user