132 lines
3.5 KiB
Go
132 lines
3.5 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"gitlab.guxuan.icu/jinshan_community/internal/mods/product/biz"
|
|
"gitlab.guxuan.icu/jinshan_community/internal/mods/product/schema"
|
|
"gitlab.guxuan.icu/jinshan_community/pkg/util"
|
|
)
|
|
|
|
// Defining the `Order` api.
|
|
type Order struct {
|
|
OrderBIZ *biz.Order
|
|
}
|
|
|
|
// @Tags OrderAPI
|
|
// @Security ApiKeyAuth
|
|
// @Summary Query order 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.Order}
|
|
// @Failure 401 {object} util.ResponseResult
|
|
// @Failure 500 {object} util.ResponseResult
|
|
// @Router /api/v1/orders [get]
|
|
func (a *Order) Query(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
var params schema.OrderQueryParam
|
|
if err := util.ParseQuery(c, ¶ms); err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
|
|
result, err := a.OrderBIZ.Query(ctx, params)
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
util.ResPage(c, result.Data, result.PageResult)
|
|
}
|
|
|
|
// @Tags OrderAPI
|
|
// @Security ApiKeyAuth
|
|
// @Summary Get order record by ID
|
|
// @Param id path string true "unique id"
|
|
// @Success 200 {object} util.ResponseResult{data=schema.Order}
|
|
// @Failure 401 {object} util.ResponseResult
|
|
// @Failure 500 {object} util.ResponseResult
|
|
// @Router /api/v1/orders/{id} [get]
|
|
func (a *Order) Get(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
item, err := a.OrderBIZ.Get(ctx, c.Param("id"))
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
util.ResSuccess(c, item)
|
|
}
|
|
|
|
// @Tags OrderAPI
|
|
// @Security ApiKeyAuth
|
|
// @Summary Create order record
|
|
// @Param body body schema.OrderForm true "Request body"
|
|
// @Success 200 {object} util.ResponseResult{data=schema.Order}
|
|
// @Failure 400 {object} util.ResponseResult
|
|
// @Failure 401 {object} util.ResponseResult
|
|
// @Failure 500 {object} util.ResponseResult
|
|
// @Router /api/v1/orders [post]
|
|
func (a *Order) Create(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
item := new(schema.OrderForm)
|
|
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.OrderBIZ.Create(ctx, item)
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
util.ResSuccess(c, result)
|
|
}
|
|
|
|
// @Tags OrderAPI
|
|
// @Security ApiKeyAuth
|
|
// @Summary Update order record by ID
|
|
// @Param id path string true "unique id"
|
|
// @Param body body schema.OrderForm 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/orders/{id} [put]
|
|
func (a *Order) Update(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
item := new(schema.OrderForm)
|
|
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
|
|
}
|
|
|
|
err := a.OrderBIZ.Update(ctx, c.Param("id"), item)
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
util.ResOK(c)
|
|
}
|
|
|
|
// @Tags OrderAPI
|
|
// @Security ApiKeyAuth
|
|
// @Summary Delete order 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/orders/{id} [delete]
|
|
func (a *Order) Delete(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
err := a.OrderBIZ.Delete(ctx, c.Param("id"))
|
|
if err != nil {
|
|
util.ResError(c, err)
|
|
return
|
|
}
|
|
util.ResOK(c)
|
|
}
|