2024-02-10 15:17:34 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-06-01 00:10:20 +01:00
|
|
|
"fmt"
|
2024-02-10 15:17:34 +00:00
|
|
|
"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 15:17:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetAllDatabases(c *gin.Context) {
|
|
|
|
databases, status := repositories.GetAllDatabases()
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.StatusOk {
|
2024-06-01 00:10:20 +01:00
|
|
|
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(databases)))
|
2024-02-26 19:03:47 +00:00
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{
|
|
|
|
"_rid": "",
|
|
|
|
"Databases": databases,
|
|
|
|
"_count": len(databases),
|
|
|
|
})
|
2024-02-10 15:17:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDatabase(c *gin.Context) {
|
2024-02-10 16:52:41 +00:00
|
|
|
id := c.Param("databaseId")
|
2024-02-10 15:17:34 +00:00
|
|
|
|
|
|
|
database, status := repositories.GetDatabase(id)
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.StatusOk {
|
2024-02-10 15:17:34 +00:00
|
|
|
c.IndentedJSON(http.StatusOK, database)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.StatusNotFound {
|
2024-02-10 15:17:34 +00:00
|
|
|
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteDatabase(c *gin.Context) {
|
2024-02-10 16:52:41 +00:00
|
|
|
id := c.Param("databaseId")
|
2024-02-10 15:17:34 +00:00
|
|
|
|
|
|
|
status := repositories.DeleteDatabase(id)
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.StatusOk {
|
2024-02-10 15:17:34 +00:00
|
|
|
c.Status(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.StatusNotFound {
|
2024-02-10 15:17:34 +00:00
|
|
|
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateDatabase(c *gin.Context) {
|
2024-02-12 19:38:03 +00:00
|
|
|
var newDatabase repositorymodels.Database
|
2024-02-10 15:17:34 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:13:04 +00:00
|
|
|
createdDatabase, status := repositories.CreateDatabase(newDatabase)
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.Conflict {
|
2024-02-10 15:17:34 +00:00
|
|
|
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-12 19:38:03 +00:00
|
|
|
if status == repositorymodels.StatusOk {
|
2024-02-25 20:13:04 +00:00
|
|
|
c.IndentedJSON(http.StatusCreated, createdDatabase)
|
2024-02-10 15:17:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
|
|
|
}
|