91 lines
2.4 KiB
Go
91 lines
2.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"financeiro-carvalho/internal/service"
|
|
)
|
|
|
|
type GameHandler struct {
|
|
svc *service.GameService
|
|
}
|
|
|
|
func NewGameHandler(svc *service.GameService) *GameHandler {
|
|
return &GameHandler{svc: svc}
|
|
}
|
|
|
|
func (h *GameHandler) Summary(w http.ResponseWriter, r *http.Request) {
|
|
s, err := h.svc.GetSummary(r.Context())
|
|
if err != nil {
|
|
respondError(w, http.StatusInternalServerError, "failed to get game summary")
|
|
return
|
|
}
|
|
respondJSON(w, http.StatusOK, s)
|
|
}
|
|
|
|
func (h *GameHandler) AwardXP(w http.ResponseWriter, r *http.Request) {
|
|
var body struct {
|
|
EventType string `json:"event_type"`
|
|
Amount int `json:"amount"`
|
|
Description string `json:"description"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
respondError(w, http.StatusBadRequest, "invalid JSON")
|
|
return
|
|
}
|
|
p, err := h.svc.AwardXP(r.Context(), body.EventType, body.Amount, body.Description)
|
|
if err != nil {
|
|
respondError(w, http.StatusInternalServerError, "failed to award xp")
|
|
return
|
|
}
|
|
respondJSON(w, http.StatusOK, p)
|
|
}
|
|
|
|
func (h *GameHandler) ClaimQuest(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
respondError(w, http.StatusBadRequest, "invalid id")
|
|
return
|
|
}
|
|
if err := h.svc.ClaimQuest(r.Context(), id); err != nil {
|
|
respondError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *GameHandler) Achievements(w http.ResponseWriter, r *http.Request) {
|
|
list, err := h.svc.ListAllAchievements(r.Context())
|
|
if err != nil {
|
|
respondError(w, http.StatusInternalServerError, "failed to list achievements")
|
|
return
|
|
}
|
|
respondJSON(w, http.StatusOK, list)
|
|
}
|
|
|
|
func (h *GameHandler) Cosmetics(w http.ResponseWriter, r *http.Request) {
|
|
list, err := h.svc.ListCosmetics(r.Context())
|
|
if err != nil {
|
|
respondError(w, http.StatusInternalServerError, "failed to list cosmetics")
|
|
return
|
|
}
|
|
respondJSON(w, http.StatusOK, list)
|
|
}
|
|
|
|
func (h *GameHandler) EquipCosmetic(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.Atoi(chi.URLParam(r, "id"))
|
|
if err != nil {
|
|
respondError(w, http.StatusBadRequest, "invalid id")
|
|
return
|
|
}
|
|
if err := h.svc.EquipCosmetic(r.Context(), id); err != nil {
|
|
respondError(w, http.StatusInternalServerError, "failed to equip cosmetic")
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|