public override Task <Response> AddBooking(Request request, ServerCallContext context)
        {
            Logger.Info("add booking");
            Response response = null;

            try
            {
                tripsGrpc.Booking booking = request.Booking;
                Trip    trip         = ProtoUtils.ConvertToTrip(booking.Trip);
                Account account      = ProtoUtils.ConvertToAccount(booking.Account);
                Booking savedBooking = tripServices.AddBooking(booking.Client, booking.Phone, booking.NumTickets, trip, account);
                response = ProtoUtils.CreateOkResponse();

                Logger.Info("notifying observers");
                Response responseBookingAdded = new Response()
                {
                    Booking = new tripsGrpc.Booking()
                    {
                        Id         = savedBooking.Id,
                        Account    = booking.Account,
                        Trip       = booking.Trip,
                        Client     = savedBooking.ClientName,
                        Phone      = savedBooking.PhoneNumber,
                        NumTickets = savedBooking.NumTickets
                    }
                };
                buffer.Post(responseBookingAdded);
            }
            catch (Exception e)
            {
                response = ProtoUtils.CreateErrorResponse(e);
            }
            Logger.InfoFormat("add booking response {0}", response);
            return(Task.FromResult(response));
        }
 internal void AddBooking(string client, string phone, int numTickets, Trip trip)
 {
     LOGGER.Info("adding booking");
     tripServices.AddBooking(client, phone, numTickets, trip, Account);
 }
Exemple #3
0
 private Response HandleRequest(Request request)
 {
     LOGGER.Info("handling request " + request);
     if (request.Type == RequestType.LOGIN)
     {
         LOGGER.Info("Login request");
         AccountDto accountDto = (AccountDto)request.Data;
         Account    account    = null;
         try
         {
             lock (tripServices)
             {
                 account = tripServices.FindAccountByNameAndPassword(accountDto.name, accountDto.password);
             }
             if (account == null)
             {
                 return(new Response()
                 {
                     Type = ResponseType.OK
                 });
             }
             tripServices.AddTripObserver(this);
             return(new Response()
             {
                 Type = ResponseType.OK,
                 Data = new AccountDto(account.Id, account.Name, account.Password)
             });
         }
         catch (Exception e)
         {
             LOGGER.Warn("handling login failed ");
             return(new Response()
             {
                 Type = ResponseType.ERROR,
                 Data = e.Message
             });
         }
     }
     if (request.Type == RequestType.LOGOUT)
     {
         LOGGER.Info("Logout request");
         lock (tripServices)
         {
             tripServices.Logout();
         }
         tripServices.RemoveTripObserver(this);
         connected = false;
         return(new Response()
         {
             Type = ResponseType.OK
         });
     }
     if (request.Type == RequestType.GET_ALL_TRIPS)
     {
         LOGGER.Info("get all trips request");
         List <Trip> trips = null;
         try
         {
             lock (tripServices)
             {
                 trips = tripServices.GetAllTrips();
             }
             return(new Response()
             {
                 Type = ResponseType.OK,
                 Data = trips
                        .Select(t => new TripDto(t.Id, t.Landmark, t.CompanyName, t.DepartureTime, t.Price, t.AvailablePlaces))
                        .ToList()
             });
         }
         catch (Exception e)
         {
             LOGGER.Warn("getting all trips failed ");
             return(new Response()
             {
                 Type = ResponseType.ERROR,
                 Data = e.Message
             });
         }
     }
     if (request.Type == RequestType.SEARCH_TRIPS)
     {
         LOGGER.Info("search trips request");
         List <Trip> trips = null;
         try
         {
             TripDto tripDto = (TripDto)request.Data;
             lock (tripServices)
             {
                 trips = tripServices.GetTripsByLandmarkDepartureHour(tripDto.landmark, tripDto.start, tripDto.end);
             }
             LOGGER.Info("returning response with searched trips");
             return(new Response()
             {
                 Type = ResponseType.OK,
                 Data = trips
                        .Select(t => new TripDto(t.Id, t.Landmark, t.CompanyName, t.DepartureTime, t.Price, t.AvailablePlaces))
                        .ToList()
             });
         }
         catch (Exception e)
         {
             LOGGER.Warn("searching trips failed ");
             return(new Response()
             {
                 Type = ResponseType.ERROR,
                 Data = e.Message
             });
         }
     }
     if (request.Type == RequestType.ADD_BOOKING)
     {
         LOGGER.Info("add booking request");
         BookingDto bookingDto = (BookingDto)request.Data;
         TripDto    tripDto    = bookingDto.trip;
         AccountDto accountDto = bookingDto.account;
         Trip       t          = new Trip(tripDto.id, tripDto.landmark, tripDto.companyName, tripDto.departure, tripDto.price, tripDto.places);
         Account    a          = new Account(accountDto.id, accountDto.name, accountDto.password);
         try
         {
             lock (tripServices)
             {
                 tripServices.AddBooking(bookingDto.client, bookingDto.phone, bookingDto.numTickets, t, a);
             }
             LOGGER.Info("saved booking returning ok response");
             return(new Response()
             {
                 Type = ResponseType.OK
             });
         }
         catch (Exception e)
         {
             LOGGER.Warn("add booking failed ");
             return(new Response()
             {
                 Type = ResponseType.ERROR,
                 Data = e.Message
             });
         }
     }
     return(null);
 }