2024-10-27 04:09:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
// getBoxesHandler handles the GET /boxes endpoint.
|
|
|
|
func GetBoxesHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-29 15:09:14 +00:00
|
|
|
log := GetLogger()
|
|
|
|
log.Info("Received %s request to %s", r.Method, r.URL)
|
|
|
|
|
2024-10-27 04:09:45 +00:00
|
|
|
var boxes []Box
|
|
|
|
db.Find(&boxes)
|
|
|
|
json.NewEncoder(w).Encode(boxes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetBoxHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-29 15:09:14 +00:00
|
|
|
log := GetLogger()
|
2024-10-27 04:09:45 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
|
|
|
|
|
|
|
var box Box
|
|
|
|
if err := db.First(&box, id).Error; err != nil {
|
2024-10-29 15:09:14 +00:00
|
|
|
log.Warn("Box not found with ID: %s", id)
|
2024-10-27 04:09:45 +00:00
|
|
|
http.Error(w, "Box not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(box)
|
|
|
|
}
|
|
|
|
|
|
|
|
// createBoxHandler handles the POST /boxes endpoint.
|
|
|
|
func CreateBoxHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-29 15:09:14 +00:00
|
|
|
log := GetLogger()
|
2024-10-27 04:09:45 +00:00
|
|
|
var box Box
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&box)
|
|
|
|
if err != nil {
|
2024-10-29 15:09:14 +00:00
|
|
|
log.Error("Failed to decode box creation request: %v", err)
|
2024-10-27 04:09:45 +00:00
|
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
db.Create(&box)
|
|
|
|
|
|
|
|
// Create a response struct to include the ID
|
|
|
|
type createBoxResponse struct {
|
|
|
|
ID uint `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
response := createBoxResponse{
|
|
|
|
ID: box.ID,
|
|
|
|
Name: box.Name,
|
|
|
|
}
|
|
|
|
|
2024-10-29 15:09:14 +00:00
|
|
|
log.DatabaseAction("create", fmt.Sprintf("Created box with ID %d", box.ID))
|
2024-10-27 04:09:45 +00:00
|
|
|
json.NewEncoder(w).Encode(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteBoxHandler handles the DELETE /boxes/{id} endpoint.
|
|
|
|
func DeleteBoxHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-29 15:09:14 +00:00
|
|
|
log := GetLogger()
|
2024-10-27 04:09:45 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
|
|
|
|
|
|
|
// Retrieve the box from the database
|
|
|
|
var box Box
|
|
|
|
if err := db.First(&box, id).Error; err != nil {
|
2024-10-29 15:09:14 +00:00
|
|
|
log.Warn("Attempt to delete non-existent box with ID: %s", id)
|
2024-10-27 04:09:45 +00:00
|
|
|
http.Error(w, "Box not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the box
|
|
|
|
db.Delete(&box)
|
2024-10-29 15:09:14 +00:00
|
|
|
log.DatabaseAction("delete", fmt.Sprintf("Deleted box with ID %d", box.ID))
|
2024-10-27 04:09:45 +00:00
|
|
|
w.WriteHeader(http.StatusNoContent) // 204 No Content
|
|
|
|
}
|