package api import ( "encoding/json" "net/http" "GoApi/internal/repository" ) // Novo controller dedicado para criação de eventos type CreateEventController struct { Repo repository.EventRepository } func NewCreateEventController(repo repository.EventRepository) CreateEventController { return CreateEventController{Repo: repo} } type CreateEventRequest struct { Name string `json:"name"` Date string `json:"date"` PassageValue string `json:"passage_value"` HotelValue string `json:"hotel_value"` GiftValue string `json:"gift_value"` TeamValue string `json:"team_value"` SponsorValue string `json:"sponsor_value"` TotalValue string `json:"total_value"` CollaboratorIDs []int64 `json:"collaborator_ids"` } func (c *CreateEventController) CreateEventHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } // Decodifica o JSON do corpo da requisição var req CreateEventRequest if err := json.NewDecoder(r.Body).Decode(&req); err != nil { http.Error(w, "Invalid request body", http.StatusBadRequest) return } // Valida os campos obrigatórios if req.Name == "" || req.Date == "" || req.TotalValue == "" { http.Error(w, "Name, date and total value are required fields", http.StatusBadRequest) return } // Cria o objeto de evento event := &repository.Event{ Name: req.Name, Date: req.Date, PassageValue: req.PassageValue, HotelValue: req.HotelValue, GiftValue: req.GiftValue, TeamValue: req.TeamValue, SponsorValue: req.SponsorValue, TotalValue: req.TotalValue, CollaboratorIDs: req.CollaboratorIDs, } // Chama o repositório para criar o evento eventID, err := c.Repo.CreateEvent(event) if err != nil { http.Error(w, "Failed to create event: "+err.Error(), http.StatusInternalServerError) return } // Retorna o ID do evento criado w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(map[string]interface{}{ "id": eventID, "message": "Event created successfully", }) }