public async Task <IActionResult> SearchEnterprises(SearchEnterpriseParameters sep)
        {
            if (ModelState.IsValid)
            {
                var temp = await enterpriseService.SearchEnterprises(sep);

                if (temp != null)
                {
                    return(Ok(temp));
                }
                else
                {
                    return(BadRequest(new { Message = "Something went wrong. Please, try again later." }));
                }
            }
            else
            {
                return(BadRequest(new { Message = "Invalid parameters supplied." }));
            }
        }
Exemplo n.º 2
0
        public async Task <IEnumerable <Enterprise> > SearchEnterprises(SearchEnterpriseParameters sep)
        {
            var temp = await repo.GetAllEnterprisesForSearch();

            List <Enterprise> retValue    = new List <Enterprise>();
            bool            rented        = false;
            List <DateTime> datesBetween2 = new List <DateTime>();

            for (var date = sep.RentFrom; date <= sep.RentTo; date = date.AddDays(1))
            {
                datesBetween2.Add(date);
            }
            foreach (var enterprise in temp)
            {
                if (sep.EnterpriseName != "" && sep.EnterpriseName != null)
                {
                    if (enterprise.Name == sep.EnterpriseName || enterprise.Name.ToLower() == sep.EnterpriseName.ToLower())
                    {
                        if (sep.BranchLocation != "" && sep.BranchLocation != null)
                        {
                            var found = 0;
                            foreach (var branch in enterprise.Branches)
                            {
                                if (branch.City.ToLower() == sep.BranchLocation.ToLower() || enterprise.Address.City.ToLower() == sep.BranchLocation.ToLower())
                                {
                                    found++;
                                    break;
                                }
                            }
                            if (found != 0)
                            {
                                foreach (var car in enterprise.Cars)
                                {
                                    List <DateTime> datesBetween = new List <DateTime>();

                                    foreach (var reservation in car.Reservations)
                                    {
                                        for (var date = reservation.DateFrom; date <= reservation.DateTo; date = date.AddDays(1))
                                        {
                                            datesBetween.Add(date);
                                        }
                                    }

                                    foreach (var date1 in datesBetween)
                                    {
                                        foreach (var date2 in datesBetween2)
                                        {
                                            if (date1 == date2)
                                            {
                                                rented = true;
                                            }
                                        }
                                    }


                                    if (rented)
                                    {
                                        rented = false;
                                        continue;
                                    }
                                    else
                                    {
                                        retValue.Add(enterprise);
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            foreach (var car in enterprise.Cars)
                            {
                                List <DateTime> datesBetween = new List <DateTime>();

                                foreach (var reservation in car.Reservations)
                                {
                                    for (var date = reservation.DateFrom; date <= reservation.DateTo; date = date.AddDays(1))
                                    {
                                        datesBetween.Add(date);
                                    }
                                }

                                foreach (var date1 in datesBetween)
                                {
                                    foreach (var date2 in datesBetween2)
                                    {
                                        if (date1 == date2)
                                        {
                                            rented = true;
                                        }
                                    }
                                }

                                if (rented)
                                {
                                    rented = false;
                                    continue;
                                }
                                else
                                {
                                    retValue.Add(enterprise);
                                    break;
                                }
                            }
                        }
                    }
                }
                else if (sep.BranchLocation != "" && sep.BranchLocation != null)
                {
                    var found = 0;
                    foreach (var branch in enterprise.Branches)
                    {
                        if (branch.City.ToLower() == sep.BranchLocation.ToLower() || enterprise.Address.City.ToLower() == sep.BranchLocation.ToLower())
                        {
                            found++;
                            break;
                        }
                    }
                    if (found != 0)
                    {
                        foreach (var car in enterprise.Cars)
                        {
                            List <DateTime> datesBetween = new List <DateTime>();

                            foreach (var reservation in car.Reservations)
                            {
                                for (var date = reservation.DateFrom; date <= reservation.DateTo; date = date.AddDays(1))
                                {
                                    datesBetween.Add(date);
                                }
                            }

                            foreach (var date1 in datesBetween)
                            {
                                foreach (var date2 in datesBetween2)
                                {
                                    if (date1 == date2)
                                    {
                                        rented = true;
                                    }
                                }
                            }

                            if (rented)
                            {
                                rented = false;
                                continue;
                            }
                            else
                            {
                                retValue.Add(enterprise);
                                break;
                            }
                        }
                    }
                }
                else
                {
                    foreach (var car in enterprise.Cars)
                    {
                        List <DateTime> datesBetween = new List <DateTime>();

                        foreach (var reservation in car.Reservations)
                        {
                            for (var date = reservation.DateFrom; date <= reservation.DateTo; date = date.AddDays(1))
                            {
                                datesBetween.Add(date);
                            }
                        }

                        foreach (var date1 in datesBetween)
                        {
                            foreach (var date2 in datesBetween2)
                            {
                                if (date1 == date2)
                                {
                                    rented = true;
                                }
                            }
                        }

                        if (rented)
                        {
                            rented = false;
                            continue;
                        }
                        else
                        {
                            retValue.Add(enterprise);
                            break;
                        }
                    }
                }
            }

            return(retValue);
        }