61 lines
1.7 KiB
Go
61 lines
1.7 KiB
Go
package app
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"bindbox-game/internal/code"
|
|
"bindbox-game/internal/pkg/core"
|
|
)
|
|
|
|
type synthesizeRequest struct {
|
|
RecipeID int64 `json:"recipe_id"`
|
|
}
|
|
|
|
type listSynthesisLogsAppRequest struct {
|
|
Page int `form:"page"`
|
|
PageSize int `form:"page_size"`
|
|
}
|
|
|
|
func (h *handler) ListSynthesisRecipesForUser() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
userID := int64(ctx.SessionUserInfo().Id)
|
|
list, err := h.synthesis.GetAvailableRecipesForUser(ctx.RequestContext(), userID)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusInternalServerError, code.ServerError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(map[string]interface{}{"list": list})
|
|
}
|
|
}
|
|
|
|
func (h *handler) DoSynthesis() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
req := new(synthesizeRequest)
|
|
if err := ctx.ShouldBindJSON(req); err != nil || req.RecipeID <= 0 {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "invalid recipe_id"))
|
|
return
|
|
}
|
|
userID := int64(ctx.SessionUserInfo().Id)
|
|
inv, err := h.synthesis.Synthesize(ctx.RequestContext(), userID, req.RecipeID)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(inv)
|
|
}
|
|
}
|
|
|
|
func (h *handler) ListSynthesisLogsForUser() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
userID := int64(ctx.SessionUserInfo().Id)
|
|
req := new(listSynthesisLogsAppRequest)
|
|
_ = ctx.ShouldBindForm(req)
|
|
list, total, err := h.synthesis.ListLogs(ctx.RequestContext(), req.Page, req.PageSize, &userID)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusInternalServerError, code.ServerError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(map[string]interface{}{"list": list, "total": total})
|
|
}
|
|
}
|