diff --git a/api/handlers/documents.go b/api/handlers/documents.go index 09fab03..4e0f1ac 100644 --- a/api/handlers/documents.go +++ b/api/handlers/documents.go @@ -60,6 +60,38 @@ func DeleteDocument(c *gin.Context) { c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) } +// TODO: Maybe move "replace" logic to repository +func ReplaceDocument(c *gin.Context) { + databaseId := c.Param("databaseId") + collectionId := c.Param("collId") + documentId := c.Param("docId") + + var requestBody map[string]interface{} + if err := c.BindJSON(&requestBody); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) + return + } + + status := repositories.DeleteDocument(databaseId, collectionId, documentId) + if status == repositorymodels.StatusNotFound { + c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) + return + } + + status = repositories.CreateDocument(databaseId, collectionId, requestBody) + if status == repositorymodels.Conflict { + c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) + return + } + + if status == repositorymodels.StatusOk { + c.IndentedJSON(http.StatusCreated, requestBody) + return + } + + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) +} + func DocumentsPost(c *gin.Context) { databaseId := c.Param("databaseId") collectionId := c.Param("collId") diff --git a/api/router.go b/api/router.go index 9dea0a6..a7e73be 100644 --- a/api/router.go +++ b/api/router.go @@ -16,6 +16,7 @@ func CreateRouter() *gin.Engine { router.POST("/dbs/:databaseId/colls/:collId/docs", handlers.DocumentsPost) 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.DELETE("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.DeleteDocument) router.POST("/dbs/:databaseId/colls", handlers.CreateCollection)