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" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
) )
@ -24,7 +25,7 @@ func (h *Handlers) GetAllCollections(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) GetCollection(c *gin.Context) { func (h *Handlers) GetCollection(c *gin.Context) {
@ -38,11 +39,11 @@ func (h *Handlers) GetCollection(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) DeleteCollection(c *gin.Context) { func (h *Handlers) DeleteCollection(c *gin.Context) {
@ -56,11 +57,11 @@ func (h *Handlers) DeleteCollection(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) CreateCollection(c *gin.Context) { func (h *Handlers) CreateCollection(c *gin.Context) {
@ -73,13 +74,13 @@ func (h *Handlers) CreateCollection(c *gin.Context) {
} }
if newCollection.ID == "" { if newCollection.ID == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "BadRequest"}) c.JSON(http.StatusBadRequest, constants.BadRequestResponse)
return return
} }
createdCollection, status := h.dataStore.CreateCollection(databaseId, newCollection) createdCollection, status := h.dataStore.CreateCollection(databaseId, newCollection)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@ -88,5 +89,5 @@ func (h *Handlers) CreateCollection(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }

View File

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
) )
@ -20,7 +21,7 @@ func (h *Handlers) GetAllDatabases(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) GetDatabase(c *gin.Context) { func (h *Handlers) GetDatabase(c *gin.Context) {
@ -33,11 +34,11 @@ func (h *Handlers) GetDatabase(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) DeleteDatabase(c *gin.Context) { func (h *Handlers) DeleteDatabase(c *gin.Context) {
@ -50,11 +51,11 @@ func (h *Handlers) DeleteDatabase(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) CreateDatabase(c *gin.Context) { func (h *Handlers) CreateDatabase(c *gin.Context) {
@ -66,13 +67,13 @@ func (h *Handlers) CreateDatabase(c *gin.Context) {
} }
if newDatabase.ID == "" { if newDatabase.ID == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "BadRequest"}) c.JSON(http.StatusBadRequest, constants.BadRequestResponse)
return return
} }
createdDatabase, status := h.dataStore.CreateDatabase(newDatabase) createdDatabase, status := h.dataStore.CreateDatabase(newDatabase)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@ -81,5 +82,5 @@ func (h *Handlers) CreateDatabase(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }

View File

@ -35,7 +35,7 @@ func (h *Handlers) GetAllDocuments(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) GetDocument(c *gin.Context) { func (h *Handlers) GetDocument(c *gin.Context) {
@ -50,11 +50,11 @@ func (h *Handlers) GetDocument(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) DeleteDocument(c *gin.Context) { func (h *Handlers) DeleteDocument(c *gin.Context) {
@ -69,11 +69,11 @@ func (h *Handlers) DeleteDocument(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
// TODO: Maybe move "replace" logic to data store // TODO: Maybe move "replace" logic to data store
@ -90,13 +90,13 @@ func (h *Handlers) ReplaceDocument(c *gin.Context) {
status := h.dataStore.DeleteDocument(databaseId, collectionId, documentId) status := h.dataStore.DeleteDocument(databaseId, collectionId, documentId)
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, requestBody) createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, requestBody)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@ -105,7 +105,7 @@ func (h *Handlers) ReplaceDocument(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) PatchDocument(c *gin.Context) { func (h *Handlers) PatchDocument(c *gin.Context) {
@ -115,7 +115,7 @@ func (h *Handlers) PatchDocument(c *gin.Context) {
document, status := h.dataStore.GetDocument(databaseId, collectionId, documentId) document, status := h.dataStore.GetDocument(databaseId, collectionId, documentId)
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
@ -166,13 +166,13 @@ func (h *Handlers) PatchDocument(c *gin.Context) {
status = h.dataStore.DeleteDocument(databaseId, collectionId, documentId) status = h.dataStore.DeleteDocument(databaseId, collectionId, documentId)
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, modifiedDocument) createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, modifiedDocument)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@ -181,7 +181,7 @@ func (h *Handlers) PatchDocument(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func (h *Handlers) DocumentsPost(c *gin.Context) { func (h *Handlers) DocumentsPost(c *gin.Context) {
@ -208,7 +208,7 @@ func (h *Handlers) DocumentsPost(c *gin.Context) {
} }
if requestBody["id"] == "" { if requestBody["id"] == "" {
c.JSON(http.StatusBadRequest, gin.H{"message": "BadRequest"}) c.JSON(http.StatusBadRequest, constants.BadRequestResponse)
return return
} }
@ -219,7 +219,7 @@ func (h *Handlers) DocumentsPost(c *gin.Context) {
createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, requestBody) createdDocument, status := h.dataStore.CreateDocument(databaseId, collectionId, requestBody)
if status == datastore.Conflict { if status == datastore.Conflict {
c.IndentedJSON(http.StatusConflict, gin.H{"message": "Conflict"}) c.IndentedJSON(http.StatusConflict, constants.ConflictResponse)
return return
} }
@ -228,7 +228,7 @@ func (h *Handlers) DocumentsPost(c *gin.Context) {
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }
func parametersToMap(pairs []interface{}) map[string]interface{} { func parametersToMap(pairs []interface{}) map[string]interface{} {

View File

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/pikami/cosmium/internal/constants"
"github.com/pikami/cosmium/internal/datastore" "github.com/pikami/cosmium/internal/datastore"
"github.com/pikami/cosmium/internal/resourceid" "github.com/pikami/cosmium/internal/resourceid"
) )
@ -42,9 +43,9 @@ func (h *Handlers) GetPartitionKeyRanges(c *gin.Context) {
} }
if status == datastore.StatusNotFound { if status == datastore.StatusNotFound {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "NotFound"}) c.IndentedJSON(http.StatusNotFound, constants.NotFoundResponse)
return return
} }
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) c.IndentedJSON(http.StatusInternalServerError, constants.UnknownErrorResponse)
} }

View File

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

View File

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

View File

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

View File

@ -19,7 +19,7 @@ func Test_Authentication(t *testing.T) {
t.Run("Should get 200 when correct account key is used", func(t *testing.T) { t.Run("Should get 200 when correct account key is used", func(t *testing.T) {
ts.DataStore.DeleteDatabase(testDatabaseName) ts.DataStore.DeleteDatabase(testDatabaseName)
client, err := azcosmos.NewClientFromConnectionString( client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, config.DefaultAccountKey), formatConnectionString(ts.URL, config.DefaultAccountKey),
&azcosmos.ClientOptions{}, &azcosmos.ClientOptions{},
) )
assert.Nil(t, err) assert.Nil(t, err)
@ -35,7 +35,7 @@ func Test_Authentication(t *testing.T) {
t.Run("Should get 401 when wrong account key is used", func(t *testing.T) { t.Run("Should get 401 when wrong account key is used", func(t *testing.T) {
ts.DataStore.DeleteDatabase(testDatabaseName) ts.DataStore.DeleteDatabase(testDatabaseName)
client, err := azcosmos.NewClientFromConnectionString( client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"), formatConnectionString(ts.URL, "AAAA"),
&azcosmos.ClientOptions{}, &azcosmos.ClientOptions{},
) )
assert.Nil(t, err) assert.Nil(t, err)
@ -72,7 +72,7 @@ func Test_Authentication_Disabled(t *testing.T) {
t.Run("Should get 200 when wrong account key is used, but authentication is dissabled", func(t *testing.T) { t.Run("Should get 200 when wrong account key is used, but authentication is dissabled", func(t *testing.T) {
ts.DataStore.DeleteDatabase(testDatabaseName) ts.DataStore.DeleteDatabase(testDatabaseName)
client, err := azcosmos.NewClientFromConnectionString( client, err := azcosmos.NewClientFromConnectionString(
fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", ts.URL, "AAAA"), formatConnectionString(ts.URL, "AAAA"),
&azcosmos.ClientOptions{}, &azcosmos.ClientOptions{},
) )
assert.Nil(t, err) assert.Nil(t, err)
@ -85,3 +85,7 @@ func Test_Authentication_Disabled(t *testing.T) {
assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName) assert.Equal(t, createResponse.DatabaseProperties.ID, testDatabaseName)
}) })
} }
func formatConnectionString(endpoint, key string) string {
return fmt.Sprintf("AccountEndpoint=%s;AccountKey=%s", endpoint, key)
}

View File

@ -30,3 +30,8 @@ var QueryPlanResponse = gin.H{
}, },
}, },
} }
var UnknownErrorResponse = gin.H{"message": "Unknown error"}
var NotFoundResponse = gin.H{"message": "NotFound"}
var ConflictResponse = gin.H{"message": "Conflict"}
var BadRequestResponse = gin.H{"message": "BadRequest"}