Ejemplo n.º 1
0
        //Also checks if the given flight is assigned to the current user.
        public void UpdateFlight(LoginToken <AirlineCompany> token, Flight flight)
        {
            if (token is null || token.user is null)
            {
                throw new NullReferenceException("User is empty.");
            }
            if (flight is null)
            {
                throw new NullReferenceException("Flight provided is empty.");
            }
            if (flight.ID <= 0)
            {
                throw new IllegalValueException($"Flight ID: '{flight.ID}' is not valid: less or equal to zero.");
            }
            if (_flightDAO.Get(flight.ID) == null)
            {
                throw new FlightNotFoundException($"Flight provided: '{flight.ID}' could not be found.");
            }
            if (flight.AirlineCompanyID <= 0)
            {
                throw new IllegalValueException($"Airline company ID: '{flight.AirlineCompanyID}' is not valid: less or equal to zero.");
            }
            if (flight.AirlineCompanyID != token.user.ID)
            {
                throw new UserAccessabillityException($"This user: id '{token.user.ID}' is not the flight's assosiated airline company: '{flight.AirlineCompanyID}'");
            }
            if (_airlineDAO.Get(flight.AirlineCompanyID) is null)
            {
                throw new AirlineNotFoundException($"No airline company with the provided ID: '{flight.AirlineCompanyID}' found.");
            }
            if (flight.DestinationCountryCode <= 0)
            {
                throw new IllegalValueException($"Destination country code: '{flight.DestinationCountryCode}' is not valid: equal or less than zero.");
            }
            if (flight.OriginCountryCode <= 0)
            {
                throw new IllegalValueException($"Origin country code: '{flight.OriginCountryCode}' is not valid: equal or less than zero.");
            }
            if (_countryDAO.Get(flight.OriginCountryCode) is null)
            {
                throw new CountryNotFoundException($"Origin country: id '{flight.OriginCountryCode}', could not be found.");
            }
            if (_countryDAO.Get(flight.DestinationCountryCode) is null)
            {
                throw new CountryNotFoundException($"Destination country: id '{flight.DestinationCountryCode}', could not be found.");
            }
            if ((DateTime.Compare(flight.DepartureTime, DateTime.Now)) < 0)
            {
                throw new IllegalValueException($"Departure date and time: '{flight.DepartureTime}' must be later than the current date and time: '{DateTime.Now}'.");
            }
            if ((DateTime.Compare(flight.LandingTime, DateTime.Now)) < 0)
            {
                throw new IllegalValueException($"Landing date and time: '{flight.LandingTime}' must be later than the current date and time: '{DateTime.Now}'.");
            }
            if (DateTime.Compare(flight.LandingTime, flight.DepartureTime) < 0)
            {
                throw new IllegalValueException($"Landing time: '{flight.LandingTime}' cannot be sooner than departure time: '{flight.DepartureTime}'.");
            }

            _flightDAO.Update(flight);
        }
        protected override bool TryAdministratorLogin(string userName, string password, out LoginToken <Administrator> token)
        {
            token = null;
            if (userName is null || password is null)
            {
                return(false);
            }

            Administrator administratorResult;

            administratorResult = _administratorDAO.GetAdminByUsername(userName);

            if (administratorResult == null)
            {
                return(false);
            }

            if (administratorResult.Password != password)
            {
                throw new WrongPasswordException($"Wrong password for user {userName}.");
            }
            else
            {
                token      = new LoginToken <Administrator>();
                token.user = administratorResult;
                return(true);
            }
        }