/
Arsinenko
/
CompClubAPI_Go
Обзор
Документация
Войти
/
Arsinenko
/
CompClubAPI_Go
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
internal/handlers/equipment_handler.go
171 строка
6 KB
Arsinenko
Update
08 ноя 2025, 13:12
08 ноя 2025, 13:12
2f36b0f
Код
Авторство
О чём код?
package handlers import ( "CompClubAPI/internal/services" "net/http" "strconv" "github.com/gin-gonic/gin" ) // BuyEquipmentHandler godoc // @Summary Buy new equipment // @Description Registers new equipment in the system (e.g., computer, chair, etc.) // @Tags equipment // @Accept json // @Produce json // @Param request body services.CreateEquipmentRequest true "Equipment creation data" // @Success 200 {object} map[string]interface{} "Equipment created successfully" // @Failure 400 {object} map[string]string "Invalid request body or equipment already exists" // @Failure 500 {object} map[string]string "Internal server error" // @Router /equipment/ [post] func BuyEquipmentHandler(s services.EquipmentService) gin.HandlerFunc { return func(c *gin.Context) { var request services.CreateEquipmentRequest if err := c.ShouldBindJSON(&request); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } equipment, err := s.BuyEquipment(request) if err != nil { if err.Error() == "equipment already exists" { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"equipment": equipment}) } } // GetEquipmentHandler godoc // @Summary Get equipment by ID // @Description Retrieves detailed information about a specific equipment item // @Tags equipment // @Produce json // @Param id path int true "Equipment ID" // @Success 200 {object} map[string]interface{} "Equipment details" // @Failure 400 {object} map[string]string "Invalid or missing ID" // @Failure 404 {object} map[string]string "Equipment not found" // @Failure 500 {object} map[string]string "Internal server error" // @Router /equipment/{id} [get] func GetEquipmentHandler(s services.EquipmentService) gin.HandlerFunc { return func(c *gin.Context) { idStr := c.Param("id") if idStr == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "id is required"}) return } id, err := strconv.Atoi(idStr) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } equipment, err := s.GetEquipment(int32(id)) if err != nil { if err.Error() == "equipment not found" { c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"equipment": equipment}) } } // GetAllEquipments godoc // @Summary Get all equipment // @Description Retrieves a list of all registered equipment items // @Tags equipment // @Produce json // @Success 200 {object} map[string]interface{} "List of equipment" // @Failure 500 {object} map[string]string "Internal server error" // @Router /equipment/ [get] func GetAllEquipments(s services.EquipmentService) gin.HandlerFunc { return func(c *gin.Context) { equipments, err := s.GetAllEquipments() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"equipments": equipments}) } } // ChangeStatusHandler godoc // @Summary Toggle equipment status // @Description Changes the operational status of equipment (e.g., from 'working' to 'broken' or vice versa) // @Tags equipment // @Produce json // @Param id path int true "Equipment ID" // @Success 200 {object} map[string]string "Status changed successfully" // @Failure 400 {object} map[string]string "Invalid or missing ID" // @Failure 500 {object} map[string]string "Internal server error" // @Router /equipment/{id} [put] func ChangeStatusHandler(s services.EquipmentService) gin.HandlerFunc { return func(c *gin.Context) { idStr := c.Param("id") if idStr == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "id is required"}) return } id, err := strconv.Atoi(idStr) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } err = s.ChangeStatus(int32(id)) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "status changed"}) } } // DeleteEquipmentHandler godoc // @Summary Delete equipment // @Description Permanently removes an equipment item from the system by ID // @Tags equipment // @Produce json // @Param id path int true "Equipment ID" // @Success 204 {object} nil "Equipment deleted successfully" // @Failure 400 {object} map[string]string "Invalid ID or equipment not found" // @Failure 500 {object} map[string]string "Internal server error" // @Router /equipment/{id} [delete] func DeleteEquipmentHandler(s services.EquipmentService) gin.HandlerFunc { return func(c *gin.Context) { idStr := c.Param("id") if idStr == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "id is required"}) return } id, err := strconv.Atoi(idStr) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } err = s.DeleteEquipment(int32(id)) if err != nil { if err.Error() == "equipment not found" { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.Status(http.StatusNoContent) } } // InitializeEquipmentHandlers registers equipment-related routes // These endpoints are public (no authentication middleware applied by default) func InitializeEquipmentHandlers(r *gin.RouterGroup, s services.EquipmentService) { r.POST("/", BuyEquipmentHandler(s)) r.GET("/:id", GetEquipmentHandler(s)) r.GET("/", GetAllEquipments(s)) r.PUT("/:id", ChangeStatusHandler(s)) r.DELETE("/:id", DeleteEquipmentHandler(s)) }