bindbox-game/internal/api/admin/synthesis_admin.go

115 lines
3.4 KiB
Go

package admin
import (
"net/http"
"strconv"
"bindbox-game/internal/code"
"bindbox-game/internal/pkg/core"
synthesissvc "bindbox-game/internal/service/synthesis"
)
type listRecipesRequest struct {
Page int `form:"page"`
PageSize int `form:"page_size"`
}
type listSynthesisLogsRequest struct {
Page int `form:"page"`
PageSize int `form:"page_size"`
UserID *int64 `form:"user_id"`
}
func (h *handler) ListSynthesisRecipes() core.HandlerFunc {
return func(ctx core.Context) {
req := new(listRecipesRequest)
_ = ctx.ShouldBindForm(req)
list, total, err := h.synthesis.ListRecipes(ctx.RequestContext(), req.Page, req.PageSize)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusInternalServerError, code.ServerError, err.Error()))
return
}
ctx.Payload(map[string]interface{}{"list": list, "total": total})
}
}
func (h *handler) GetSynthesisRecipe() core.HandlerFunc {
return func(ctx core.Context) {
id, _ := strconv.ParseInt(ctx.Param("id"), 10, 64)
if id <= 0 {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "invalid id"))
return
}
r, err := h.synthesis.GetRecipe(ctx.RequestContext(), id)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusNotFound, code.ServerError, err.Error()))
return
}
ctx.Payload(r)
}
}
func (h *handler) CreateSynthesisRecipe() core.HandlerFunc {
return func(ctx core.Context) {
req := new(synthesissvc.CreateRecipeRequest)
if err := ctx.ShouldBindJSON(req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
return
}
recipe, err := h.synthesis.CreateRecipe(ctx.RequestContext(), *req)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, err.Error()))
return
}
ctx.Payload(recipe)
}
}
func (h *handler) ModifySynthesisRecipe() core.HandlerFunc {
return func(ctx core.Context) {
id, _ := strconv.ParseInt(ctx.Param("id"), 10, 64)
if id <= 0 {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "invalid id"))
return
}
req := new(synthesissvc.CreateRecipeRequest)
if err := ctx.ShouldBindJSON(req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
return
}
if err := h.synthesis.ModifyRecipe(ctx.RequestContext(), id, *req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, err.Error()))
return
}
ctx.Payload(nil)
}
}
func (h *handler) DeleteSynthesisRecipe() core.HandlerFunc {
return func(ctx core.Context) {
id, _ := strconv.ParseInt(ctx.Param("id"), 10, 64)
if id <= 0 {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "invalid id"))
return
}
if err := h.synthesis.DeleteRecipe(ctx.RequestContext(), id); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, err.Error()))
return
}
ctx.Payload(nil)
}
}
func (h *handler) ListSynthesisLogs() core.HandlerFunc {
return func(ctx core.Context) {
req := new(listSynthesisLogsRequest)
_ = ctx.ShouldBindForm(req)
list, total, err := h.synthesis.ListLogs(ctx.RequestContext(), req.Page, req.PageSize, req.UserID)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusInternalServerError, code.ServerError, err.Error()))
return
}
ctx.Payload(map[string]interface{}{"list": list, "total": total})
}
}