189 lines
5.6 KiB
Go
189 lines
5.6 KiB
Go
package category
|
||
|
||
import (
|
||
"git.echol.cn/loser/lckt/global"
|
||
"git.echol.cn/loser/lckt/model/category"
|
||
categoryReq "git.echol.cn/loser/lckt/model/category/request"
|
||
"git.echol.cn/loser/lckt/model/common/response"
|
||
"github.com/gin-gonic/gin"
|
||
"go.uber.org/zap"
|
||
)
|
||
|
||
type CategoryApi struct{}
|
||
|
||
// CreateCategory 创建类别
|
||
// @Tags Category
|
||
// @Summary 创建类别
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param data body category.Category true "创建类别"
|
||
// @Success 200 {object} response.Response{msg=string} "创建成功"
|
||
// @Router /cat/createCategory [post]
|
||
func (catApi *CategoryApi) CreateCategory(c *gin.Context) {
|
||
// 创建业务用Context
|
||
ctx := c.Request.Context()
|
||
|
||
var cat category.Category
|
||
err := c.ShouldBindJSON(&cat)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
err = catService.CreateCategory(ctx, &cat)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("创建失败!", zap.Error(err))
|
||
response.FailWithMessage("创建失败:"+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithMessage("创建成功", c)
|
||
}
|
||
|
||
// DeleteCategory 删除类别
|
||
// @Tags Category
|
||
// @Summary 删除类别
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param data body category.Category true "删除类别"
|
||
// @Success 200 {object} response.Response{msg=string} "删除成功"
|
||
// @Router /cat/deleteCategory [delete]
|
||
func (catApi *CategoryApi) DeleteCategory(c *gin.Context) {
|
||
// 创建业务用Context
|
||
ctx := c.Request.Context()
|
||
|
||
ID := c.Query("ID")
|
||
err := catService.DeleteCategory(ctx, ID)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("删除失败!", zap.Error(err))
|
||
response.FailWithMessage("删除失败:"+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithMessage("删除成功", c)
|
||
}
|
||
|
||
// DeleteCategoryByIds 批量删除类别
|
||
// @Tags Category
|
||
// @Summary 批量删除类别
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
|
||
// @Router /cat/deleteCategoryByIds [delete]
|
||
func (catApi *CategoryApi) DeleteCategoryByIds(c *gin.Context) {
|
||
// 创建业务用Context
|
||
ctx := c.Request.Context()
|
||
|
||
IDs := c.QueryArray("IDs[]")
|
||
err := catService.DeleteCategoryByIds(ctx, IDs)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
|
||
response.FailWithMessage("批量删除失败:"+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithMessage("批量删除成功", c)
|
||
}
|
||
|
||
// UpdateCategory 更新类别
|
||
// @Tags Category
|
||
// @Summary 更新类别
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param data body category.Category true "更新类别"
|
||
// @Success 200 {object} response.Response{msg=string} "更新成功"
|
||
// @Router /cat/updateCategory [put]
|
||
func (catApi *CategoryApi) UpdateCategory(c *gin.Context) {
|
||
// 从ctx获取标准context进行业务行为
|
||
ctx := c.Request.Context()
|
||
|
||
var cat category.Category
|
||
err := c.ShouldBindJSON(&cat)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
err = catService.UpdateCategory(ctx, cat)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("更新失败!", zap.Error(err))
|
||
response.FailWithMessage("更新失败:"+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithMessage("更新成功", c)
|
||
}
|
||
|
||
// FindCategory 用id查询类别
|
||
// @Tags Category
|
||
// @Summary 用id查询类别
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param ID query uint true "用id查询类别"
|
||
// @Success 200 {object} response.Response{data=category.Category,msg=string} "查询成功"
|
||
// @Router /cat/findCategory [get]
|
||
func (catApi *CategoryApi) FindCategory(c *gin.Context) {
|
||
// 创建业务用Context
|
||
ctx := c.Request.Context()
|
||
|
||
ID := c.Query("ID")
|
||
recat, err := catService.GetCategory(ctx, ID)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("查询失败!", zap.Error(err))
|
||
response.FailWithMessage("查询失败:"+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithData(recat, c)
|
||
}
|
||
|
||
// GetCategoryList 分页获取类别列表
|
||
// @Tags Category
|
||
// @Summary 分页获取类别列表
|
||
// @Security ApiKeyAuth
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Param data query categoryReq.CategorySearch true "分页获取类别列表"
|
||
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
|
||
// @Router /cat/getCategoryList [get]
|
||
func (catApi *CategoryApi) GetCategoryList(c *gin.Context) {
|
||
// 创建业务用Context
|
||
ctx := c.Request.Context()
|
||
|
||
var pageInfo categoryReq.CategorySearch
|
||
err := c.ShouldBindQuery(&pageInfo)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
list, total, err := catService.GetCategoryInfoList(ctx, pageInfo)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
||
response.FailWithMessage("获取失败:"+err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithDetailed(response.PageResult{
|
||
List: list,
|
||
Total: total,
|
||
Page: pageInfo.Page,
|
||
PageSize: pageInfo.PageSize,
|
||
}, "获取成功", c)
|
||
}
|
||
|
||
// GetCategoryPublic 不需要鉴权的类别接口
|
||
// @Tags Category
|
||
// @Summary 不需要鉴权的类别接口
|
||
// @Accept application/json
|
||
// @Produce application/json
|
||
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
|
||
// @Router /cat/getCategoryPublic [get]
|
||
func (catApi *CategoryApi) GetCategoryPublic(c *gin.Context) {
|
||
// 创建业务用Context
|
||
ctx := c.Request.Context()
|
||
|
||
// 此接口不需要鉴权
|
||
// 示例为返回了一个固定的消息接口,一般本接口用于C端服务,需要自己实现业务逻辑
|
||
catService.GetCategoryPublic(ctx)
|
||
response.OkWithDetailed(gin.H{
|
||
"info": "不需要鉴权的类别接口信息",
|
||
}, "获取成功", c)
|
||
}
|