120 lines
3.6 KiB
Go
120 lines
3.6 KiB
Go
package app
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"bindbox-game/internal/code"
|
|
"bindbox-game/internal/pkg/core"
|
|
welfaresvc "bindbox-game/internal/service/welfare_activity"
|
|
)
|
|
|
|
type listWelfareActivitiesRequest struct {
|
|
Type string `form:"type"`
|
|
Status string `form:"status"`
|
|
Page int `form:"page"`
|
|
PageSize int `form:"page_size"`
|
|
}
|
|
|
|
type listWelfareParticipantsRequest struct {
|
|
Page int `form:"page"`
|
|
PageSize int `form:"page_size"`
|
|
}
|
|
|
|
type listWelfareWinnersRequest struct {
|
|
Page int `form:"page"`
|
|
PageSize int `form:"page_size"`
|
|
}
|
|
|
|
func (h *handler) ListWelfareActivities() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
req := new(listWelfareActivitiesRequest)
|
|
if err := ctx.ShouldBindForm(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
status := req.Status
|
|
if status == "" {
|
|
status = welfaresvc.StatusActive
|
|
}
|
|
res, err := h.welfare.ListActivities(ctx.RequestContext(), welfaresvc.ListActivitiesRequest{Type: req.Type, Status: status, Page: req.Page, PageSize: req.PageSize})
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListActivitiesError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
func (h *handler) GetWelfareActivity() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
userID := int64(ctx.SessionUserInfo().Id)
|
|
res, err := h.welfare.GetActivity(ctx.RequestContext(), id, userID)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.GetActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
func (h *handler) JoinWelfareActivity() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
info := ctx.SessionUserInfo()
|
|
userID := int64(info.Id)
|
|
if userID <= 0 {
|
|
ctx.AbortWithError(core.Error(http.StatusUnauthorized, code.AuthorizationError, "请先登录"))
|
|
return
|
|
}
|
|
if err := h.welfare.Join(ctx.RequestContext(), id, userID); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ModifyActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(map[string]string{"message": "参与成功"})
|
|
}
|
|
}
|
|
|
|
func (h *handler) ListWelfareParticipants() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, _ := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
req := new(listWelfareParticipantsRequest)
|
|
if err := ctx.ShouldBindForm(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
res, err := h.welfare.ListParticipants(ctx.RequestContext(), id, req.Page, req.PageSize)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListActivitiesError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
func (h *handler) ListWelfareWinners() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, _ := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
req := new(listWelfareWinnersRequest)
|
|
if err := ctx.ShouldBindForm(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
res, err := h.welfare.ListWinners(ctx.RequestContext(), id, req.Page, req.PageSize)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListActivitiesError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|