From 065f4fffbf517f74d1771c46e03754c2479a1ea2 Mon Sep 17 00:00:00 2001 From: Akulij Date: Mon, 5 May 2025 19:27:08 +0300 Subject: [PATCH] create bc reservation methods --- cmd/app/db.go | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/cmd/app/db.go b/cmd/app/db.go index bfe65b3..64d027c 100644 --- a/cmd/app/db.go +++ b/cmd/app/db.go @@ -152,11 +152,12 @@ var ReservationStatusString = []string{ type Reservation struct { gorm.Model - ID int64 `gorm:"primary_key"` - UserID int64 `gorm:"uniqueIndex:user_event_uniq"` - TimeBooked *time.Time - EventID int64 `gorm:"uniqueIndex:user_event_uniq"` - Status ReservationStatus + ID int64 `gorm:"primary_key"` + UserID int64 `gorm:"uniqueIndex:user_event_uniq"` + EnteredName string + TimeBooked *time.Time + EventID int64 `gorm:"uniqueIndex:user_event_uniq"` + Status ReservationStatus } func (bc BotController) GetAllReservations() ([]Reservation, error) { @@ -177,6 +178,42 @@ func (bc BotController) GetReservationsByEventID(EventID int64) ([]Reservation, return reservations, nil } +func (bc BotController) CountReservationsByEventID(EventID int64) (int64, error) { + var count int64 + result := bc.db.Model(&Reservation{}).Where("event_id = ?", EventID).Count(&count) + if result.Error != nil { + return 0, result.Error + } + return count, nil +} + +func (bc BotController) CreateReservation(userID int64, eventID int64, name string) (Reservation, error) { + var dubaiLocation, _ = time.LoadLocation("Asia/Dubai") + timenow := time.Now().In(dubaiLocation) + reservation := Reservation{ + UserID: userID, + EventID: eventID, + TimeBooked: &timenow, + Status: Booked, + EnteredName: name, + } + result := bc.db.Create(&reservation) + return reservation, result.Error +} + +func (bc BotController) GetReservationByID(reservationID int64) (Reservation, error) { + var reservation Reservation + result := bc.db.First(&reservation, reservationID) + if result.Error != nil { + return Reservation{}, result.Error + } + return reservation, nil +} + +func (bc BotController) UpdateReservation(r Reservation) { + bc.db.Save(&r) +} + type Event struct { gorm.Model ID int64 `gorm:"primary_key"`