feat: add frame

This commit is contained in:
2026-07-08 12:34:21 +03:30
parent 525cd554ef
commit b5420bac21
12 changed files with 280 additions and 13 deletions
+64
View File
@@ -155,6 +155,70 @@ func (h *Handler) carpetAction(w http.ResponseWriter, r *http.Request, sel bool)
}
}
// Frames — GET /api/frames (قاب‌های آواتار + مالکیت + انتخابِ کاربر)
func (h *Handler) Frames(w http.ResponseWriter, r *http.Request) {
id, ok := uid(r)
if !ok {
httpx.Error(w, http.StatusUnauthorized, "unauthorized")
return
}
frames, err := h.svc.Frames(r.Context(), id)
if err != nil {
httpx.Error(w, http.StatusInternalServerError, "server error")
return
}
selected := ""
if p, _ := h.svc.GetProfile(r.Context(), id); p != nil {
selected = p.SelectedFrame
}
httpx.JSON(w, http.StatusOK, map[string]any{"frames": frames, "selected": selected})
}
// BuyFrame — POST /api/shop/buy-frame
func (h *Handler) BuyFrame(w http.ResponseWriter, r *http.Request) {
h.frameAction(w, r, false)
}
// SelectFrame — POST /api/shop/select-frame
func (h *Handler) SelectFrame(w http.ResponseWriter, r *http.Request) {
h.frameAction(w, r, true)
}
func (h *Handler) frameAction(w http.ResponseWriter, r *http.Request, sel bool) {
id, ok := uid(r)
if !ok {
httpx.Error(w, http.StatusUnauthorized, "unauthorized")
return
}
var req struct {
FrameID string `json:"frame_id"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httpx.Error(w, http.StatusBadRequest, "invalid body")
return
}
var err error
if sel {
err = h.svc.SelectFrame(r.Context(), id, req.FrameID)
} else {
err = h.svc.BuyFrame(r.Context(), id, req.FrameID)
}
switch {
case err == nil:
httpx.JSON(w, http.StatusOK, map[string]string{"message": "ok"})
case errors.Is(err, ErrNotFound):
httpx.Error(w, http.StatusNotFound, "frame not found")
case errors.Is(err, ErrAlreadyOwned):
httpx.Error(w, http.StatusConflict, "already owned")
case errors.Is(err, ErrNotOwned):
httpx.Error(w, http.StatusForbidden, "frame not owned")
case errors.Is(err, ErrInsufficient):
httpx.Error(w, http.StatusPaymentRequired, "insufficient coins")
default:
httpx.Error(w, http.StatusInternalServerError, "server error")
}
}
// ChatPacks — GET /api/chat-packs (بسته‌های پیام/شکلک + مالکیتِ کاربر)
func (h *Handler) ChatPacks(w http.ResponseWriter, r *http.Request) {
id, ok := uid(r)