carpool-service/servers/grpc/server/book.go

76 lines
2.2 KiB
Go
Raw Normal View History

package grpcserver
import (
"context"
"time"
"git.coopgo.io/coopgo-platform/carpool-service/servers/grpc/proto"
"github.com/rs/zerolog/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *CarpoolServiceServerImpl) CreateBooking(ctx context.Context, req *proto.CreateBookingRequest) (*proto.CreateBookingResponse, error) {
booking := req.Booking.ToOCSS()
_, err := s.Handler.Book(booking)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not create booking - %s", err.Error())
}
return &proto.CreateBookingResponse{}, nil
}
func (s *CarpoolServiceServerImpl) GetUserBookings(ctx context.Context, req *proto.GetUserBookingsRequest) (*proto.GetUserBookingsResponse, error) {
log.Debug().
Str("user", req.UserId).
Str("mindate", req.MinDate.AsTime().Format(time.RFC3339)).
Any("maxdate", req.MaxDate).
Msg("grpc server - GetUserBookings")
userid := req.UserId
var mindate *time.Time
if req.MinDate != nil {
d := req.MinDate.AsTime()
mindate = &d
}
var maxdate *time.Time
if req.MaxDate != nil {
d := req.MaxDate.AsTime()
maxdate = &d
}
bookings, err := s.Handler.GetUserBookings(userid, mindate, maxdate)
if err != nil {
return nil, status.Errorf(codes.Internal, "error retrieving user bookings - %s", err.Error())
}
results := []*proto.CarpoolServiceBooking{}
for _, b := range bookings {
nb := proto.BookingFromInternal(b)
results = append(results, nb)
}
return &proto.GetUserBookingsResponse{
Bookings: results,
}, nil
}
func (s *CarpoolServiceServerImpl) UpdateBooking(ctx context.Context, req *proto.UpdateBookingRequest) (*proto.UpdateBookingResponse, error) {
err := s.Handler.UpdateBookingStatus(req.BookingId, req.Status.ToOCSS())
if err != nil {
return nil, status.Errorf(codes.Internal, "could not update booking status")
}
return &proto.UpdateBookingResponse{}, nil
}
func (s *CarpoolServiceServerImpl) GetBooking(ctx context.Context, req *proto.GetBookingRequest) (*proto.GetBookingResponse, error) {
result, err := s.Handler.GetBooking(req.BookingId)
if err != nil {
log.Error().Err(err).Msg("issue retrieving booking in handler")
return nil, err
}
return &proto.GetBookingResponse{
Booking: proto.BookingFromInternal(*result),
}, nil
}