|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/Financial-Partner/server/internal/contextutil" |
| 9 | + "github.com/Financial-Partner/server/internal/entities" |
| 10 | + "github.com/Financial-Partner/server/internal/interfaces/http/dto" |
| 11 | + httperror "github.com/Financial-Partner/server/internal/interfaces/http/error" |
| 12 | + responde "github.com/Financial-Partner/server/internal/interfaces/http/respond" |
| 13 | +) |
| 14 | + |
| 15 | +//go:generate mockgen -source=gacha.go -destination=gacha_mock.go -package=handler |
| 16 | + |
| 17 | +type GachaService interface { |
| 18 | + DrawGacha(ctx context.Context, userID string, req *dto.DrawGachaRequest) (*entities.Gacha, error) |
| 19 | + PreviewGachas(ctx context.Context, userID string) ([]entities.Gacha, error) |
| 20 | +} |
| 21 | + |
| 22 | +// @Summary Decrease user's gacha amount and return gacha result |
| 23 | +// @Description Decrease user's gacha amount and return gacha result |
| 24 | +// @Tags gacha |
| 25 | +// @Accept json |
| 26 | +// @Produce json |
| 27 | +// @Param request body dto.DrawGachaRequest true "Draw gacha request" |
| 28 | +// @Param Authorization header string true "Bearer {token}" default "Bearer " |
| 29 | +// @Success 200 {object} dto.GachaResponse |
| 30 | +// @Failure 401 {object} dto.ErrorResponse |
| 31 | +// @Failure 500 {object} dto.ErrorResponse |
| 32 | +// @Router /gacha/draw [post] |
| 33 | +func (h *Handler) DrawGacha(w http.ResponseWriter, r *http.Request) { |
| 34 | + userID, ok := contextutil.GetUserID(r.Context()) |
| 35 | + if !ok { |
| 36 | + h.log.Warnf("failed to get user ID from context") |
| 37 | + responde.WithError(w, r, h.log, nil, httperror.ErrUnauthorized, http.StatusUnauthorized) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + var req dto.DrawGachaRequest |
| 42 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 43 | + h.log.WithError(err).Warnf("failed to decode request body") |
| 44 | + responde.WithError(w, r, h.log, err, httperror.ErrInvalidRequest, http.StatusBadRequest) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + gacha, err := h.gachaService.DrawGacha(r.Context(), userID, &req) |
| 49 | + if err != nil { |
| 50 | + h.log.WithError(err).Warnf("failed to draw a gacha") |
| 51 | + responde.WithError(w, r, h.log, err, httperror.ErrFailedToDrawGacha, http.StatusInternalServerError) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + resp := dto.GachaResponse{ |
| 56 | + ID: gacha.ID.Hex(), |
| 57 | + ImgSrc: gacha.ImgSrc, |
| 58 | + } |
| 59 | + |
| 60 | + responde.WithJSON(w, r, resp, http.StatusOK) |
| 61 | +} |
| 62 | + |
| 63 | +// @Summary Get 9 gacha images for preview |
| 64 | +// @Description Get 9 gacha images for preview |
| 65 | +// @Tags gacha |
| 66 | +// @Accept json |
| 67 | +// @Produce json |
| 68 | +// @Param Authorization header string true "Bearer {token}" default "Bearer " |
| 69 | +// @Success 200 {object} dto.PreviewGachasResponse |
| 70 | +// @Failure 401 {object} dto.ErrorResponse |
| 71 | +// @Failure 500 {object} dto.ErrorResponse |
| 72 | +// @Router /gacha/preview [get] |
| 73 | +func (h *Handler) PreviewGachas(w http.ResponseWriter, r *http.Request) { |
| 74 | + userID, ok := contextutil.GetUserID(r.Context()) |
| 75 | + if !ok { |
| 76 | + h.log.Warnf("failed to get user ID from context") |
| 77 | + responde.WithError(w, r, h.log, nil, httperror.ErrUnauthorized, http.StatusUnauthorized) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + gachas, err := h.gachaService.PreviewGachas(r.Context(), userID) |
| 82 | + if err != nil { |
| 83 | + h.log.WithError(err).Warnf("failed to get preview gacha images") |
| 84 | + responde.WithError(w, r, h.log, err, httperror.ErrFailedToPreviewGachas, http.StatusInternalServerError) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + resp := dto.PreviewGachasResponse{ |
| 89 | + Gachas: make([]dto.GachaResponse, 0, 9), |
| 90 | + } |
| 91 | + |
| 92 | + for _, gacha := range gachas { |
| 93 | + resp.Gachas = append(resp.Gachas, dto.GachaResponse{ |
| 94 | + ID: gacha.ID.Hex(), |
| 95 | + ImgSrc: gacha.ImgSrc, |
| 96 | + }) |
| 97 | + } |
| 98 | + |
| 99 | + responde.WithJSON(w, r, resp, http.StatusOK) |
| 100 | +} |
0 commit comments