Go workspace (go.work) with 5 microservices + shared library: - gateway (8080), auth-service (8081), purchaser-service (8082) - textile-service (8083), washing-service (8084) - shared: proto definitions, common packages Infrastructure: docker-compose for local dev, K8s manifests for K3s cluster deployment (mysql/redis/etcd + traefik ingress). Frontend (iloom-flatten) added as git submodule. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/muyuqingfeng/iloom/shared/pkg/response"
|
|
"github.com/muyuqingfeng/iloom/shared/pkg/websocket"
|
|
"github.com/muyuqingfeng/iloom/washing-service/internal/model"
|
|
"github.com/muyuqingfeng/iloom/washing-service/internal/service"
|
|
)
|
|
|
|
type FinishedProductHandler struct {
|
|
svc *service.FinishedProductService
|
|
wsHub *websocket.Hub
|
|
}
|
|
|
|
func NewFinishedProductHandler(svc *service.FinishedProductService, wsHub *websocket.Hub) *FinishedProductHandler {
|
|
return &FinishedProductHandler{svc: svc, wsHub: wsHub}
|
|
}
|
|
|
|
func (h *FinishedProductHandler) List(c *gin.Context) {
|
|
companyID := c.GetHeader("X-Company-ID")
|
|
if companyID == "" {
|
|
response.BadRequest(c, "missing company id")
|
|
return
|
|
}
|
|
|
|
products, err := h.svc.List(c.Request.Context(), companyID)
|
|
if err != nil {
|
|
log.Printf("list finished products error: %v", err)
|
|
response.InternalError(c, "failed to list finished products")
|
|
return
|
|
}
|
|
|
|
response.OK(c, products)
|
|
}
|
|
|
|
func (h *FinishedProductHandler) Create(c *gin.Context) {
|
|
companyID := c.GetHeader("X-Company-ID")
|
|
userID := c.GetHeader("X-User-ID")
|
|
if companyID == "" {
|
|
response.BadRequest(c, "missing company id")
|
|
return
|
|
}
|
|
|
|
var fp model.FinishedProduct
|
|
if err := c.ShouldBindJSON(&fp); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
|
|
if err := h.svc.Create(c.Request.Context(), companyID, userID, &fp); err != nil {
|
|
log.Printf("create finished product error: %v", err)
|
|
response.InternalError(c, "failed to create finished product")
|
|
return
|
|
}
|
|
|
|
h.wsHub.BroadcastToCompany(companyID, &websocket.Event{
|
|
Table: "finished_products",
|
|
Action: "INSERT",
|
|
})
|
|
|
|
c.JSON(http.StatusCreated, response.Response{
|
|
Code: 0,
|
|
Message: "success",
|
|
Data: fp,
|
|
})
|
|
}
|
|
|
|
type OutboundReq struct {
|
|
Rolls int `json:"rolls" binding:"required,gt=0"`
|
|
Meters float64 `json:"meters" binding:"required,gt=0"`
|
|
Notes string `json:"notes"`
|
|
}
|
|
|
|
func (h *FinishedProductHandler) Outbound(c *gin.Context) {
|
|
companyID := c.GetHeader("X-Company-ID")
|
|
userID := c.GetHeader("X-User-ID")
|
|
if companyID == "" {
|
|
response.BadRequest(c, "missing company id")
|
|
return
|
|
}
|
|
|
|
fpID := c.Param("id")
|
|
|
|
var req OutboundReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
|
|
if err := h.svc.Outbound(c.Request.Context(), fpID, companyID, userID, req.Rolls, req.Meters, req.Notes); err != nil {
|
|
log.Printf("outbound finished product error: %v", err)
|
|
response.InternalError(c, "failed to outbound finished product")
|
|
return
|
|
}
|
|
|
|
h.wsHub.BroadcastToCompany(companyID, &websocket.Event{
|
|
Table: "finished_products",
|
|
Action: "UPDATE",
|
|
})
|
|
|
|
response.OK(c, nil)
|
|
}
|