rentease/internal/booking/service.go
2024-09-08 23:12:13 +02:00

93 lines
2.1 KiB
Go

package booking
import (
"log"
"time"
"gorm.io/gorm"
)
type Service struct {
store *PgStore
}
func NewService(db *gorm.DB) (*Service, error) {
return &Service{store: NewPgStore(db)}, nil
}
func (bs Service) All() []*Line {
return bs.store.All()
}
func (bs Service) Search(value string) []*Line {
return bs.store.Search(value)
}
// TODO: return the error
func (bs Service) Create(From time.Time, To time.Time, Name, PhoneNumber, Email, Platform string,
CustomerNumber int, PlatformFees float64, externalId *string,
) *Booking {
b := &Booking{
Name: Name,
PhoneNumber: PhoneNumber,
CustomerNumber: CustomerNumber,
Email: Email,
From: From,
To: To,
Platform: Platform,
PlatformFees: PlatformFees,
ExternalId: externalId,
}
err := bs.store.Create(b)
if err != nil {
log.Println(err)
}
return b
}
func (bs Service) One(id int) *Booking {
return bs.store.Get(id)
}
// TODO: return the error
func (bs Service) Update(id int, From time.Time, To time.Time, Name string, PhoneNumber string, Email string, Platform string,
CustomerNumber int, PlatformFees float64, externalId *string,
) *Booking {
b := &Booking{
Id: id,
Name: Name,
PhoneNumber: PhoneNumber,
CustomerNumber: CustomerNumber,
Email: Email,
From: From,
To: To,
Platform: Platform,
PlatformFees: PlatformFees,
ExternalId: externalId,
}
if err := bs.store.Update(b); err != nil {
log.Println(err)
}
return b
}
// func (bs Service) CreateRequest(From time.Time, To time.Time, Name string, PhoneNumber string, Email string, Item string, CustomerNumber int) *BookingRequest {
// b := &BookingRequest{
// CustomerName: Name,
// PhoneNumber: &PhoneNumber,
// CustomerNumber: CustomerNumber,
// Email: &Email,
// From: From,
// To: To,
// ItemType: Item,
// }
// _ = bs.db.Create(b)
// return b
// }
func (bs Service) Cancel(id int) {
err := bs.store.Cancel(id)
if err != nil {
log.Println(err)
}
}