Added Collections CRUD

This commit is contained in:
Pijus Kamandulis
2024-02-10 18:52:41 +02:00
parent f30d0528bc
commit 5dc7d87fba
5 changed files with 179 additions and 5 deletions

View File

@@ -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"})
}

View File

@@ -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 {