cosmium/api/handlers/documents.go

159 lines
4.5 KiB
Go
Raw Normal View History

2024-02-10 23:44:20 +00:00
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/repositories"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
2024-02-10 23:44:20 +00:00
)
func GetAllDocuments(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
documents, status := repositories.GetAllDocuments(databaseId, collectionId)
if status == repositorymodels.StatusOk {
2024-02-10 23:44:20 +00:00
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "Documents": documents, "_count": len(documents)})
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
}
func GetDocument(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.StatusOk {
2024-02-10 23:44:20 +00:00
c.IndentedJSON(http.StatusOK, document)
return
}
if status == repositorymodels.StatusNotFound {
2024-02-10 23:44:20 +00:00
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
}
func DeleteDocument(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
documentId := c.Param("docId")
status := repositories.DeleteDocument(databaseId, collectionId, documentId)
if status == repositorymodels.StatusOk {
2024-02-10 23:44:20 +00:00
c.Status(http.StatusNoContent)
return
}
if status == repositorymodels.StatusNotFound {
2024-02-10 23:44:20 +00:00
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
}
2024-02-14 18:36:57 +00:00
// 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"})
}
2024-02-10 23:44:20 +00:00
func DocumentsPost(c *gin.Context) {
databaseId := c.Param("databaseId")
collectionId := c.Param("collId")
var requestBody map[string]interface{}
if err := c.BindJSON(&requestBody); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
query := requestBody["query"]
if query != nil {
if c.GetHeader("x-ms-cosmos-is-query-plan-request") != "" {
c.IndentedJSON(http.StatusOK, constants.QueryPlanResponse)
return
}
2024-02-15 22:13:11 +00:00
var queryParameters map[string]interface{}
if paramsArray, ok := requestBody["parameters"].([]interface{}); ok {
queryParameters = parametersToMap(paramsArray)
}
2024-02-10 23:44:20 +00:00
// TODO: Handle these {"query":"select c.id, c._self, c._rid, c._ts, [c[\"pk\"]] as _partitionKeyValue from c"}
2024-02-15 22:13:11 +00:00
docs, status := repositories.ExecuteQueryDocuments(databaseId, collectionId, query.(string), queryParameters)
if status != repositorymodels.StatusOk {
2024-02-11 23:54:12 +00:00
// TODO: Currently we return everything if the query fails
GetAllDocuments(c)
return
}
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "Documents": docs, "_count": len(docs)})
2024-02-10 23:44:20 +00:00
return
}
if requestBody["id"] == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "BadRequest"})
return
}
status := repositories.CreateDocument(databaseId, collectionId, requestBody)
if status == repositorymodels.Conflict {
2024-02-10 23:44:20 +00:00
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
return
}
if status == repositorymodels.StatusOk {
2024-02-10 23:44:20 +00:00
c.IndentedJSON(http.StatusCreated, requestBody)
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
}
2024-02-15 22:13:11 +00:00
func parametersToMap(pairs []interface{}) map[string]interface{} {
result := make(map[string]interface{})
for _, pair := range pairs {
if pairMap, ok := pair.(map[string]interface{}); ok {
result[pairMap["name"].(string)] = pairMap["value"]
}
}
return result
}