solidarity-transport/handler/bookings.go

74 lines
1.8 KiB
Go

package handler
import (
"errors"
"time"
"git.coopgo.io/coopgo-platform/solidarity-transport/types"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
)
func (h Handler) BookDriverJourney(passengerid string, driverid string, journeyid string) error {
journey, err := h.Storage.GetDriverJourney(journeyid)
if err != nil {
log.Error().Err(err).Msg("could not find driver journey")
return err
}
if journey.DriverId != driverid {
return errors.New("not authorized : journey id driver and driverid mismatch")
}
booking := types.Booking{
Id: uuid.NewString(),
GroupId: uuid.NewString(),
Status: "WAITING_CONFIRMATION",
PassengerId: passengerid,
DriverId: driverid,
Journey: journey,
}
if err := h.Storage.CreateBooking(booking); err != nil {
log.Error().Err(err).Msg("error creating booking")
return err
}
return nil
}
func (h Handler) GetBookings(startdate time.Time, enddate time.Time) ([]*types.Booking, error) {
res := []*types.Booking{}
bookings, err := h.Storage.GetAllBookings()
if err != nil {
log.Error().Err(err).Msg("error retrieving bookings")
return nil, err
}
for _, b := range bookings {
if b.Journey.DriverDepartureDate.After(startdate) && b.Journey.DriverDepartureDate.Before(enddate) {
res = append(res, b)
}
}
return res, nil
}
func (h Handler) GetBooking(id string) (*types.Booking, error) {
booking, err := h.Storage.GetBooking(id)
if err != nil {
log.Error().Err(err).Msg("could not get booking")
return nil, err
}
return booking, nil
}
func (h Handler) UpdateBookingStatus(bookingid string, newStatus string) error {
if err := h.Storage.UpdateBookingStatus(bookingid, newStatus); err != nil {
log.Error().Err(err).Msg("could not update booking")
return err
}
return nil
}