| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package api
- import (
- "net/http"
- "encoding/json"
- "GoApi/internal/repository"
- )
- type EditEventsController struct {
- repo repository.EventRepository
- }
- func NewEditEventsController(repo repository.EventRepository) EditEventsController {
- return EditEventsController{repo: repo}
- }
- type EditEventsRequest struct {
- EventID int `json:"event_id"`
- 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 *EditEventsController) EditEventHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method != http.MethodPost {
- http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
- return
- }
- var req EditEventsRequest
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- http.Error(w, "Invalid request body", http.StatusBadRequest)
- return
- }
- if req.EventID <= 0 {
- http.Error(w, "Event ID is required", http.StatusBadRequest)
- return
- }
- event := repository.Event{
- ID: req.EventID,
- 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,
- }
- if err := c.repo.EditEvent(&event); err != nil {
- http.Error(w, "Failed to edit event: "+err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- json.NewEncoder(w).Encode(map[string]interface{}{
- "message": "Event edited successfully",
- })
- }
|