mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-19 08:50:46 +00:00
Added Documents CURD
This commit is contained in:
@@ -12,7 +12,7 @@ func GetAllCollections(c *gin.Context) {
|
||||
|
||||
collections, status := repositories.GetAllCollections(databaseId)
|
||||
if status == repositories.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "DocumentCollections": collections})
|
||||
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "DocumentCollections": collections, "_count": len(collections)})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
func GetAllDatabases(c *gin.Context) {
|
||||
databases, status := repositories.GetAllDatabases()
|
||||
if status == repositories.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "Databases": databases})
|
||||
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "Databases": databases, "_count": len(databases)})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
101
api/handlers/documents.go
Normal file
101
api/handlers/documents.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/constants"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
func GetAllDocuments(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
documents, status := repositories.GetAllDocuments(databaseId, collectionId)
|
||||
if status == repositories.StatusOk {
|
||||
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 == repositories.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, document)
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositories.StatusNotFound {
|
||||
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 == repositories.StatusOk {
|
||||
c.Status(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositories.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// TODO: Handle these {"query":"select c.id, c._self, c._rid, c._ts, [c[\"pk\"]] as _partitionKeyValue from c"}
|
||||
GetAllDocuments(c)
|
||||
return
|
||||
}
|
||||
|
||||
if requestBody["id"] == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"message": "BadRequest"})
|
||||
return
|
||||
}
|
||||
|
||||
status := repositories.CreateDocument(databaseId, collectionId, requestBody)
|
||||
if status == repositories.Conflict {
|
||||
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositories.StatusOk {
|
||||
c.IndentedJSON(http.StatusCreated, requestBody)
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
30
api/handlers/partition_key_ranges.go
Normal file
30
api/handlers/partition_key_ranges.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/internal/repositories"
|
||||
)
|
||||
|
||||
func GetPartitionKeyRanges(c *gin.Context) {
|
||||
databaseId := c.Param("databaseId")
|
||||
collectionId := c.Param("collId")
|
||||
|
||||
partitionKeyRanges, status := repositories.GetPartitionKeyRanges(databaseId, collectionId)
|
||||
if status == repositories.StatusOk {
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"_rid": "",
|
||||
"_count": len(partitionKeyRanges),
|
||||
"PartitionKeyRanges": partitionKeyRanges,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if status == repositories.StatusNotFound {
|
||||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
||||
}
|
||||
@@ -1,42 +1,12 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pikami/cosmium/api/config"
|
||||
"github.com/pikami/cosmium/internal/constants"
|
||||
)
|
||||
|
||||
func GetServerInfo(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusOK, gin.H{
|
||||
"_self": "",
|
||||
"id": config.Config.DatabaseAccount,
|
||||
"_rid": fmt.Sprintf("%s.%s", config.Config.DatabaseAccount, config.Config.DatabaseDomain),
|
||||
"media": "//media/",
|
||||
"addresses": "//addresses/",
|
||||
"_dbs": "//dbs/",
|
||||
"writableLocations": []map[string]interface{}{
|
||||
{
|
||||
"name": "South Central US",
|
||||
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"readableLocations": []map[string]interface{}{
|
||||
{
|
||||
"name": "South Central US",
|
||||
"databaseAccountEndpoint": config.Config.DatabaseEndpoint,
|
||||
},
|
||||
},
|
||||
"enableMultipleWriteLocations": false,
|
||||
"userReplicationPolicy": map[string]interface{}{
|
||||
"asyncReplication": false,
|
||||
"minReplicaSetSize": 1,
|
||||
"maxReplicasetSize": 4,
|
||||
},
|
||||
"userConsistencyPolicy": map[string]interface{}{"defaultConsistencyLevel": "Session"},
|
||||
"systemReplicationPolicy": map[string]interface{}{"minReplicaSetSize": 1, "maxReplicasetSize": 4},
|
||||
"readPolicy": map[string]interface{}{"primaryReadCoefficient": 1, "secondaryReadCoefficient": 1},
|
||||
"queryEngineConfiguration": "{\"allowNewKeywords\":true,\"maxJoinsPerSqlQuery\":10,\"maxQueryRequestTimeoutFraction\":0.9,\"maxSqlQueryInputLength\":524288,\"maxUdfRefPerSqlQuery\":10,\"queryMaxInMemorySortDocumentCount\":-1000,\"spatialMaxGeometryPointCount\":256,\"sqlAllowNonFiniteNumbers\":false,\"sqlDisableOptimizationFlags\":0,\"enableSpatialIndexing\":true,\"maxInExpressionItemsCount\":2147483647,\"maxLogicalAndPerSqlQuery\":2147483647,\"maxLogicalOrPerSqlQuery\":2147483647,\"maxSpatialQueryCells\":2147483647,\"sqlAllowAggregateFunctions\":true,\"sqlAllowGroupByClause\":true,\"sqlAllowLike\":true,\"sqlAllowSubQuery\":true,\"sqlAllowScalarSubQuery\":true,\"sqlAllowTop\":true}",
|
||||
})
|
||||
c.IndentedJSON(http.StatusOK, constants.ServerInfoResponse)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,13 @@ import (
|
||||
func CreateRouter() *gin.Engine {
|
||||
router := gin.Default()
|
||||
|
||||
router.GET("/dbs/:databaseId/colls/:collId/pkranges", handlers.GetPartitionKeyRanges)
|
||||
|
||||
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.DELETE("/dbs/:databaseId/colls/:collId/docs/:docId", handlers.DeleteDocument)
|
||||
|
||||
router.POST("/dbs/:databaseId/colls", handlers.CreateCollection)
|
||||
router.GET("/dbs/:databaseId/colls", handlers.GetAllCollections)
|
||||
router.GET("/dbs/:databaseId/colls/:collId", handlers.GetCollection)
|
||||
|
||||
Reference in New Issue
Block a user