feat: improve responses

This commit is contained in:
Siesta
2025-07-27 22:57:43 +02:00
parent aa7d36158d
commit cc317ca661
5 changed files with 56 additions and 48 deletions
+7 -11
View File
@@ -5,7 +5,6 @@ import (
"net/http"
"github.com/siestaw/laterna/server/cmd/internal/db"
"github.com/siestaw/laterna/server/cmd/internal/logger"
"github.com/siestaw/laterna/server/cmd/internal/middleware"
"github.com/siestaw/laterna/server/cmd/internal/models"
"github.com/siestaw/laterna/server/cmd/utils"
@@ -20,28 +19,25 @@ func createController(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
ID, err := db.CreateController()
if err != nil {
utils.HTTPResponseHandler(w, r, http.StatusInternalServerError, "An error occured. Check the server logs for more information")
utils.ErrorResponse(w, http.StatusInternalServerError, "An error occured. Check the server logs for more information")
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]int64{"ID": ID})
utils.SuccessResponse(w, http.StatusOK, models.CreateData{Created: int(ID)})
}
func deleteController(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var req models.ControllerRequests
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
logger.HTTPLogger.Print(err)
utils.HTTPResponseHandler(w, r, http.StatusBadRequest, "Invalid JSON")
utils.ErrorResponse(w, http.StatusBadRequest, "Invalid JSON")
return
}
if !db.ControllerExists(req.ID) || req.ID <= 0 {
utils.HTTPResponseHandler(w, r, http.StatusBadRequest,"Invalid ID")
utils.ErrorResponse(w, http.StatusBadRequest, "Invalid ID")
return
}
if db.DeleteController(req.ID) != nil {
utils.HTTPResponseHandler(w, r, http.StatusInternalServerError,"An error occured. Check the server logs for more information")
utils.ErrorResponse(w, http.StatusInternalServerError, "An error occured. Check the server logs for more information")
return
}
utils.HTTPResponseHandler(w, r, http.StatusOK, "Success")
}
utils.SuccessResponse(w, http.StatusOK, models.DeleteData{Deleted: req.ID})
}