package handler import ( "net/http" "strconv" "github.com/go-chi/chi/v5" "financeiro-carvalho/internal/service" ) type PendingBillHandler struct { svc *service.PendingBillService gameSvc *service.GameService } func NewPendingBillHandler(svc *service.PendingBillService, gameSvc *service.GameService) *PendingBillHandler { return &PendingBillHandler{svc: svc, gameSvc: gameSvc} } func (h *PendingBillHandler) List(w http.ResponseWriter, r *http.Request) { items, err := h.svc.List(r.Context()) if err != nil { respondError(w, http.StatusInternalServerError, "failed to list pending bills") return } respondJSON(w, http.StatusOK, items) } func (h *PendingBillHandler) Confirm(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.Confirm(r.Context(), id); err != nil { if err == service.ErrPendingBillNotFound { respondError(w, http.StatusNotFound, "pending bill not found") return } respondError(w, http.StatusInternalServerError, "failed to confirm pending bill") return } h.gameSvc.NotifyAction(r.Context(), "import_confirmed", map[string]any{"count": 1}) w.WriteHeader(http.StatusNoContent) } func (h *PendingBillHandler) Discard(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.Discard(r.Context(), id); err != nil { respondError(w, http.StatusInternalServerError, "failed to discard pending bill") return } w.WriteHeader(http.StatusNoContent) }