예제 #1
0
        public async Task <List <GraphDTO> > GetLastMonthsCarReservations(long id, int numberOfMonths)
        {
            var reservations = await _rentACarRepository.GetCarReservationsByCompanyId(id);

            var company = await GetCompany(id);

            var vehicles = company.Vehicles;

            List <GraphDTO> months     = new List <GraphDTO>();
            var             lastMonths = Enumerable.Range(0, numberOfMonths)
                                         .Select(i => DateTime.Now.AddMonths(i - numberOfMonths + 1))
                                         .Select(date => date.ToString("MM/yyyy"));

            var lastMonthsList = lastMonths.ToList();

            for (int i = 0; i < numberOfMonths; ++i)
            {
                months.Add(new GraphDTO());

                months[i].Month           = lastMonthsList[i];
                months[i].CarReservations = 0;
            }

            if (vehicles != null && reservations != null)
            {
                foreach (CarReservation cr in reservations)
                {
                    int index = vehicles.FindIndex(v => v.VehicleId == cr.VehicleId);

                    if (index >= 0)
                    {
                        var date = cr.PickUpDate.ToString("MM/yyyy");

                        if (lastMonths.Contains(date))
                        {
                            var monthIndex = months.FindIndex(g => g.Month == date);

                            if (monthIndex >= 0)
                            {
                                ++months[monthIndex].CarReservations;
                            }
                        }
                    }
                }

                return(months);
            }

            return(months);
        }
        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);
        }