diff --git a/api/handlers/collections.go b/api/handlers/collections.go new file mode 100644 index 0000000..3291988 --- /dev/null +++ b/api/handlers/collections.go @@ -0,0 +1,84 @@ +package handlers + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/pikami/cosmium/internal/repositories" +) + +func GetAllCollections(c *gin.Context) { + databaseId := c.Param("databaseId") + + collections, status := repositories.GetAllCollections(databaseId) + if status == repositories.StatusOk { + c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "DocumentCollections": collections}) + return + } + + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) +} + +func GetCollection(c *gin.Context) { + databaseId := c.Param("databaseId") + id := c.Param("collId") + + collection, status := repositories.GetCollection(databaseId, id) + if status == repositories.StatusOk { + c.IndentedJSON(http.StatusOK, collection) + return + } + + if status == repositories.StatusNotFound { + c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) + return + } + + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) +} + +func DeleteCollection(c *gin.Context) { + databaseId := c.Param("databaseId") + id := c.Param("collId") + + status := repositories.DeleteCollection(databaseId, id) + 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 CreateCollection(c *gin.Context) { + databaseId := c.Param("databaseId") + var newCollection repositories.Collection + + if err := c.BindJSON(&newCollection); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) + return + } + + if newCollection.ID == "" { + c.JSON(http.StatusBadRequest, gin.H{"message": "BadRequest"}) + return + } + + status := repositories.CreateCollection(databaseId, newCollection) + if status == repositories.Conflict { + c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) + return + } + + if status == repositories.StatusOk { + c.IndentedJSON(http.StatusCreated, newCollection) + return + } + + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) +} diff --git a/api/handlers/databases.go b/api/handlers/databases.go index 5e27406..c8aa975 100644 --- a/api/handlers/databases.go +++ b/api/handlers/databases.go @@ -18,7 +18,7 @@ func GetAllDatabases(c *gin.Context) { } func GetDatabase(c *gin.Context) { - id := c.Param("id") + id := c.Param("databaseId") database, status := repositories.GetDatabase(id) if status == repositories.StatusOk { @@ -35,7 +35,7 @@ func GetDatabase(c *gin.Context) { } func DeleteDatabase(c *gin.Context) { - id := c.Param("id") + id := c.Param("databaseId") status := repositories.DeleteDatabase(id) if status == repositories.StatusOk { diff --git a/api/router.go b/api/router.go index 2630d68..bae8411 100644 --- a/api/router.go +++ b/api/router.go @@ -8,10 +8,16 @@ import ( func CreateRouter() *gin.Engine { router := gin.Default() - router.GET("/dbs/:id", handlers.GetDatabase) - router.DELETE("/dbs/:id", handlers.DeleteDatabase) - router.GET("/dbs", handlers.GetAllDatabases) + router.POST("/dbs/:databaseId/colls", handlers.CreateCollection) + router.GET("/dbs/:databaseId/colls", handlers.GetAllCollections) + router.GET("/dbs/:databaseId/colls/:collId", handlers.GetCollection) + router.DELETE("/dbs/:databaseId/colls/:collId", handlers.DeleteCollection) + router.POST("/dbs", handlers.CreateDatabase) + router.GET("/dbs", handlers.GetAllDatabases) + router.GET("/dbs/:databaseId", handlers.GetDatabase) + router.DELETE("/dbs/:databaseId", handlers.DeleteDatabase) + router.GET("/", handlers.GetServerInfo) return router diff --git a/internal/repositories/collections.go b/internal/repositories/collections.go new file mode 100644 index 0000000..38efe28 --- /dev/null +++ b/internal/repositories/collections.go @@ -0,0 +1,50 @@ +package repositories + +var collections = []Collection{ + {ID: "db1"}, + {ID: "db2"}, +} + +func GetAllCollections(databaseId string) ([]Collection, RepositoryStatus) { + var dbCollections []Collection + + for _, coll := range collections { + if coll.internals.databaseId == databaseId { + dbCollections = append(dbCollections, coll) + } + } + + return dbCollections, StatusOk +} + +func GetCollection(databaseId string, id string) (Collection, RepositoryStatus) { + for _, coll := range collections { + if coll.internals.databaseId == databaseId && coll.ID == id { + return coll, StatusOk + } + } + + return Collection{}, StatusNotFound +} + +func DeleteCollection(databaseId string, id string) RepositoryStatus { + for index, coll := range collections { + if coll.internals.databaseId == databaseId && coll.ID == id { + collections = append(collections[:index], collections[index+1:]...) + return StatusOk + } + } + + return StatusNotFound +} + +func CreateCollection(databaseId string, newCollection Collection) RepositoryStatus { + for _, coll := range collections { + if coll.internals.databaseId == databaseId && coll.ID == newCollection.ID { + return Conflict + } + } + + collections = append(collections, newCollection) + return StatusOk +} diff --git a/internal/repositories/models.go b/internal/repositories/models.go index 2f0c679..536dc08 100644 --- a/internal/repositories/models.go +++ b/internal/repositories/models.go @@ -11,3 +11,37 @@ const ( StatusNotFound = 2 Conflict = 3 ) + +type Collection struct { + ID string `json:"id"` + IndexingPolicy struct { + IndexingMode string `json:"indexingMode"` + Automatic bool `json:"automatic"` + IncludedPaths []struct { + Path string `json:"path"` + Indexes []struct { + Kind string `json:"kind"` + DataType string `json:"dataType"` + Precision int `json:"precision"` + } `json:"indexes"` + } `json:"includedPaths"` + ExcludedPaths []any `json:"excludedPaths"` + } `json:"indexingPolicy"` + PartitionKey struct { + Paths []string `json:"paths"` + Kind string `json:"kind"` + Version int `json:"Version"` + } `json:"partitionKey"` + Rid string `json:"_rid"` + Ts int `json:"_ts"` + Self string `json:"_self"` + Etag string `json:"_etag"` + Docs string `json:"_docs"` + Sprocs string `json:"_sprocs"` + Triggers string `json:"_triggers"` + Udfs string `json:"_udfs"` + Conflicts string `json:"_conflicts"` + internals struct { + databaseId string + } +}