rentease/internal/booking/booking_store.go
2024-09-08 23:16:50 +02:00

102 lines
2.3 KiB
Go

package booking
import (
"fmt"
"time"
"gorm.io/gorm"
)
type PgStore struct {
db *gorm.DB
}
func NewPgStore(db *gorm.DB) *PgStore {
return &PgStore{db}
}
func (ps *PgStore) All() []*Line {
bookings := make([]*Line, 0)
ps.db.Raw(`
select bookings.id, customer_name, "from", "to", platform, sum(price * quantity) as total, canceled
from bookings
left join items on bookings.id = items.booking_id
group by bookings.id
order by id desc;
`).
Scan(&bookings)
return bookings
}
func (ps *PgStore) Search(value string) []*Line {
bookings := make([]*Line, 0)
ps.db.Raw(`
select bookings.id, customer_name, "from", "to", platform, sum(price * quantity) as total, canceled
from bookings
left join items on bookings.id = items.booking_id
where customer_name ilike ?
group by bookings.id
order by id desc;
`, fmt.Sprintf("%%%s%%", value)).
Scan(&bookings)
return bookings
}
func (ps *PgStore) List(from, to time.Time) ([]*Line, error) {
bookings := make([]*Line, 0)
if err := ps.db.Raw(`
select bookings.id, customer_name, "from", "to", platform, sum(price * quantity) as total, platform_fees
from bookings
join items on bookings.id = items.booking_id
where "to" between ? and ?
group by bookings.id
order by bookings.id;
`,
from.Format(time.DateOnly), to.Format(time.DateOnly)).
Scan(&bookings).
Error; err != nil {
return nil, err
}
return bookings, nil
}
func (ps *PgStore) CardTotal(from, to time.Time) (float64, error) {
cardTotal := 0.0
if err := ps.db.Raw(`
select sum(total)
from (select sum(price * quantity) as total
from bookings
join items on bookings.id = items.booking_id
where "to" between ? and ?
and payment_method = 'Card'
group by booking_id) as t;
`,
from.Format(time.DateOnly), to.Format(time.DateOnly)).
Scan(&cardTotal).
Error; err != nil {
return 0.0, err
}
return cardTotal, nil
}
func (ps *PgStore) Get(id int) *Booking {
b := &Booking{Id: id}
ps.db.Preload("Items").First(b)
return b
}
func (ps *PgStore) Create(b *Booking) error {
return ps.db.Create(b).Error
}
func (ps *PgStore) Update(b *Booking) error {
return ps.db.Save(b).Error
}
func (ps *PgStore) Cancel(id int) error {
b := &Booking{Id: id}
return ps.db.Model(&b).
Update("canceled", true).
Error
}