2024-02-10 21:05:08 +02:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-06-01 02:10:20 +03:00
|
|
|
"fmt"
|
2024-02-10 21:05:08 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-02-12 21:38:03 +02:00
|
|
|
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
2024-02-10 21:05:08 +02:00
|
|
|
)
|
|
|
|
|
2024-12-18 19:39:57 +02:00
|
|
|
func (h *Handlers) GetAllStoredProcedures(c *gin.Context) {
|
2024-02-10 21:05:08 +02:00
|
|
|
databaseId := c.Param("databaseId")
|
|
|
|
collectionId := c.Param("collId")
|
|
|
|
|
2024-12-18 19:39:57 +02:00
|
|
|
sps, status := h.repository.GetAllStoredProcedures(databaseId, collectionId)
|
2024-02-10 21:05:08 +02:00
|
|
|
|
2024-02-12 21:38:03 +02:00
|
|
|
if status == repositorymodels.StatusOk {
|
2024-06-01 02:10:20 +03:00
|
|
|
c.Header("x-ms-item-count", fmt.Sprintf("%d", len(sps)))
|
2024-02-10 21:05:08 +02:00
|
|
|
c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "StoredProcedures": sps, "_count": len(sps)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
|
|
|
}
|
2025-01-06 20:45:43 +02:00
|
|
|
|
|
|
|
func (h *Handlers) GetStoredProcedure(c *gin.Context) {
|
|
|
|
databaseId := c.Param("databaseId")
|
|
|
|
collectionId := c.Param("collId")
|
|
|
|
spId := c.Param("spId")
|
|
|
|
|
|
|
|
sp, status := h.repository.GetStoredProcedure(databaseId, collectionId, spId)
|
|
|
|
|
|
|
|
if status == repositorymodels.StatusOk {
|
|
|
|
c.IndentedJSON(http.StatusOK, sp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == repositorymodels.StatusNotFound {
|
|
|
|
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) DeleteStoredProcedure(c *gin.Context) {
|
|
|
|
databaseId := c.Param("databaseId")
|
|
|
|
collectionId := c.Param("collId")
|
|
|
|
spId := c.Param("spId")
|
|
|
|
|
|
|
|
status := h.repository.DeleteStoredProcedure(databaseId, collectionId, spId)
|
|
|
|
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 (h *Handlers) ReplaceStoredProcedure(c *gin.Context) {
|
|
|
|
databaseId := c.Param("databaseId")
|
|
|
|
collectionId := c.Param("collId")
|
|
|
|
spId := c.Param("spId")
|
|
|
|
|
|
|
|
var sp repositorymodels.StoredProcedure
|
|
|
|
if err := c.BindJSON(&sp); err != nil {
|
|
|
|
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
status := h.repository.DeleteStoredProcedure(databaseId, collectionId, spId)
|
|
|
|
if status == repositorymodels.StatusNotFound {
|
|
|
|
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
createdSP, status := h.repository.CreateStoredProcedure(databaseId, collectionId, sp)
|
|
|
|
if status == repositorymodels.Conflict {
|
|
|
|
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == repositorymodels.StatusOk {
|
|
|
|
c.IndentedJSON(http.StatusOK, createdSP)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) CreateStoredProcedure(c *gin.Context) {
|
|
|
|
databaseId := c.Param("databaseId")
|
|
|
|
collectionId := c.Param("collId")
|
|
|
|
|
|
|
|
var sp repositorymodels.StoredProcedure
|
|
|
|
if err := c.BindJSON(&sp); err != nil {
|
|
|
|
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
createdSP, status := h.repository.CreateStoredProcedure(databaseId, collectionId, sp)
|
|
|
|
if status == repositorymodels.Conflict {
|
|
|
|
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == repositorymodels.StatusOk {
|
|
|
|
c.IndentedJSON(http.StatusCreated, createdSP)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
|
|
|
|
}
|