Skip to content

Commit

Permalink
Implementando service e repository para events
Browse files Browse the repository at this point in the history
  • Loading branch information
mffonseca committed Jul 26, 2024
1 parent b9a3a55 commit 6b83dcf
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
57 changes: 57 additions & 0 deletions backend/internal/repository/event_repository.go
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 40 additions & 0 deletions backend/internal/services/event_service.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 6b83dcf

Please sign in to comment.