Files
carvalho-finances/apps/api/internal/handler/transaction.go
T
Mlcavalho1andClaude Sonnet 4.6 88461a4aea feat: excluir transações de extrato + delete em lote por mês
Remove restrição que bloqueava delete de transações importadas.
Adiciona DELETE /transactions?month=YYYY-MM para exclusão em lote
e botão "EXCLUIR MÊS" na view de transações.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
2026-05-27 16:41:26 -03:00

100 lines
2.7 KiB
Go

package handler
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"financeiro-carvalho/internal/model"
"financeiro-carvalho/internal/repository"
"financeiro-carvalho/internal/service"
)
type TransactionHandler struct {
svc *service.TransactionService
gameSvc *service.GameService
}
func NewTransactionHandler(svc *service.TransactionService, gameSvc *service.GameService) *TransactionHandler {
return &TransactionHandler{svc: svc, gameSvc: gameSvc}
}
func (h *TransactionHandler) List(w http.ResponseWriter, r *http.Request) {
month := r.URL.Query().Get("month")
txs, err := h.svc.List(r.Context(), month)
if err != nil {
respondError(w, http.StatusInternalServerError, "failed to list transactions")
return
}
respondJSON(w, http.StatusOK, txs)
}
func (h *TransactionHandler) Create(w http.ResponseWriter, r *http.Request) {
var t model.Transaction
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
respondError(w, http.StatusBadRequest, "invalid JSON")
return
}
out, err := h.svc.Create(r.Context(), t)
if err != nil {
respondError(w, http.StatusBadRequest, err.Error())
return
}
h.gameSvc.NotifyAction(r.Context(), "transaction_created", map[string]any{"category_id": out.CategoryID})
respondJSON(w, http.StatusCreated, out)
}
func (h *TransactionHandler) Update(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
respondError(w, http.StatusBadRequest, "invalid id")
return
}
var t model.Transaction
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
respondError(w, http.StatusBadRequest, "invalid JSON")
return
}
t.ID = id
out, err := h.svc.Update(r.Context(), t)
if errors.Is(err, repository.ErrNotFound) {
respondError(w, http.StatusNotFound, err.Error())
return
}
if err != nil {
respondError(w, http.StatusBadRequest, err.Error())
return
}
respondJSON(w, http.StatusOK, out)
}
func (h *TransactionHandler) Delete(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.Delete(r.Context(), id); err != nil {
respondError(w, http.StatusNotFound, err.Error())
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *TransactionHandler) DeleteByMonth(w http.ResponseWriter, r *http.Request) {
month := r.URL.Query().Get("month")
if len(month) != 7 {
respondError(w, http.StatusBadRequest, "month required (YYYY-MM)")
return
}
count, err := h.svc.DeleteByMonth(r.Context(), month)
if err != nil {
respondError(w, http.StatusInternalServerError, err.Error())
return
}
respondJSON(w, http.StatusOK, map[string]int{"deleted": count})
}