82 lines
2.0 KiB
Go
Raw Normal View History

package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/muyuqingfeng/iloom/shared/pkg/response"
ws "github.com/muyuqingfeng/iloom/shared/pkg/websocket"
"github.com/muyuqingfeng/iloom/textile-service/internal/service"
)
type InventoryHandler struct {
svc *service.InventoryService
wsHub *ws.Hub
}
type InboundRequest struct {
WarehouseID string `json:"warehouse_id" binding:"required"`
Quantity float64 `json:"quantity" binding:"required,gt=0"`
Rolls int `json:"rolls"`
PricePerMeter float64 `json:"price_per_meter"`
PriceNote string `json:"price_note"`
WarehouseLocation string `json:"warehouse_location"`
}
func NewInventoryHandler(svc *service.InventoryService, wsHub *ws.Hub) *InventoryHandler {
return &InventoryHandler{svc: svc, wsHub: wsHub}
}
func (h *InventoryHandler) Inbound(c *gin.Context) {
companyID := c.GetHeader("X-Company-ID")
userID := c.GetHeader("X-User-ID")
if companyID == "" || userID == "" {
response.BadRequest(c, "missing required headers")
return
}
planID := c.Param("id")
var req InboundRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, err.Error())
return
}
rec, err := h.svc.Inbound(c.Request.Context(), planID, companyID, userID, service.InboundRequest{
WarehouseID: req.WarehouseID,
Quantity: req.Quantity,
Rolls: req.Rolls,
PricePerMeter: req.PricePerMeter,
PriceNote: req.PriceNote,
WarehouseLocation: req.WarehouseLocation,
})
if err != nil {
response.InternalError(c, err.Error())
return
}
h.wsHub.BroadcastToCompany(companyID, &ws.Event{
Table: "inventory_records",
Action: "INSERT",
})
c.JSON(http.StatusCreated, response.Response{
Code: 0,
Message: "success",
Data: rec,
})
}
func (h *InventoryHandler) List(c *gin.Context) {
planID := c.Param("id")
records, err := h.svc.ListByPlan(c.Request.Context(), planID)
if err != nil {
response.InternalError(c, err.Error())
return
}
response.OK(c, records)
}