146 lines
3.8 KiB
Go
146 lines
3.8 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.guxuan/haibei/internal/mods/product/biz"
|
|
"github.guxuan/haibei/internal/mods/product/schema"
|
|
"github.guxuan/haibei/pkg/util"
|
|
)
|
|
|
|
// Defining the `Product` api.
|
|
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)
|
|
// @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)
|
|
}
|
|
|
|
// @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()
|
|
id, err := util.GetQueryID(c)
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
item, err := a.ProductBIZ.Get(ctx, 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
|
|
} else if err := item.Validate(); 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
|
|
} else if err := item.Validate(); err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
id, err := util.GetQueryID(c)
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
err = a.ProductBIZ.Update(ctx, 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()
|
|
id, err := util.GetQueryID(c)
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
err = a.ProductBIZ.Delete(ctx, id)
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
util.ResOK(c)
|
|
}
|