- 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
133 lines
3.4 KiB
Go
133 lines
3.4 KiB
Go
package empresas
|
|
|
|
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 EmpresaResponse struct {
|
|
ID string `json:"id"`
|
|
Nome string `json:"nome"`
|
|
}
|
|
|
|
type CreateEmpresaRequest struct {
|
|
Nome string `json:"nome" binding:"required"`
|
|
}
|
|
|
|
func toResponse(e generated.Empresa) EmpresaResponse {
|
|
return EmpresaResponse{
|
|
ID: uuid.UUID(e.ID.Bytes).String(),
|
|
Nome: e.Nome,
|
|
}
|
|
}
|
|
|
|
// Create godoc
|
|
// @Summary Create a new company
|
|
// @Tags empresas
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param request body CreateEmpresaRequest true "Empresa Name"
|
|
// @Success 201 {object} EmpresaResponse
|
|
// @Failure 400 {object} map[string]string
|
|
// @Failure 409 {object} map[string]string
|
|
// @Router /api/empresas [post]
|
|
func (h *Handler) Create(c *gin.Context) {
|
|
var req CreateEmpresaRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
empresa, err := h.service.Create(c.Request.Context(), req.Nome)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "duplicate key value") {
|
|
c.JSON(http.StatusConflict, gin.H{"error": "Empresa already exists"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, toResponse(*empresa))
|
|
}
|
|
|
|
// List godoc
|
|
// @Summary List all companies
|
|
// @Tags empresas
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Success 200 {array} EmpresaResponse
|
|
// @Router /api/empresas [get]
|
|
func (h *Handler) List(c *gin.Context) {
|
|
empresas, err := h.service.List(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
var response []EmpresaResponse
|
|
for _, empresa := range empresas {
|
|
response = append(response, toResponse(empresa))
|
|
}
|
|
c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// Update godoc
|
|
// @Summary Update a company
|
|
// @Tags empresas
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path string true "Empresa ID"
|
|
// @Param request body CreateEmpresaRequest true "Empresa Name"
|
|
// @Success 200 {object} EmpresaResponse
|
|
// @Router /api/empresas/{id} [put]
|
|
func (h *Handler) Update(c *gin.Context) {
|
|
id := c.Param("id")
|
|
var req CreateEmpresaRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
empresa, err := h.service.Update(c.Request.Context(), id, req.Nome)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, toResponse(*empresa))
|
|
}
|
|
|
|
// Delete godoc
|
|
// @Summary Delete a company
|
|
// @Tags empresas
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path string true "Empresa ID"
|
|
// @Success 204 {object} nil
|
|
// @Router /api/empresas/{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)
|
|
}
|