editEvents.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package api
  2. import (
  3. "net/http"
  4. "encoding/json"
  5. "GoApi/internal/repository"
  6. )
  7. type EditEventsController struct {
  8. repo repository.EventRepository
  9. }
  10. func NewEditEventsController(repo repository.EventRepository) EditEventsController {
  11. return EditEventsController{repo: repo}
  12. }
  13. type EditEventsRequest struct {
  14. EventID int `json:"event_id"`
  15. Name string `json:"name"`
  16. Date string `json:"date"`
  17. PassageValue string `json:"passage_value"`
  18. HotelValue string `json:"hotel_value"`
  19. GiftValue string `json:"gift_value"`
  20. TeamValue string `json:"team_value"`
  21. SponsorValue string `json:"sponsor_value"`
  22. TotalValue string `json:"total_value"`
  23. CollaboratorIDs []int64 `json:"collaborator_ids"`
  24. }
  25. func (c *EditEventsController) EditEventHandler(w http.ResponseWriter, r *http.Request) {
  26. if r.Method != http.MethodPost {
  27. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  28. return
  29. }
  30. var req EditEventsRequest
  31. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  32. http.Error(w, "Invalid request body", http.StatusBadRequest)
  33. return
  34. }
  35. if req.EventID <= 0 {
  36. http.Error(w, "Event ID is required", http.StatusBadRequest)
  37. return
  38. }
  39. event := repository.Event{
  40. ID: req.EventID,
  41. Name: req.Name,
  42. Date: req.Date,
  43. PassageValue: req.PassageValue,
  44. HotelValue: req.HotelValue,
  45. GiftValue: req.GiftValue,
  46. TeamValue: req.TeamValue,
  47. SponsorValue: req.SponsorValue,
  48. TotalValue: req.TotalValue,
  49. CollaboratorIDs: req.CollaboratorIDs,
  50. }
  51. if err := c.repo.EditEvent(&event); err != nil {
  52. http.Error(w, "Failed to edit event: "+err.Error(), http.StatusInternalServerError)
  53. return
  54. }
  55. w.Header().Set("Content-Type", "application/json")
  56. json.NewEncoder(w).Encode(map[string]interface{}{
  57. "message": "Event edited successfully",
  58. })
  59. }