photum/backend/internal/anos_formaturas/handler.go
NANDO9322 7f1d4144db feat(api): Implementado novas tabelas ,refatorado a autenticação e aprimorado a documentação Swagger
- Implementado CRUDs para: cursos, empresas, anos_formaturas, tipos_servicos, tipos_eventos
- Implementado lógica de precificação de eventos (precos_tipos_eventos)
- Refatorado a autenticação: Simplificar o payload de cadastro/login (somente e-mail/senha), função padrão 'profissional'
- Corrigido o middleware de autenticação: Resolvido a incompatibilidade de tipo UUID vs String (corrigir erro 500)
- Aprimorado o Swagger: Adicionado structs nomeados, validação de duplicatas (409 Conflict) e segurança BearerAuth
- Atualizar o esquema do banco de dados: Adicionar tabelas e restrições
2025-12-09 17:05:19 -03:00

137 lines
3.7 KiB
Go

package anos_formaturas
import (
"net/http"
"strings"
"photum-backend/internal/db/generated"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type Handler struct {
service *Service
}
func NewHandler(service *Service) *Handler {
return &Handler{service: service}
}
type AnoFormaturaResponse struct {
ID string `json:"id"`
AnoSemestre string `json:"ano_semestre"`
}
type CreateAnoFormaturaRequest struct {
AnoSemestre string `json:"ano_semestre" binding:"required" example:"2029.1"`
}
func toResponse(a generated.AnosFormatura) AnoFormaturaResponse {
return AnoFormaturaResponse{
ID: uuid.UUID(a.ID.Bytes).String(),
AnoSemestre: a.AnoSemestre,
}
}
// Create godoc
// @Summary Create a new graduation year
// @Tags anos_formaturas
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param request body CreateAnoFormaturaRequest true "Ano Semestre"
// @Success 201 {object} AnoFormaturaResponse
// @Failure 400 {object} map[string]string
// @Failure 409 {object} map[string]string
// @Router /api/anos-formaturas [post]
func (h *Handler) Create(c *gin.Context) {
var req CreateAnoFormaturaRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ano, err := h.service.Create(c.Request.Context(), req.AnoSemestre)
if err != nil {
if strings.Contains(err.Error(), "duplicate key value") {
c.JSON(http.StatusConflict, gin.H{"error": "Ano/Semestre already exists"})
return
}
if strings.Contains(err.Error(), "invalid format") {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, toResponse(*ano))
}
// List godoc
// @Summary List all graduation years
// @Tags anos_formaturas
// @Accept json
// @Produce json
// @Security BearerAuth
// @Success 200 {array} AnoFormaturaResponse
// @Router /api/anos-formaturas [get]
func (h *Handler) List(c *gin.Context) {
anos, err := h.service.List(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
var response []AnoFormaturaResponse
for _, ano := range anos {
response = append(response, toResponse(ano))
}
c.JSON(http.StatusOK, response)
}
// Update godoc
// @Summary Update a graduation year
// @Tags anos_formaturas
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path string true "ID"
// @Param request body CreateAnoFormaturaRequest true "Ano Semestre"
// @Success 200 {object} AnoFormaturaResponse
// @Router /api/anos-formaturas/{id} [put]
func (h *Handler) Update(c *gin.Context) {
id := c.Param("id")
var req CreateAnoFormaturaRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ano, err := h.service.Update(c.Request.Context(), id, req.AnoSemestre)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, toResponse(*ano))
}
// Delete godoc
// @Summary Delete a graduation year
// @Tags anos_formaturas
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path string true "ID"
// @Success 204 {object} nil
// @Router /api/anos-formaturas/{id} [delete]
func (h *Handler) Delete(c *gin.Context) {
id := c.Param("id")
if err := h.service.Delete(c.Request.Context(), id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.Status(http.StatusNoContent)
}