示例#1
0
 private Response HandleRequest(Request request)
 {
     if (request.Type == RequestType.LOGIN)
     {
         AccountDTO accountDto = (AccountDTO)request.Data;
         Account    account    = null;
         try
         {
             lock (travelServices)
             {
                 account = travelServices.Login(accountDto.Username, accountDto.Password);
             }
             if (account == null)
             {
                 return(new Response()
                 {
                     Type = ResponseType.OK
                 });
             }
             travelServices.AddTravelObserver(this);
             return(new Response()
             {
                 Type = ResponseType.OK,
                 Data = new AccountDTO(account.Id, account.Username, account.Password)
             });
         }
         catch (Exception e)
         {
             return(new Response()
             {
                 Type = ResponseType.ERROR,
                 Data = e.Message
             });
         }
     }
     if (request.Type == RequestType.LOGOUT)
     {
         //LOGGER.Info("Logout request");
         lock (travelServices)
         {
             travelServices.Logout();
         }
         travelServices.RemoveTravelObserver(this);
         connected = false;
         return(new Response()
         {
             Type = ResponseType.OK
         });
     }
     if (request.Type == RequestType.GET_ALL_FLIGHTS)
     {
         // LOGGER.Info("get all trips request");
         List <Flight> flights = null;
         try
         {
             lock (travelServices)
             {
                 flights = travelServices.GetAllFlights();
             }
             return(new Response()
             {
                 Type = ResponseType.OK,
                 Data = flights
                        .Select(f => new FlightDTO(f.Id, f.Destination, f.Data, f.AirportName, f.AvailableSeats))
                        .ToList()
             });
         }
         catch (Exception e)
         {
             //LOGGER.Warn("getting all flights failed ");
             return(new Response()
             {
                 Type = ResponseType.ERROR,
                 Data = e.Message
             });
         }
     }
     if (request.Type == RequestType.SEARCH_FLIGHTS)
     {
         //LOGGER.Info("search flights request");
         List <Flight> flights = null;
         try
         {
             FlightDTO flightDto = (FlightDTO)request.Data;
             lock (travelServices)
             {
                 flights = travelServices.GetFlightsByDestAndDate(flightDto.Destination, flightDto.Date);
             }
             //LOGGER.Info("returning response with searched flights");
             return(new Response()
             {
                 Type = ResponseType.OK,
                 Data = flights
                        .Select(f => new FlightDTO(f.Id, f.Destination, f.Data, f.AirportName, f.AvailableSeats))
                        .ToList()
             });
         }
         catch (Exception e)
         {
             //LOGGER.Warn("searching flights failed ");
             return(new Response()
             {
                 Type = ResponseType.ERROR,
                 Data = e.Message
             });
         }
     }
     if (request.Type == RequestType.ADD_TICKET)
     {
         //LOGGER.Info("add ticket request");
         TicketDTO  ticketDto  = (TicketDTO)request.Data;
         FlightDTO  flightDto  = ticketDto.Flight;
         AccountDTO accountDto = ticketDto.Account;
         Flight     t          = new Flight(flightDto.Id, flightDto.Destination, flightDto.Date, flightDto.Airport, flightDto.Available);
         Account    a          = new Account(accountDto.Id, accountDto.Username, accountDto.Password);
         try
         {
             lock (travelServices)
             {
                 travelServices.AddTicket(a, t, ticketDto.ClientName, ticketDto.Tourists, ticketDto.ClientAdress, ticketDto.NrSeats);
             }
             //LOGGER.Info("saved ticket returning ok response");
             return(new Response()
             {
                 Type = ResponseType.OK
             });
         }
         catch (Exception e)
         {
             //LOGGER.Warn("add ticket failed ");
             return(new Response()
             {
                 Type = ResponseType.ERROR,
                 Data = e.Message
             });
         }
     }
     return(null);
 }
 internal void AddTicket(Flight fli, String clientName, String tourists, String clientAddress, int nrSeats)
 {
     Console.WriteLine("adding booking");
     travelServices.AddTicket(Account, fli, clientName, tourists, clientAddress, nrSeats);
 }