refatoração de tipagem go

This commit is contained in:
Luiz Silva 2026-01-01 20:03:47 -03:00
parent 6f78511946
commit 0c41ed4279
12 changed files with 175 additions and 117 deletions

View file

@ -7,6 +7,7 @@ import (
"net/http"
"strings"
"e-li.nps/internal/contratos"
"e-li.nps/internal/db"
"github.com/go-chi/chi/v5"
@ -29,7 +30,7 @@ func NewHandlers(pool *pgxpool.Pool) *Handlers {
func (h *Handlers) PostPedido(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var in PedidoInput
var in contratos.PedidoInput
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "json_invalido"})
return
@ -55,29 +56,29 @@ func (h *Handlers) PostPedido(w http.ResponseWriter, r *http.Request) {
if err != nil {
slog.Error("erro ao checar resposta recente", "err", err)
// Fail-closed.
writeJSON(w, http.StatusOK, PedidoResponse{PodeAbrir: false, Motivo: "erro"})
writeJSON(w, http.StatusOK, contratos.PedidoResponse{PodeAbrir: false, Motivo: "erro"})
return
}
if respRecente {
writeJSON(w, http.StatusOK, PedidoResponse{PodeAbrir: false, Motivo: "resposta_recente"})
writeJSON(w, http.StatusOK, contratos.PedidoResponse{PodeAbrir: false, Motivo: "resposta_recente"})
return
}
pedidoAberto, err := h.store.HasPedidoEmAbertoRecente(ctx, table, in.InquilinoCodigo, in.UsuarioCodigo)
if err != nil {
slog.Error("erro ao checar pedido em aberto", "err", err)
writeJSON(w, http.StatusOK, PedidoResponse{PodeAbrir: false, Motivo: "erro"})
writeJSON(w, http.StatusOK, contratos.PedidoResponse{PodeAbrir: false, Motivo: "erro"})
return
}
if pedidoAberto {
writeJSON(w, http.StatusOK, PedidoResponse{PodeAbrir: false, Motivo: "pedido_em_aberto"})
writeJSON(w, http.StatusOK, contratos.PedidoResponse{PodeAbrir: false, Motivo: "pedido_em_aberto"})
return
}
id, err := h.store.CreatePedido(ctx, table, in, r)
if err != nil {
slog.Error("erro ao criar pedido", "err", err)
writeJSON(w, http.StatusOK, PedidoResponse{PodeAbrir: false, Motivo: "erro"})
writeJSON(w, http.StatusOK, contratos.PedidoResponse{PodeAbrir: false, Motivo: "erro"})
return
}
@ -102,7 +103,7 @@ func (h *Handlers) PatchResposta(w http.ResponseWriter, r *http.Request) {
return
}
var in PatchInput
var in contratos.PatchInput
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]any{"error": "json_invalido"})
return
@ -132,7 +133,7 @@ func (h *Handlers) PatchResposta(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("db"))
return
}
data := FormPageData{Produto: prod, ID: id, Reg: reg}
data := contratos.FormPageData{Produto: prod, ID: id, Reg: reg}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
h.tpl.Render(w, "form_inner.html", data)
return
@ -173,7 +174,7 @@ func (h *Handlers) GetForm(w http.ResponseWriter, r *http.Request) {
return
}
data := FormPageData{
data := contratos.FormPageData{
Produto: prod,
ID: id,
Reg: reg,