- Cria tabela 'agenda' com FKs normalizadas e colunas específicas de evento - Gera operações CRUD via SQLC para Agenda - Implementa Service e Handler da Agenda - Adiciona cálculo automático de 'status_profissionais' (OK/FALTA/ERRO) baseado na contagem de faltantes - Registra rotas da Agenda em /api/agenda - Atualiza documentação Swagger - Corrige caminhos de importação e tipagem UUID no novo serviço
152 lines
4.4 KiB
Go
152 lines
4.4 KiB
Go
package agenda
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Handler struct {
|
|
service *Service
|
|
}
|
|
|
|
func NewHandler(service *Service) *Handler {
|
|
return &Handler{service: service}
|
|
}
|
|
|
|
// Create godoc
|
|
// @Summary Create a new agenda event
|
|
// @Description Create a new agenda event
|
|
// @Tags agenda
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body CreateAgendaRequest true "Create Agenda Request"
|
|
// @Success 201 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/agenda [post]
|
|
func (h *Handler) Create(c *gin.Context) {
|
|
var req CreateAgendaRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Dados inválidos: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
agenda, err := h.service.Create(c.Request.Context(), req)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erro ao criar agenda: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, agenda)
|
|
}
|
|
|
|
// List godoc
|
|
// @Summary List all agenda events
|
|
// @Description List all agenda events with details
|
|
// @Tags agenda
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {array} map[string]interface{}
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/agenda [get]
|
|
func (h *Handler) List(c *gin.Context) {
|
|
agendas, err := h.service.List(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erro ao listar agendas: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, agendas)
|
|
}
|
|
|
|
// Get godoc
|
|
// @Summary Get agenda event by ID
|
|
// @Description Get agenda event details by ID
|
|
// @Tags agenda
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "Agenda ID"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/agenda/{id} [get]
|
|
func (h *Handler) Get(c *gin.Context) {
|
|
idParam := c.Param("id")
|
|
id, err := uuid.Parse(idParam)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
agenda, err := h.service.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erro ao buscar agenda: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, agenda)
|
|
}
|
|
|
|
// Update godoc
|
|
// @Summary Update agenda event
|
|
// @Description Update agenda event by ID
|
|
// @Tags agenda
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "Agenda ID"
|
|
// @Param request body CreateAgendaRequest true "Update Agenda Request"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/agenda/{id} [put]
|
|
func (h *Handler) Update(c *gin.Context) {
|
|
idParam := c.Param("id")
|
|
id, err := uuid.Parse(idParam)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
var req CreateAgendaRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Dados inválidos: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
agenda, err := h.service.Update(c.Request.Context(), id, req)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erro ao atualizar agenda: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, agenda)
|
|
}
|
|
|
|
// Delete godoc
|
|
// @Summary Delete agenda event
|
|
// @Description Delete agenda event by ID
|
|
// @Tags agenda
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path string true "Agenda ID"
|
|
// @Success 204 {object} nil
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 500 {object} map[string]string
|
|
// @Router /api/agenda/{id} [delete]
|
|
func (h *Handler) Delete(c *gin.Context) {
|
|
idParam := c.Param("id")
|
|
id, err := uuid.Parse(idParam)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
if err := h.service.Delete(c.Request.Context(), id); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erro ao deletar agenda: " + err.Error()})
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusNoContent)
|
|
}
|