예제 #1
0
 public IActionResult GetTrips()
 {
     try
     {
         var allTrips = _service.GetAllTrips();
         return(Ok(allTrips));
     }
     catch (Exception eX)
     {
         return(BadRequest(eX.Message));
     }
 }
예제 #2
0
        public override Task <Response> GetAllTrips(Request request, ServerCallContext context)
        {
            Logger.Info("getting all trips");
            Response response = null;

            try
            {
                List <Trip> t = tripServices.GetAllTrips();
                response = ProtoUtils.CreateGetTripsResponse(t);
            }
            catch (Exception e)
            {
                response = ProtoUtils.CreateErrorResponse(e);
            }
            Logger.InfoFormat("sending get all trips response {0}", response.Type);
            return(Task.FromResult(response));
        }
예제 #3
0
 public List <Trip> GetTrips()
 {
     LOGGER.Info("getting all trips");
     return(tripServices.GetAllTrips());
 }
예제 #4
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);
 }