mirror of https://github.com/zhufuyi/sponge
274 lines
7.7 KiB
Go
274 lines
7.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/zhufuyi/sponge/pkg/gin/middleware"
|
|
"github.com/zhufuyi/sponge/pkg/gin/response"
|
|
"github.com/zhufuyi/sponge/pkg/logger"
|
|
"github.com/zhufuyi/sponge/pkg/utils"
|
|
|
|
"github.com/zhufuyi/sponge/internal/cache"
|
|
"github.com/zhufuyi/sponge/internal/dao"
|
|
"github.com/zhufuyi/sponge/internal/ecode"
|
|
"github.com/zhufuyi/sponge/internal/model"
|
|
"github.com/zhufuyi/sponge/internal/types"
|
|
)
|
|
|
|
var _ UserExampleHandler = (*userExampleHandler)(nil)
|
|
|
|
// UserExampleHandler defining the handler interface
|
|
type UserExampleHandler interface {
|
|
Create(c *gin.Context)
|
|
DeleteByID(c *gin.Context)
|
|
UpdateByID(c *gin.Context)
|
|
GetByID(c *gin.Context)
|
|
List(c *gin.Context)
|
|
}
|
|
|
|
type userExampleHandler struct {
|
|
iDao dao.UserExampleDao
|
|
}
|
|
|
|
// NewUserExampleHandler creating the handler interface
|
|
func NewUserExampleHandler() UserExampleHandler {
|
|
return &userExampleHandler{
|
|
iDao: dao.NewUserExampleDao(
|
|
model.GetDB(),
|
|
cache.NewUserExampleCache(model.GetCacheType()),
|
|
),
|
|
}
|
|
}
|
|
|
|
// Create a record
|
|
// @Summary create userExample
|
|
// @Description submit information to create userExample
|
|
// @Tags userExample
|
|
// @accept json
|
|
// @Produce json
|
|
// @Param data body types.CreateUserExampleRequest true "userExample information"
|
|
// @Success 200 {object} types.CreateUserExampleRespond{}
|
|
// @Router /api/v1/userExample [post]
|
|
// @Security BearerAuth
|
|
func (h *userExampleHandler) Create(c *gin.Context) {
|
|
form := &types.CreateUserExampleRequest{}
|
|
err := c.ShouldBindJSON(form)
|
|
if err != nil {
|
|
logger.Warn("ShouldBindJSON error: ", logger.Err(err), middleware.GCtxRequestIDField(c))
|
|
response.Error(c, ecode.InvalidParams)
|
|
return
|
|
}
|
|
|
|
userExample := &model.UserExample{}
|
|
err = copier.Copy(userExample, form)
|
|
if err != nil {
|
|
response.Error(c, ecode.ErrCreateUserExample)
|
|
return
|
|
}
|
|
// Note: if copier.Copy cannot assign a value to a field, add it here
|
|
|
|
ctx := middleware.WrapCtx(c)
|
|
err = h.iDao.Create(ctx, userExample)
|
|
if err != nil {
|
|
logger.Error("Create error", logger.Err(err), logger.Any("form", form), middleware.GCtxRequestIDField(c))
|
|
response.Output(c, ecode.InternalServerError.ToHTTPCode())
|
|
return
|
|
}
|
|
|
|
response.Success(c, gin.H{"id": userExample.ID})
|
|
}
|
|
|
|
// DeleteByID delete a record by id
|
|
// @Summary delete userExample
|
|
// @Description delete userExample by id
|
|
// @Tags userExample
|
|
// @accept json
|
|
// @Produce json
|
|
// @Param id path string true "id"
|
|
// @Success 200 {object} types.DeleteUserExampleByIDRespond{}
|
|
// @Router /api/v1/userExample/{id} [delete]
|
|
// @Security BearerAuth
|
|
func (h *userExampleHandler) DeleteByID(c *gin.Context) {
|
|
_, id, isAbort := getUserExampleIDFromPath(c)
|
|
if isAbort {
|
|
response.Error(c, ecode.InvalidParams)
|
|
return
|
|
}
|
|
|
|
ctx := middleware.WrapCtx(c)
|
|
err := h.iDao.DeleteByID(ctx, id)
|
|
if err != nil {
|
|
logger.Error("DeleteByID error", logger.Err(err), logger.Any("id", id), middleware.GCtxRequestIDField(c))
|
|
response.Output(c, ecode.InternalServerError.ToHTTPCode())
|
|
return
|
|
}
|
|
|
|
response.Success(c)
|
|
}
|
|
|
|
// UpdateByID update information by id
|
|
// @Summary update userExample
|
|
// @Description update userExample information by id
|
|
// @Tags userExample
|
|
// @accept json
|
|
// @Produce json
|
|
// @Param id path string true "id"
|
|
// @Param data body types.UpdateUserExampleByIDRequest true "userExample information"
|
|
// @Success 200 {object} types.UpdateUserExampleByIDRespond{}
|
|
// @Router /api/v1/userExample/{id} [put]
|
|
// @Security BearerAuth
|
|
func (h *userExampleHandler) UpdateByID(c *gin.Context) {
|
|
_, id, isAbort := getUserExampleIDFromPath(c)
|
|
if isAbort {
|
|
response.Error(c, ecode.InvalidParams)
|
|
return
|
|
}
|
|
|
|
form := &types.UpdateUserExampleByIDRequest{}
|
|
err := c.ShouldBindJSON(form)
|
|
if err != nil {
|
|
logger.Warn("ShouldBindJSON error: ", logger.Err(err), middleware.GCtxRequestIDField(c))
|
|
response.Error(c, ecode.InvalidParams)
|
|
return
|
|
}
|
|
form.ID = id
|
|
|
|
userExample := &model.UserExample{}
|
|
err = copier.Copy(userExample, form)
|
|
if err != nil {
|
|
response.Error(c, ecode.ErrUpdateByIDUserExample)
|
|
return
|
|
}
|
|
// Note: if copier.Copy cannot assign a value to a field, add it here
|
|
|
|
ctx := middleware.WrapCtx(c)
|
|
err = h.iDao.UpdateByID(ctx, userExample)
|
|
if err != nil {
|
|
logger.Error("UpdateByID error", logger.Err(err), logger.Any("form", form), middleware.GCtxRequestIDField(c))
|
|
response.Output(c, ecode.InternalServerError.ToHTTPCode())
|
|
return
|
|
}
|
|
|
|
response.Success(c)
|
|
}
|
|
|
|
// GetByID get a record by id
|
|
// @Summary get userExample detail
|
|
// @Description get userExample detail by id
|
|
// @Tags userExample
|
|
// @Param id path string true "id"
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} types.GetUserExampleByIDRespond{}
|
|
// @Router /api/v1/userExample/{id} [get]
|
|
// @Security BearerAuth
|
|
func (h *userExampleHandler) GetByID(c *gin.Context) {
|
|
idStr, id, isAbort := getUserExampleIDFromPath(c)
|
|
if isAbort {
|
|
response.Error(c, ecode.InvalidParams)
|
|
return
|
|
}
|
|
|
|
ctx := middleware.WrapCtx(c)
|
|
userExample, err := h.iDao.GetByID(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, model.ErrRecordNotFound) {
|
|
logger.Warn("GetByID not found", logger.Err(err), logger.Any("id", id), middleware.GCtxRequestIDField(c))
|
|
response.Error(c, ecode.NotFound)
|
|
} else {
|
|
logger.Error("GetByID error", logger.Err(err), logger.Any("id", id), middleware.GCtxRequestIDField(c))
|
|
response.Output(c, ecode.InternalServerError.ToHTTPCode())
|
|
}
|
|
return
|
|
}
|
|
|
|
data := &types.UserExampleObjDetail{}
|
|
err = copier.Copy(data, userExample)
|
|
if err != nil {
|
|
response.Error(c, ecode.ErrGetByIDUserExample)
|
|
return
|
|
}
|
|
// Note: if copier.Copy cannot assign a value to a field, add it here
|
|
data.ID = idStr
|
|
|
|
response.Success(c, gin.H{"userExample": data})
|
|
}
|
|
|
|
// List of records by query parameters
|
|
// @Summary list of userExamples by query parameters
|
|
// @Description list of userExamples by paging and conditions
|
|
// @Tags userExample
|
|
// @accept json
|
|
// @Produce json
|
|
// @Param data body types.Params true "query parameters"
|
|
// @Success 200 {object} types.ListUserExamplesRespond{}
|
|
// @Router /api/v1/userExample/list [post]
|
|
// @Security BearerAuth
|
|
func (h *userExampleHandler) List(c *gin.Context) {
|
|
form := &types.ListUserExamplesRequest{}
|
|
err := c.ShouldBindJSON(form)
|
|
if err != nil {
|
|
logger.Warn("ShouldBindJSON error: ", logger.Err(err), middleware.GCtxRequestIDField(c))
|
|
response.Error(c, ecode.InvalidParams)
|
|
return
|
|
}
|
|
|
|
ctx := middleware.WrapCtx(c)
|
|
userExamples, total, err := h.iDao.GetByColumns(ctx, &form.Params)
|
|
if err != nil {
|
|
logger.Error("GetByColumns error", logger.Err(err), logger.Any("form", form), middleware.GCtxRequestIDField(c))
|
|
response.Output(c, ecode.InternalServerError.ToHTTPCode())
|
|
return
|
|
}
|
|
|
|
data, err := convertUserExamples(userExamples)
|
|
if err != nil {
|
|
response.Error(c, ecode.ErrListUserExample)
|
|
return
|
|
}
|
|
|
|
response.Success(c, gin.H{
|
|
"userExamples": data,
|
|
"total": total,
|
|
})
|
|
}
|
|
|
|
func getUserExampleIDFromPath(c *gin.Context) (string, uint64, bool) {
|
|
idStr := c.Param("id")
|
|
id, err := utils.StrToUint64E(idStr)
|
|
if err != nil || id == 0 {
|
|
logger.Warn("StrToUint64E error: ", logger.String("idStr", idStr), middleware.GCtxRequestIDField(c))
|
|
return "", 0, true
|
|
}
|
|
|
|
return idStr, id, false
|
|
}
|
|
|
|
func convertUserExample(userExample *model.UserExample) (*types.UserExampleObjDetail, error) {
|
|
data := &types.UserExampleObjDetail{}
|
|
err := copier.Copy(data, userExample)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Note: if copier.Copy cannot assign a value to a field, add it here
|
|
data.ID = utils.Uint64ToStr(userExample.ID)
|
|
|
|
return data, nil
|
|
}
|
|
|
|
func convertUserExamples(fromValues []*model.UserExample) ([]*types.UserExampleObjDetail, error) {
|
|
toValues := []*types.UserExampleObjDetail{}
|
|
for _, v := range fromValues {
|
|
data, err := convertUserExample(v)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
toValues = append(toValues, data)
|
|
}
|
|
|
|
return toValues, nil
|
|
}
|