Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 25s
feat(admin): 新增工会管理功能 feat(activity): 添加活动管理相关服务 feat(user): 实现用户道具卡和积分管理 feat(guild): 新增工会成员管理功能 fix: 修复数据库连接配置 fix: 修正jwtoken导入路径 fix: 解决端口冲突问题 style: 统一代码格式和注释风格 style: 更新项目常量命名 docs: 添加项目框架和开发规范文档 docs: 更新接口文档注释 chore: 移除无用代码和文件 chore: 更新Makefile和配置文件 chore: 清理日志文件 test: 添加道具卡测试脚本
266 lines
6.3 KiB
Smarty
266 lines
6.3 KiB
Smarty
package {{.PackageName}}
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"bindbox-game/internal/code"
|
|
"bindbox-game/internal/pkg/core"
|
|
"WeChatService/internal/pkg/logger"
|
|
"bindbox-game/internal/repository/mysql"
|
|
"bindbox-game/internal/repository/mysql/dao"
|
|
"bindbox-game/internal/repository/mysql/model"
|
|
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type handler struct {
|
|
logger logger.CustomLoggerLogger
|
|
writeDB *dao.Query
|
|
readDB *dao.Query
|
|
}
|
|
|
|
type genResultInfo struct {
|
|
RowsAffected int64 `json:"rows_affected"`
|
|
Error error `json:"error"`
|
|
}
|
|
|
|
func New(logger logger.CustomLogger, db mysql.Repo) *handler {
|
|
return &handler{
|
|
logger: logger,
|
|
writeDB: dao.Use(db.GetDbW()),
|
|
readDB: dao.Use(db.GetDbR()),
|
|
}
|
|
}
|
|
|
|
// Create 新增数据
|
|
// @Summary 新增数据
|
|
// @Description 新增数据
|
|
// @Tags API.{{.VariableName}}
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param RequestBody body model.{{.StructName}} true "请求参数"
|
|
// @Success 200 {object} model.{{.StructName}}
|
|
// @Failure 400 {object} code.Failure
|
|
// @Router /api/{{.VariableName}} [post]
|
|
func (h *handler) Create() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
var createData model.{{.StructName}}
|
|
if err := ctx.ShouldBindJSON(&createData); err != nil {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ParamBindError,
|
|
err.Error()),
|
|
)
|
|
return
|
|
}
|
|
|
|
if err := h.writeDB.{{.StructName}}.WithContext(ctx.RequestContext()).Create(&createData); err != nil {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ServerError,
|
|
err.Error()),
|
|
)
|
|
return
|
|
}
|
|
|
|
ctx.Payload(createData)
|
|
}
|
|
}
|
|
|
|
// List 获取列表数据
|
|
// @Summary 获取列表数据
|
|
// @Description 获取列表数据
|
|
// @Tags API.{{.VariableName}}
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} []model.{{.StructName}}
|
|
// @Failure 400 {object} code.Failure
|
|
// @Router /api/{{.VariableName}}s [get]
|
|
func (h *handler) List() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
list, err := h.readDB.{{.StructName}}.WithContext(ctx.RequestContext()).Find()
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ServerError,
|
|
err.Error()),
|
|
)
|
|
return
|
|
}
|
|
|
|
ctx.Payload(list)
|
|
}
|
|
}
|
|
|
|
// GetByID 根据 ID 获取数据
|
|
// @Summary 根据 ID 获取数据
|
|
// @Description 根据 ID 获取数据
|
|
// @Tags API.{{.VariableName}}
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "ID"
|
|
// @Success 200 {object} model.{{.StructName}}
|
|
// @Failure 400 {object} code.Failure
|
|
// @Router /api/{{.VariableName}}/{id} [get]
|
|
func (h *handler) GetByID() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, err := strconv.Atoi(ctx.Param("id"))
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ParamBindError,
|
|
err.Error()),
|
|
)
|
|
return
|
|
}
|
|
|
|
info, err := h.readDB.{{.StructName}}.WithContext(ctx.RequestContext()).Where(h.readDB.{{.StructName}}.ID.Eq(int32(id))).First()
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ServerError,
|
|
"record not found"),
|
|
)
|
|
} else {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ServerError,
|
|
err.Error()),
|
|
)
|
|
}
|
|
return
|
|
}
|
|
|
|
ctx.Payload(info)
|
|
}
|
|
}
|
|
|
|
// DeleteByID 根据 ID 删除数据
|
|
// @Summary 根据 ID 删除数据
|
|
// @Description 根据 ID 删除数据
|
|
// @Tags API.{{.VariableName}}
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "ID"
|
|
// @Success 200 {object} genResultInfo
|
|
// @Failure 400 {object} code.Failure
|
|
// @Router /api/{{.VariableName}}/{id} [delete]
|
|
func (h *handler) DeleteByID() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, err := strconv.Atoi(ctx.Param("id"))
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ParamBindError,
|
|
err.Error()),
|
|
)
|
|
return
|
|
}
|
|
|
|
info, err := h.readDB.{{.StructName}}.WithContext(ctx.RequestContext()).Where(h.readDB.{{.StructName}}.ID.Eq(int32(id))).First()
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ServerError,
|
|
"record not found"),
|
|
)
|
|
} else {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ServerError,
|
|
err.Error()),
|
|
)
|
|
}
|
|
return
|
|
}
|
|
|
|
result, err := h.writeDB.{{.StructName}}.Delete(info)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ServerError,
|
|
err.Error()),
|
|
)
|
|
}
|
|
|
|
resultInfo := new(genResultInfo)
|
|
resultInfo.RowsAffected = result.RowsAffected
|
|
resultInfo.Error = result.Error
|
|
|
|
ctx.Payload(resultInfo)
|
|
}
|
|
}
|
|
|
|
// UpdateByID 根据 ID 更新数据
|
|
// @Summary 根据 ID 更新数据
|
|
// @Description 根据 ID 更新数据
|
|
// @Tags API.{{.VariableName}}
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "ID"
|
|
// @Param RequestBody body model.{{.StructName}} true "请求参数"
|
|
// @Success 200 {object} genResultInfo
|
|
// @Failure 400 {object} code.Failure
|
|
// @Router /api/{{.VariableName}}/{id} [put]
|
|
func (h *handler) UpdateByID() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, err := strconv.Atoi(ctx.Param("id"))
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ParamBindError,
|
|
err.Error()),
|
|
)
|
|
return
|
|
}
|
|
|
|
var updateData map[string]interface{}
|
|
if err := ctx.ShouldBindJSON(&updateData); err != nil {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ServerError,
|
|
err.Error()),
|
|
)
|
|
return
|
|
}
|
|
|
|
info, err := h.readDB.{{.StructName}}.WithContext(ctx.RequestContext()).Where(h.readDB.{{.StructName}}.ID.Eq(int32(id))).First()
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ServerError,
|
|
"record not found"),
|
|
)
|
|
} else {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ServerError,
|
|
err.Error()),
|
|
)
|
|
}
|
|
return
|
|
}
|
|
|
|
result, err := h.writeDB.{{.StructName}}.WithContext(ctx.RequestContext()).Where(h.writeDB.{{.StructName}}.ID.Eq(info.ID)).Updates(updateData)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(
|
|
http.StatusBadRequest,
|
|
code.ServerError,
|
|
err.Error()),
|
|
)
|
|
return
|
|
}
|
|
|
|
resultInfo := new(genResultInfo)
|
|
resultInfo.RowsAffected = result.RowsAffected
|
|
resultInfo.Error = result.Error
|
|
|
|
ctx.Payload(resultInfo)
|
|
}
|
|
}
|