Extract constants instead of duplicating literals

This commit is contained in:
Pijus Kamandulis
2025-05-14 20:01:46 +03:00
parent 7e0c10479b
commit e20a6ca7cd
9 changed files with 87 additions and 72 deletions

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore"
)
@@ -20,7 +21,7 @@ func (h *Handlers) GetAllTriggers(c *gin.Context) {
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) GetTrigger(c *gin.Context) {
@@ -36,11 +37,11 @@ func (h *Handlers) GetTrigger(c *gin.Context) {
}
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) DeleteTrigger(c *gin.Context) {
@@ -55,11 +56,11 @@ func (h *Handlers) DeleteTrigger(c *gin.Context) {
}
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) ReplaceTrigger(c *gin.Context) {
@@ -69,19 +70,19 @@ func (h *Handlers) ReplaceTrigger(c *gin.Context) {
var trigger datastore.Trigger
if err := c.BindJSON(&trigger); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"})
c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return
}
status := h.dataStore.DeleteTrigger(databaseId, collectionId, triggerId)
if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"})
c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return
}
createdTrigger, status := h.dataStore.CreateTrigger(databaseId, collectionId, trigger)
if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return
}
@@ -90,7 +91,7 @@ func (h *Handlers) ReplaceTrigger(c *gin.Context) {
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}
func (h *Handlers) CreateTrigger(c *gin.Context) {
@@ -99,13 +100,13 @@ func (h *Handlers) CreateTrigger(c *gin.Context) {
var trigger datastore.Trigger
if err := c.BindJSON(&trigger); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid body"})
c.IndentedJSON(http.StatusBadRequest, constants.BadRequestResponse)
return
}
createdTrigger, status := h.dataStore.CreateTrigger(databaseId, collectionId, trigger)
if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"})
c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return
}
@@ -114,5 +115,5 @@ func (h *Handlers) CreateTrigger(c *gin.Context) {
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"})
c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
}