Files
rostpoliplast/backend/internal/handlers/Bale_handlers.go
T
2026-04-29 14:27:16 +03:00

151 lines
4.3 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
"gitea.d3m0k1d.ru/d3m0k1d/rostpoliplast/backend/internal/mq"
"gitea.d3m0k1d.ru/d3m0k1d/rostpoliplast/backend/internal/storage"
)
type BaleHandlers struct {
Repo *storage.Repository
MQ *mq.RabbitMQ
}
func (h *BaleHandlers) RegisterRoutes(g *gin.RouterGroup) {
g.GET("/bales", h.GetBales)
g.GET("/bales/:id", h.GetBaleByID)
g.POST("/bales", h.CreateBale)
g.PUT("/bales/:id", h.UpdateBale)
g.DELETE("/bales/:id", h.DeleteBale)
}
// GetBales Получить список всех тюков
// @Summary Получить все тюки
// @Description Возвращает список всех тюков
// @Tags bales
// @Accept json
// @Produce json
// @Success 200 {array} storage.Bale
// @Router /bales [get]
func (h *BaleHandlers) GetBales(c *gin.Context) {
bales, err := h.Repo.GetBales(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, bales)
}
// GetBaleByID Получить тюк по ID
// @Summary Получить тюк по ID
// @Description Возвращает тюк по ID
// @Tags bales
// @Accept json
// @Produce json
// @Param id path string true "ID тюка"
// @Success 200 {object} storage.Bale
// @Router /bales/{id} [get]
func (h *BaleHandlers) GetBaleByID(c *gin.Context) {
id := c.Param("id")
bale, err := h.Repo.GetBaleByID(c.Request.Context(), id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
return
}
c.JSON(http.StatusOK, bale)
}
// CreateBale Создать новый тюк
// @Summary Создать тюк
// @Description Создаёт новый тюк и отправляет в очередь задач RabbitMQ
// @Tags bales
// @Accept json
// @Produce json
// @Param bale body storage.Bale false "Данные тюка"
// @Param type query string false "Тип тюка (для QR кодов)"
// @Success 201 {object} storage.Bale
// @Router /bales [post]
func (h *BaleHandlers) CreateBale(c *gin.Context) {
var input storage.Bale
if err := c.ShouldBindJSON(&input); err != nil && err.Error() != "EOF" {
typeName := c.Query("type")
if typeName == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
bt, err := h.Repo.GetBaleTypeByType(c.Request.Context(), typeName)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "type not found"})
return
}
input.TypeID = bt.ID
} else if input.TypeID == 0 && c.Query("type") != "" {
typeName := c.Query("type")
bt, err := h.Repo.GetBaleTypeByType(c.Request.Context(), typeName)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "type not found"})
return
}
input.TypeID = bt.ID
}
bale, err := h.Repo.CreateBale(c.Request.Context(), input)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if h.MQ != nil {
data, _ := json.Marshal(bale)
h.MQ.Publish(c.Request.Context(), data)
}
c.JSON(http.StatusCreated, bale)
}
// UpdateBale Обновить тюк
// @Summary Обновить тюк
// @Description Обновляет данные тюка по ID
// @Tags bales
// @Accept json
// @Produce json
// @Param id path string true "ID тюка"
// @Param bale body storage.Bale true "Данные тюка"
// @Success 200 {object} storage.Bale
// @Router /bales/{id} [put]
func (h *BaleHandlers) UpdateBale(c *gin.Context) {
id := c.Param("id")
var input storage.Bale
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
bale, err := h.Repo.UpdateBale(c.Request.Context(), id, input)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, bale)
}
// DeleteBale Удалить тюк
// @Summary Удалить тюк
// @Description Удаляет тюк по ID
// @Tags bales
// @Accept json
// @Produce json
// @Param id path string true "ID тюка"
// @Success 200 {object} map[string]bool
// @Router /bales/{id} [delete]
func (h *BaleHandlers) DeleteBale(c *gin.Context) {
id := c.Param("id")
if err := h.Repo.DeleteBale(c.Request.Context(), id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"deleted": true})
}