2024-02-10 16:52:41 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/pikami/cosmium/internal/repositories"
|
2024-02-12 19:38:03 +00:00
|
|
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
2024-02-10 16:52:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetAllCollections(c *gin.Context) {
|
|
|
|
databaseId := c.Param("databaseId")
|
|
|
|
|
|
|
|
collections, status := repositories.GetAllCollections(databaseId)
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.StatusOk {
|
2024-02-10 23:44:20 +00:00
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "DocumentCollections": collections, "_count": len(collections)})
|
2024-02-10 16:52:41 +00:00
|
|
|
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)
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.StatusOk {
|
2024-02-10 16:52:41 +00:00
|
|
|
c.IndentedJSON(http.StatusOK, collection)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.StatusNotFound {
|
2024-02-10 16:52:41 +00:00
|
|
|
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)
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.StatusOk {
|
2024-02-10 16:52:41 +00:00
|
|
|
c.Status(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.StatusNotFound {
|
2024-02-10 16:52:41 +00:00
|
|
|
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")
|
2024-02-12 19:38:03 +00:00
|
|
|
var newCollection repositorymodels.Collection
|
2024-02-10 16:52:41 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
createdCollection, status := repositories.CreateCollection(databaseId, newCollection)
|
|
|
|
if status == repositorymodels.Conflict {
|
2024-02-10 16:52:41 +00:00
|
|
|
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.StatusOk {
|
|
|
|
c.IndentedJSON(http.StatusCreated, createdCollection)
|
2024-02-10 16:52:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
|
|
|
}
|