chore: add qe and integrations with rabbit mq
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"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 {
|
||||
DB *pgxpool.Pool
|
||||
Repo *storage.Repository
|
||||
MQ *mq.RabbitMQ
|
||||
}
|
||||
|
||||
func (h *BaleHandlers) RegisterRoutes(g *gin.RouterGroup) {
|
||||
@@ -20,22 +23,128 @@ func (h *BaleHandlers) RegisterRoutes(g *gin.RouterGroup) {
|
||||
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) {
|
||||
c.JSON(200, gin.H{"message": "GetBales"})
|
||||
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) {
|
||||
c.JSON(200, gin.H{"message": "GetBaleByID"})
|
||||
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) {
|
||||
c.JSON(200, gin.H{"message": "CreateBale"})
|
||||
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) {
|
||||
c.JSON(200, gin.H{"message": "UpdateBale"})
|
||||
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) {
|
||||
c.JSON(200, gin.H{"message": "DeleteBale"})
|
||||
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})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user