public FilteringViewModel(FilteringItem model)
 {
     _model = model;
 }
示例#2
0
        public async Task <ActionResult <List <Animal> > > FilterAllAnimals([FromBody] FilteringItem filteringItem)
        {
            List <Animal> animals = new List <Animal>();

            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.GetAsync(_apiPath + "api/Animals"))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();

                    animals = JsonConvert.DeserializeObject <List <Animal> >(apiResponse);
                    foreach (var animal in animals)
                    {
                        Dictionary <string, bool> _getsAlongWith = new Dictionary <string, bool>()
                        {
                            { "Dogs", animal.FriendlyWithDogs },
                            { "Cats", animal.FriendlyWithCats },
                            { "Kids", animal.FriendlyWithKids }
                        };
                        var str = new List <string>();
                        foreach (KeyValuePair <string, bool> kv in _getsAlongWith)
                        {
                            if (kv.Value == true)
                            {
                                str.Add(kv.Key);
                            }
                        }
                        animal.GetsAlongWith = String.Join(",", str);
                    }
                }
            }
            if (filteringItem.Type[0] != "null")
            {
                foreach (var type in filteringItem.Type)
                {
                    animals = animals.Where(x => x.Type == type).ToList();
                }
            }
            if (filteringItem.Breed[0] != "false")
            {
                animals = animals.Where(x => x.Breed != "Mixed" || x.Breed != "mixed" || x.Breed != "mix" || x.Breed != "Mix" || x.Breed != "corcitura").ToList();
            }
            if (filteringItem.Age[0] != "null")
            {
                var animalsByAge  = new List <Animal>();
                var filteredByAge = new List <Animal>();
                foreach (var age in filteringItem.Age)
                {
                    animalsByAge = animals.Where(x => x.Age == age).ToList();

                    foreach (var animal in animalsByAge)
                    {
                        filteredByAge.Add(animal);
                    }
                }
                animals = filteredByAge;
            }
            if (filteringItem.EnergyLevel[0] != "null")
            {
                var animalsByEnergy  = new List <Animal>();
                var filteredByEnergy = new List <Animal>();
                foreach (var energy in filteringItem.EnergyLevel)
                {
                    animalsByEnergy = animals.Where(x => x.EnergyLevel == energy).ToList();

                    foreach (var animal in animalsByEnergy)
                    {
                        filteredByEnergy.Add(animal);
                    }
                }
                animals = filteredByEnergy;
            }
            if (filteringItem.Size[0] != "null")
            {
                var animalsBySize  = new List <Animal>();
                var filteredBySize = new List <Animal>();
                foreach (var size in filteringItem.Size)
                {
                    animalsBySize = animals.Where(x => x.Size == size).ToList();

                    foreach (var animal in animalsBySize)
                    {
                        filteredBySize.Add(animal);
                    }
                }
                animals = filteredBySize;
            }
            if (filteringItem.Gender[0] != "null")
            {
                foreach (var gender in filteringItem.Gender)
                {
                    animals = animals.Where(x => x.Gender == gender).ToList();
                }
            }
            if (filteringItem.FriendlyWithDogs[0] != "false")
            {
                animals = animals.Where(x => x.FriendlyWithDogs == true).ToList();
            }
            if (filteringItem.FriendlyWithCats[0] != "false")
            {
                animals = animals.Where(x => x.FriendlyWithCats == true).ToList();
            }
            if (filteringItem.FriendlyWithKids[0] != "false")
            {
                animals = animals.Where(x => x.FriendlyWithDogs == true).ToList();
            }
            if (filteringItem.SpecialNeeds[0] != "false")
            {
                animals = animals.Where(x => x.SpecialNeeds == true).ToList();
            }
            filteredAnimals = animals;

            return(Ok(animals));
        }