new file: serv_nginx/api_bb/internal/repository/event_registration_repository.go
new file: serv_nginx/api_bb/internal/repository/event_repository.go add event repo and event registration repository
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
// repository/event_registration_repository.go
|
||||
package repository
|
||||
|
||||
import (
|
||||
"api_bb/internal/models"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type EventRegistrationRepository interface {
|
||||
Create(registration *models.EventRegistration) error
|
||||
FindByID(id uint) (*models.EventRegistration, error)
|
||||
FindByEventID(eventID uint) ([]models.EventRegistration, error)
|
||||
FindByUserID(userID uint) ([]models.EventRegistration, error)
|
||||
FindByEventAndUser(eventID, userID uint) (*models.EventRegistration, error)
|
||||
Update(registration *models.EventRegistration) error
|
||||
Delete(id uint) error
|
||||
UpdateStatus(registrationID uint, status string) error
|
||||
UpdateResultTime(registrationID uint, resultTime string) error
|
||||
CountByEventID(eventID uint) (int64, error)
|
||||
}
|
||||
|
||||
type eventRegistrationRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewEventRegistrationRepository(db *gorm.DB) EventRegistrationRepository {
|
||||
return &eventRegistrationRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *eventRegistrationRepository) Create(registration *models.EventRegistration) error {
|
||||
return r.db.Create(registration).Error
|
||||
}
|
||||
|
||||
func (r *eventRegistrationRepository) FindByID(id uint) (*models.EventRegistration, error) {
|
||||
var registration models.EventRegistration
|
||||
err := r.db.Preload("Event").Preload("User").First(®istration, id).Error
|
||||
return ®istration, err
|
||||
}
|
||||
|
||||
func (r *eventRegistrationRepository) FindByEventID(eventID uint) ([]models.EventRegistration, error) {
|
||||
var registrations []models.EventRegistration
|
||||
err := r.db.Preload("User").Where("event_id = ?", eventID).Find(®istrations).Error
|
||||
return registrations, err
|
||||
}
|
||||
|
||||
func (r *eventRegistrationRepository) FindByUserID(userID uint) ([]models.EventRegistration, error) {
|
||||
var registrations []models.EventRegistration
|
||||
err := r.db.Preload("Event").Where("user_id = ?", userID).Find(®istrations).Error
|
||||
return registrations, err
|
||||
}
|
||||
|
||||
func (r *eventRegistrationRepository) FindByEventAndUser(eventID, userID uint) (*models.EventRegistration, error) {
|
||||
var registration models.EventRegistration
|
||||
err := r.db.Where("event_id = ? AND user_id = ?", eventID, userID).First(®istration).Error
|
||||
return ®istration, err
|
||||
}
|
||||
|
||||
func (r *eventRegistrationRepository) Update(registration *models.EventRegistration) error {
|
||||
return r.db.Save(registration).Error
|
||||
}
|
||||
|
||||
func (r *eventRegistrationRepository) Delete(id uint) error {
|
||||
return r.db.Delete(&models.EventRegistration{}, id).Error
|
||||
}
|
||||
|
||||
func (r *eventRegistrationRepository) UpdateStatus(registrationID uint, status string) error {
|
||||
result := r.db.Model(&models.EventRegistration{}).Where("id = ?", registrationID).Update("status", status)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("registration not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *eventRegistrationRepository) UpdateResultTime(registrationID uint, resultTime string) error {
|
||||
result := r.db.Model(&models.EventRegistration{}).Where("id = ?", registrationID).Update("result_time", resultTime)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("registration not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *eventRegistrationRepository) CountByEventID(eventID uint) (int64, error) {
|
||||
var count int64
|
||||
err := r.db.Model(&models.EventRegistration{}).Where("event_id = ? AND status IN ?", eventID, []string{"pending", "confirmed"}).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// repository/event_repository.go
|
||||
package repository
|
||||
|
||||
import (
|
||||
"api_bb/internal/models"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type EventRepository interface {
|
||||
Create(event *models.Event) error
|
||||
FindByID(id uint) (*models.Event, error)
|
||||
FindAll() ([]models.Event, error)
|
||||
Update(event *models.Event) error
|
||||
Delete(id uint) error
|
||||
FindByType(eventType models.EventType) ([]models.Event, error)
|
||||
FindUpcoming() ([]models.Event, error)
|
||||
FindByDateRange(startDate, endDate time.Time) ([]models.Event, error)
|
||||
UpdateParticipantsCount(eventID uint, count int) error
|
||||
UpdateRegistrationStatus(eventID uint, registrationOpen bool) error
|
||||
}
|
||||
|
||||
type eventRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewEventRepository(db *gorm.DB) EventRepository {
|
||||
return &eventRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *eventRepository) Create(event *models.Event) error {
|
||||
return r.db.Create(event).Error
|
||||
}
|
||||
|
||||
func (r *eventRepository) FindByID(id uint) (*models.Event, error) {
|
||||
var event models.Event
|
||||
err := r.db.Preload("Registrations").First(&event, id).Error
|
||||
return &event, err
|
||||
}
|
||||
|
||||
func (r *eventRepository) FindAll() ([]models.Event, error) {
|
||||
var events []models.Event
|
||||
err := r.db.Order("date DESC").Find(&events).Error
|
||||
return events, err
|
||||
}
|
||||
|
||||
func (r *eventRepository) Update(event *models.Event) error {
|
||||
return r.db.Save(event).Error
|
||||
}
|
||||
|
||||
func (r *eventRepository) Delete(id uint) error {
|
||||
return r.db.Delete(&models.Event{}, id).Error
|
||||
}
|
||||
|
||||
func (r *eventRepository) FindByType(eventType models.EventType) ([]models.Event, error) {
|
||||
var events []models.Event
|
||||
err := r.db.Where("type = ?", eventType).Order("date DESC").Find(&events).Error
|
||||
return events, err
|
||||
}
|
||||
|
||||
func (r *eventRepository) FindUpcoming() ([]models.Event, error) {
|
||||
var events []models.Event
|
||||
err := r.db.Where("date >= ?", time.Now()).Order("date ASC").Find(&events).Error
|
||||
return events, err
|
||||
}
|
||||
|
||||
func (r *eventRepository) FindByDateRange(startDate, endDate time.Time) ([]models.Event, error) {
|
||||
var events []models.Event
|
||||
err := r.db.Where("date BETWEEN ? AND ?", startDate, endDate).Order("date ASC").Find(&events).Error
|
||||
return events, err
|
||||
}
|
||||
|
||||
func (r *eventRepository) UpdateParticipantsCount(eventID uint, count int) error {
|
||||
result := r.db.Model(&models.Event{}).Where("id = ?", eventID).Update("participants_count", count)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("event not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *eventRepository) UpdateRegistrationStatus(eventID uint, registrationOpen bool) error {
|
||||
result := r.db.Model(&models.Event{}).Where("id = ?", eventID).Update("registration_open", registrationOpen)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("event not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user