iloom/purchaser-service/internal/handler/accounts_payable.go

63 lines
1.3 KiB
Go
Raw Normal View History

package handler
import (
"log"
"github.com/gin-gonic/gin"
"github.com/muyuqingfeng/iloom/purchaser-service/internal/service"
"github.com/muyuqingfeng/iloom/shared/pkg/response"
)
type APHandler struct {
svc *service.APService
}
func NewAPHandler(svc *service.APService) *APHandler {
return &APHandler{svc: svc}
}
type PayRequest struct {
Amount float64 `json:"amount" binding:"required,gt=0"`
}
func (h *APHandler) List(c *gin.Context) {
companyID := c.GetHeader("X-Company-ID")
if companyID == "" {
response.BadRequest(c, "missing company id")
return
}
list, err := h.svc.List(c.Request.Context(), companyID)
if err != nil {
log.Printf("list accounts payable error: %v", err)
response.InternalError(c, "failed to list accounts payable")
return
}
response.OK(c, list)
}
func (h *APHandler) Pay(c *gin.Context) {
companyID := c.GetHeader("X-Company-ID")
if companyID == "" {
response.BadRequest(c, "missing company id")
return
}
apID := c.Param("id")
var req PayRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.BadRequest(c, err.Error())
return
}
if err := h.svc.Pay(c.Request.Context(), apID, companyID, req.Amount); err != nil {
log.Printf("pay error: %v", err)
response.InternalError(c, "failed to process payment")
return
}
response.OK(c, nil)
}