Files
goauto/server/app/goauto/product/handler.go
T

203 lines
5.2 KiB
Go

package product
import (
"encoding/json"
"errors"
"io"
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/sdk/pkg"
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
"gorm.io/gorm"
)
type Handler struct{ DB *gorm.DB }
func (handler Handler) List(c *gin.Context) {
page, err := queryInt(c.Query("page"), 1)
if err != nil {
writeError(c, invalidRequest("page 必须是正整数"))
return
}
pageSize, err := queryInt(c.Query("pageSize"), 20)
if err != nil {
writeError(c, invalidRequest("pageSize 必须是正整数"))
return
}
service, ok := handler.service(c)
if !ok {
return
}
request := ListRequest{
Page: page, PageSize: pageSize, GoodsID: c.Query("goodsId"), Keyword: c.Query("keyword"), Status: strings.TrimSpace(c.Query("status")),
}
claims := jwt.ExtractClaims(c)
if role, _ := claims["rolekey"].(string); role != "admin" {
id, _ := claims["identity"].(float64)
owner := uint64(id)
request.CollectionOwnerUserID = &owner
}
response, err := service.List(c.Request.Context(), request)
if err != nil {
writeError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"code": 200, "data": response})
}
func (handler Handler) Create(c *gin.Context) {
var request SaveRequest
if err := decodeJSON(c, &request); err != nil {
writeError(c, invalidRequest("请求 JSON 无效"))
return
}
service, ok := handler.service(c)
if !ok {
return
}
response, err := service.Create(c.Request.Context(), request)
if err != nil {
writeError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"code": 200, "data": response})
}
func (handler Handler) Update(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("productId"), 10, 64)
if err != nil || id == 0 {
writeError(c, invalidRequest("productId 无效"))
return
}
var request UpdateRequest
if err := decodeJSON(c, &request); err != nil {
writeError(c, invalidRequest("请求 JSON 无效"))
return
}
service, ok := handler.service(c)
if !ok {
return
}
response, err := service.Update(c.Request.Context(), id, request)
if err != nil {
writeError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"code": 200, "data": response})
}
func (handler Handler) Detail(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("productId"), 10, 64)
if err != nil || id == 0 {
writeError(c, invalidRequest("productId 无效"))
return
}
service, ok := handler.service(c)
if !ok {
return
}
response, err := service.Detail(c.Request.Context(), id)
if err != nil {
writeError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"code": 200, "data": response})
}
func (handler Handler) RelatedSYBProducts(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("productId"), 10, 64)
if err != nil || id == 0 {
writeError(c, invalidRequest("productId 无效"))
return
}
page, err := queryInt(c.Query("page"), 1)
if err != nil {
writeError(c, invalidRequest("page 必须是正整数"))
return
}
pageSize, err := queryInt(c.Query("pageSize"), 20)
if err != nil {
writeError(c, invalidRequest("pageSize 必须是正整数"))
return
}
var shopeeProductID *uint64
if raw := strings.TrimSpace(c.Query("shopeeProductId")); raw != "" {
parsed, parseErr := strconv.ParseUint(raw, 10, 64)
if parseErr != nil || parsed == 0 {
writeError(c, invalidRequest("shopeeProductId 无效"))
return
}
shopeeProductID = &parsed
}
service, ok := handler.service(c)
if !ok {
return
}
response, err := service.RelatedSYBProducts(c.Request.Context(), id, RelatedSYBRequest{Page: page, PageSize: pageSize, Scope: c.Query("scope"), ShopeeProductID: shopeeProductID})
if err != nil {
writeError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"code": 200, "data": response})
}
func (handler Handler) service(c *gin.Context) (*Service, bool) {
db := handler.DB
var err error
if db == nil {
db, err = pkg.GetOrm(c)
}
if err != nil {
writeError(c, internalError(err))
return nil, false
}
return NewService(db), true
}
func decodeJSON(c *gin.Context, request any) error {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 1<<20)
decoder := json.NewDecoder(c.Request.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(request); err != nil {
return err
}
if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
return errors.New("one object required")
}
return nil
}
func queryInt(value string, fallback int) (int, error) {
if strings.TrimSpace(value) == "" {
return fallback, nil
}
parsed, err := strconv.Atoi(value)
if err != nil || parsed < 1 {
return 0, errors.New("invalid integer")
}
return parsed, nil
}
func writeError(c *gin.Context, err error) {
var target *ServiceError
if !errors.As(err, &target) {
target = internalError(err).(*ServiceError)
}
status := http.StatusInternalServerError
switch target.Code {
case CodeInvalidRequest, CodeGoodsIDInvalid:
status = http.StatusUnprocessableEntity
case CodeProductExists:
status = http.StatusConflict
case CodeProductNotFound:
status = http.StatusNotFound
}
response := gin.H{"code": target.Code, "message": target.Message, "retryable": target.Retryable}
if target.ExistingProductID > 0 {
response["existingProductId"] = target.ExistingProductID
}
c.JSON(status, response)
}