package codigos 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 Access Code // @Tags codigos // @Accept json // @Produce json // @Param req body CreateCodigoInput true "Req" // @Success 201 {object} map[string]interface{} // @Router /api/codigos-acesso [post] func (h *Handler) Create(c *gin.Context) { var req CreateCodigoInput if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } code, err := h.service.Create(c.Request.Context(), req) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusCreated, gin.H{"id": uuid.UUID(code.ID.Bytes).String(), "codigo": code.Codigo}) } // List godoc // @Summary List Access Codes // @Tags codigos // @Produce json // @Success 200 {array} map[string]interface{} // @Router /api/codigos-acesso [get] func (h *Handler) List(c *gin.Context) { codes, err := h.service.List(c.Request.Context()) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } resp := make([]map[string]interface{}, len(codes)) for i, v := range codes { resp[i] = map[string]interface{}{ "id": uuid.UUID(v.ID.Bytes).String(), "codigo": v.Codigo, "descricao": v.Descricao.String, "validade_dias": v.ValidadeDias, "criado_em": v.CriadoEm.Time, "expira_em": v.ExpiraEm.Time, "ativo": v.Ativo, "usos": v.Usos, } } c.JSON(http.StatusOK, resp) } // Delete godoc // @Summary Delete Access Code // @Tags codigos // @Param id path string true "ID" // @Success 200 {object} map[string]string // @Router /api/codigos-acesso/{id} [delete] func (h *Handler) Delete(c *gin.Context) { id := c.Param("id") if id == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "id required"}) return } err := h.service.Delete(c.Request.Context(), id) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "deleted"}) }