cosmium/api/handlers/databases.go

85 lines
2.0 KiB
Go

package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/repositories"
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
)
func GetAllDatabases(c *gin.Context) {
databases, status := repositories.GetAllDatabases()
if status == repositorymodels.StatusOk {
c.IndentedJSON(http.StatusOK, gin.H{
"_rid": "",
"Databases": databases,
"_count": len(databases),
})
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
}
func GetDatabase(c *gin.Context) {
id := c.Param("databaseId")
database, status := repositories.GetDatabase(id)
if status == repositorymodels.StatusOk {
c.IndentedJSON(http.StatusOK, database)
return
}
if status == repositorymodels.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
}
func DeleteDatabase(c *gin.Context) {
id := c.Param("databaseId")
status := repositories.DeleteDatabase(id)
if status == repositorymodels.StatusOk {
c.Status(http.StatusNoContent)
return
}
if status == repositorymodels.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
}
func CreateDatabase(c *gin.Context) {
var newDatabase repositorymodels.Database
if err := c.BindJSON(&newDatabase); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
if newDatabase.ID == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "BadRequest"})
return
}
createdDatabase, status := repositories.CreateDatabase(newDatabase)
if status == repositorymodels.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
return
}
if status == repositorymodels.StatusOk {
c.IndentedJSON(http.StatusCreated, createdDatabase)
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
}