getEvents.go 646 B

123456789101112131415161718192021222324252627
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "GoApi/internal/repository"
  6. )
  7. type EventController struct {
  8. Repo repository.EventRepository
  9. }
  10. func NewEventController(repo repository.EventRepository) EventController {
  11. return EventController{Repo: repo}
  12. }
  13. func (c EventController) GetEventsHandler(w http.ResponseWriter, r *http.Request) {
  14. events, err := c.Repo.GetEvents()
  15. if err != nil {
  16. http.Error(w, "ERROR: Not found events", http.StatusInternalServerError)
  17. return
  18. }
  19. w.Header().Set("Content-Type", "application/json")
  20. json.NewEncoder(w).Encode(events)
  21. }