From 6b83dcf6183ee15996e3ee16c6d54f6ba5646117 Mon Sep 17 00:00:00 2001 From: Marcos Fonseca Date: Fri, 26 Jul 2024 17:40:34 -0300 Subject: [PATCH] Implementando service e repository para events --- .../internal/repository/event_repository.go | 57 +++++++++++++++++++ backend/internal/services/event_service.go | 40 +++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 backend/internal/repository/event_repository.go create mode 100644 backend/internal/services/event_service.go diff --git a/backend/internal/repository/event_repository.go b/backend/internal/repository/event_repository.go new file mode 100644 index 0000000..defed77 --- /dev/null +++ b/backend/internal/repository/event_repository.go @@ -0,0 +1,57 @@ +package repository + +import ( + "faladev/internal/models" + "time" + + "gorm.io/gorm" +) + +type EventRepository struct { + db *gorm.DB +} + +func NewEventRepository(db *gorm.DB) *EventRepository { + return &EventRepository{ + db: db, + } +} + +func (r *EventRepository) CreateEvent(event *models.Event) error { + return r.db.Create(event).Error +} + +func (r *EventRepository) GetEventByID(id uint) (*models.Event, error) { + var event models.Event + err := r.db.First(&event, id).Error + if err != nil { + return nil, err + } + return &event, nil +} + +func (r *EventRepository) UpdateEvent(event *models.Event) error { + return r.db.Save(event).Error +} + +func (r *EventRepository) DeleteEvent(id uint) error { + return r.db.Delete(&models.Event{}, id).Error +} + +func (r *EventRepository) ListEvents() ([]models.Event, error) { + var events []models.Event + err := r.db.Find(&events).Error + if err != nil { + return nil, err + } + return events, nil +} + +func (r *EventRepository) GetNextEvent() (*models.Event, error) { + var event models.Event + err := r.db.Where("start_date >= ?", time.Now()).Order("start_date, start_time").Debug().First(&event).Error + if err != nil { + return nil, err + } + return &event, nil +} diff --git a/backend/internal/services/event_service.go b/backend/internal/services/event_service.go new file mode 100644 index 0000000..805d047 --- /dev/null +++ b/backend/internal/services/event_service.go @@ -0,0 +1,40 @@ +package services + +import ( + "faladev/internal/models" + "faladev/internal/repository" +) + +type EventService struct { + repo *repository.EventRepository +} + +func NewEventService(repo *repository.EventRepository) *EventService { + return &EventService{ + repo: repo, + } +} + +func (s *EventService) CreateEvent(event *models.Event) error { + return s.repo.CreateEvent(event) +} + +func (s *EventService) GetEventByID(id uint) (*models.Event, error) { + return s.repo.GetEventByID(id) +} + +func (s *EventService) UpdateEvent(event *models.Event) error { + return s.repo.UpdateEvent(event) +} + +func (s *EventService) DeleteEvent(id uint) error { + return s.repo.DeleteEvent(id) +} + +func (s *EventService) ListEvents() ([]models.Event, error) { + return s.repo.ListEvents() +} + +func (s *EventService) GetNextEvent() (*models.Event, error) { + return s.repo.GetNextEvent() +}