service/codecreate/template/controller.txt

72 lines
1.9 KiB
Plaintext
Raw Permalink Normal View History

2023-12-21 22:17:40 +08:00
package controller
import (
"github.com/gin-gonic/gin"
"service/api/errcode"
#{moduleName}proto "service/api/proto/#{moduleName}/proto"
"service/app/mix/service"
"service/bizcommon/util"
"service/library/logger"
)
func OpCreate#{EntityName}(ctx *gin.Context) {
req := ctx.MustGet("client_req").(*#{moduleName}proto.OpCreateReq)
ec := service.DefaultService.OpCreate#{EntityName}(ctx, req)
if ec != errcode.ErrCode#{EntityName}SrvOk {
logger.Error("OpCreate#{EntityName} fail, req: %v, ec: %v", util.ToJson(req), ec)
ReplyErrorMsg(ctx, "server error")
return
}
ReplyOk(ctx, nil)
}
func OpUpdate#{EntityName}(ctx *gin.Context) {
req := ctx.MustGet("client_req").(*#{moduleName}proto.OpUpdateReq)
ec := service.DefaultService.OpUpdate#{EntityName}(ctx, req)
if ec != errcode.ErrCode#{EntityName}SrvOk {
logger.Error("OpUpdate#{EntityName} fail, req: %v, ec: %v", util.ToJson(req), ec)
ReplyErrCodeMsg(ctx, ec)
return
}
ReplyOk(ctx, nil)
}
func OpDelete#{EntityName}(ctx *gin.Context) {
req := ctx.MustGet("client_req").(*#{moduleName}proto.OpDeleteReq)
ec := service.DefaultService.OpDelete#{EntityName}(ctx, req.Id)
if ec != errcode.ErrCode#{EntityName}SrvOk {
logger.Error("OpDelete#{EntityName} fail, req: %v, ec: %v", util.ToJson(req), ec)
ReplyErrCodeMsg(ctx, ec)
return
}
ReplyOk(ctx, nil)
}
func OpGet#{EntityName}List(ctx *gin.Context) {
req := ctx.MustGet("client_req").(*#{moduleName}proto.OpListReq)
//设置默认页长
if req.Limit == 0 {
req.Limit = consts.DefaultPageSize
}
list, ec := service.DefaultService.OpGet#{EntityName}List(ctx, req)
if ec != errcode.ErrCode#{EntityName}SrvOk {
logger.Error("OpGet#{EntityName}List fail, req: %v, ec: %v", util.ToJson(req), ec)
ReplyErrCodeMsg(ctx, ec)
return
}
data := &#{moduleName}proto.OpListData{
List: list,
Offset: req.Offset + len(list),
}
if len(list) >= req.Limit {
data.More = 1
}
ReplyOk(ctx, data)
}