Exemplo n.º 1
0
 public async Task <CarCompany> GetCompany(long id)
 {
     return(await _rentACarRepository.GetCompany(id));
 }
        public async Task <List <Vehicle> > GetVehiclesBySearch(CarSearch searchParameters)
        {
            List <Vehicle> retVal = new List <Vehicle>();

            if (searchParameters.PickUpDate < DateTime.Now)
            {
                return(retVal);
            }

            if (searchParameters.PickUpDate > searchParameters.ReturnDate)
            {
                return(retVal);
            }

            if (searchParameters.PickUpDate == searchParameters.ReturnDate)
            {
                if (searchParameters.PickUpTime >= searchParameters.ReturnTime)
                {
                    return(retVal);
                }
            }

            var vehicles = await _repository.GetAllVehicles();

            foreach (Vehicle v in vehicles)
            {
                if (v.CarType == searchParameters.CarType && v.Passangers >= searchParameters.Passangers)
                {
                    var company = await _companyRepository.GetCompany(v.CarCompanyId);

                    var pickUpLocationExists = company.Offices.SingleOrDefault(o => o.Location.Name == searchParameters.PickUpLocation) != null;
                    var returnLocationExists = company.Offices.SingleOrDefault(o => o.Location.Name == searchParameters.ReturnLocation) != null;

                    if (pickUpLocationExists && returnLocationExists)
                    {
                        var allCarReservations = await _companyRepository.GetCarReservationsByCompanyId(v.CarCompanyId);

                        if (allCarReservations != null)
                        {
                            var reservations = allCarReservations.Where(cr => cr.VehicleId == v.VehicleId);

                            DateTime pickUpDate = searchParameters.PickUpDate.Add(searchParameters.PickUpTime.TimeOfDay);
                            DateTime returnDate = searchParameters.ReturnDate.Add(searchParameters.ReturnTime.TimeOfDay);
                            bool     available  = true;
                            foreach (CarReservation r in reservations)
                            {
                                // rp    p     rr     r
                                if (r.PickUpDate < pickUpDate && r.ReturnDate > pickUpDate)
                                {
                                    available = false;
                                    break;
                                }

                                // p     rp    r     rr
                                if (r.PickUpDate > pickUpDate && r.PickUpDate < returnDate)
                                {
                                    available = false;
                                    break;
                                }

                                if (r.PickUpDate == pickUpDate && r.ReturnDate == returnDate)
                                {
                                    available = false;
                                    break;
                                }
                            }

                            if (available)
                            {
                                retVal.Add(v);
                            }
                        }
                        else
                        {
                            retVal.Add(v);
                        }
                    }
                }
            }

            return(retVal);
        }