package backend import ( "github.com/ahmetb/go-linq/v3" "github.com/gin-gonic/gin" "github.com/qit-team/snow-core/log/logger" "qteam/app/constants/errorcode" "qteam/app/http/controllers" "qteam/app/http/domains" "qteam/app/http/entities/backend" "qteam/app/services" ) func ProductCreateHandler(c *gin.Context) { req, exists := c.Get("request") if !exists { controllers.Error(c, errorcode.ParamError) logger.Error(c.Request.Context(), "req不存在") return } in, ok := req.(*backend.ProductCreateRequest) if !ok { controllers.Error(c, errorcode.ParamError) logger.Error(c.Request.Context(), "req不匹配") return } do, _ := in.ToDomain() if err := services.ProductCreate(do); err != nil { controllers.Error(c, errorcode.SystemError) logger.Error(c.Request.Context(), err.Error()) } else { controllers.Success(c, "", "product create success") } } func ProductUpdateHandler(c *gin.Context) { req, exists := c.Get("request") if !exists { controllers.Error(c, errorcode.ParamError) logger.Error(c.Request.Context(), "req不存在") return } in, ok := req.(backend.ProductCreateRequest) if !ok { controllers.Error(c, errorcode.ParamError) logger.Error(c.Request.Context(), "req不匹配") return } do, _ := in.ToDomain() if err := services.ProductUpdate(do); err != nil { controllers.Error(c, errorcode.SystemError) logger.Error(c.Request.Context(), err.Error()) } else { controllers.Success(c, errorcode.Success, "product create success") } } func ProductDeleteHandler(c *gin.Context) { req, exists := c.Get("request") if !exists { controllers.Error(c, errorcode.ParamError) logger.Error(c.Request.Context(), "req不存在") return } in, ok := req.(backend.ProductCreateRequest) if !ok { controllers.Error(c, errorcode.ParamError) logger.Error(c.Request.Context(), "req不匹配") return } do, _ := in.ToDomain() if err := services.ProductDelete(do); err != nil { controllers.Error(c, errorcode.SystemError) logger.Error(c.Request.Context(), err.Error()) } else { controllers.Success(c, errorcode.Success, "product create success") } } func ProductQueryHandler(c *gin.Context) { //page := c.Param("page") params := domains.ProductFilter{} err := c.Bind(¶ms) if err != nil { controllers.Error(c, errorcode.ParamError) logger.Error(c.Request.Context(), err.Error()) } data, count, err := services.ProductQuery(params) rsp := make([]backend.ProductCreateResponse, len(data)) linq.From(data).SelectT(func(do domains.Product) (rs backend.ProductCreateResponse) { rs.FromDomain(do) return rs }).ToSlice(&rsp) if err != nil { controllers.Error(c, errorcode.SystemError) logger.Error(c.Request.Context(), err.Error()) } else { controllers.Success(c, gin.H{"data": rsp, "count": count}, "") } }