179 lines
4.8 KiB
Go
179 lines
4.8 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/guxuan/hailin_service/internal/mods/rbac/biz"
|
|
"github.com/guxuan/hailin_service/internal/mods/rbac/schema"
|
|
"github.com/guxuan/hailin_service/pkg/util"
|
|
)
|
|
|
|
type Product struct {
|
|
ProductBIZ *biz.Product
|
|
}
|
|
|
|
// @Tags 产品模块
|
|
// @Security ApiKeyAuth
|
|
// @Summary Query Product list
|
|
// @Param current query int true "pagination index" default(1)
|
|
// @Param pageSize query int true "pagination size" default(10)
|
|
// @Param title query string false "title of Product"
|
|
// @Param categoryId query int false "categoryId of Product"
|
|
// @Param code query string false "code of Product"
|
|
// @Param status query string false "Status of Product (disabled,enabled)"
|
|
// @Success 200 {object} util.ResponseResult{data=[]schema.Product}
|
|
// @Failure 401 {object} util.ResponseResult
|
|
// @Failure 500 {object} util.ResponseResult
|
|
// @Router /api/v1/products [get]
|
|
func (a *Product) Query(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
var params schema.ProductQueryParam
|
|
if err := util.ParseQuery(c, ¶ms); err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
|
|
result, err := a.ProductBIZ.Query(ctx, params)
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
util.ResPage(c, result.Data, result.PageResult)
|
|
}
|
|
|
|
type Option struct {
|
|
Value interface{} `json:"value"`
|
|
Label string `json:"label"`
|
|
Children []Option `json:"children"`
|
|
}
|
|
|
|
func ToOptions(categories *[]schema.ProductCategory) []Option {
|
|
if categories == nil {
|
|
return []Option{}
|
|
}
|
|
optMap := make(map[uint]*Option, len(*categories))
|
|
for _, cat := range *categories {
|
|
optMap[cat.ID] = &Option{
|
|
Value: cat.ID,
|
|
Label: cat.Label,
|
|
Children: []Option{},
|
|
}
|
|
}
|
|
var roots []Option
|
|
for _, cat := range *categories {
|
|
node := optMap[cat.ID]
|
|
if cat.ParentID == 0 {
|
|
roots = append(roots, *node)
|
|
} else if parent, ok := optMap[cat.ParentID]; ok {
|
|
parent.Children = append(parent.Children, *node)
|
|
}
|
|
}
|
|
|
|
return roots
|
|
}
|
|
|
|
// @Tags 产品模块
|
|
// @Security ApiKeyAuth
|
|
// @Summary Query ProductCategory list
|
|
// @Success 200 {object} util.ResponseResult{data=[]schema.ProductCategory}
|
|
// @Failure 401 {object} util.ResponseResult
|
|
// @Failure 500 {object} util.ResponseResult
|
|
// @Router /api/v1/products/categorys [get]
|
|
func (a *Product) QueryCategory(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
|
|
result, err := a.ProductBIZ.QueryProductCategory(ctx)
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
list := ToOptions(result)
|
|
util.ResSuccess(c, list)
|
|
}
|
|
|
|
// @Tags 产品模块
|
|
// @Security ApiKeyAuth
|
|
// @Summary Get Product record by ID
|
|
// @Param id path string true "unique id"
|
|
// @Success 200 {object} util.ResponseResult{data=schema.Product}
|
|
// @Failure 401 {object} util.ResponseResult
|
|
// @Failure 500 {object} util.ResponseResult
|
|
// @Router /api/v1/products/{id} [get]
|
|
func (a *Product) Get(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
item, err := a.ProductBIZ.Get(ctx, c.Param("id"))
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
util.ResSuccess(c, item)
|
|
}
|
|
|
|
// @Tags 产品模块
|
|
// @Security ApiKeyAuth
|
|
// @Summary Create Product record
|
|
// @Param body body schema.ProductForm true "Request body"
|
|
// @Success 200 {object} util.ResponseResult{data=schema.Product}
|
|
// @Failure 400 {object} util.ResponseResult
|
|
// @Failure 401 {object} util.ResponseResult
|
|
// @Failure 500 {object} util.ResponseResult
|
|
// @Router /api/v1/products [post]
|
|
func (a *Product) Create(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
item := new(schema.ProductForm)
|
|
if err := util.ParseJSON(c, item); err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
|
|
result, err := a.ProductBIZ.Create(ctx, item)
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
util.ResSuccess(c, result)
|
|
}
|
|
|
|
// @Tags 产品模块
|
|
// @Security ApiKeyAuth
|
|
// @Summary Update Product record by ID
|
|
// @Param id path string true "unique id"
|
|
// @Param body body schema.ProductForm true "Request body"
|
|
// @Success 200 {object} util.ResponseResult
|
|
// @Failure 400 {object} util.ResponseResult
|
|
// @Failure 401 {object} util.ResponseResult
|
|
// @Failure 500 {object} util.ResponseResult
|
|
// @Router /api/v1/products/{id} [put]
|
|
func (a *Product) Update(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
item := new(schema.ProductForm)
|
|
if err := util.ParseJSON(c, item); err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
|
|
err := a.ProductBIZ.Update(ctx, c.Param("id"), item)
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
util.ResOK(c)
|
|
}
|
|
|
|
// @Tags 产品模块
|
|
// @Security ApiKeyAuth
|
|
// @Summary Delete Product record by ID
|
|
// @Param id path string true "unique id"
|
|
// @Success 200 {object} util.ResponseResult
|
|
// @Failure 401 {object} util.ResponseResult
|
|
// @Failure 500 {object} util.ResponseResult
|
|
// @Router /api/v1/products/{id} [delete]
|
|
func (a *Product) Delete(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
err := a.ProductBIZ.Delete(ctx, c.Param("id"))
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
util.ResOK(c)
|
|
}
|