public List <Pet> SearchForPet(FilterModel filter)
        {
            string searchValue  = filter.SearchValue;
            string searchString = filter.SearchTerm.ToLower();

            switch (searchString)
            {
            case "name":
                return(_petRepo.FindPetByName(searchValue).ToList());

            case "type":
                int            theSearch;
                List <PetType> petTypes = null;
                if (int.TryParse(searchValue, out theSearch) && theSearch != 0)
                {
                    petTypes = _petTypeRepo.FindPetTypeById(theSearch);
                    if (petTypes.Count < 1)
                    {
                        throw new Exception(message: "We couldn't find the id of the Pet Type");
                    }
                    else
                    {
                        return(_petTypeRepo.FindAllPetsByType(petTypes[0]));
                    }
                }
                else
                {
                    petTypes = _petTypeRepo.FindPetTypeByName(searchValue);
                    if (petTypes.Count < 1)
                    {
                        throw new Exception(message: "We couldn't find name of that Pet Type");
                    }
                    else
                    {
                        List <Pet> allPetsByType = null;
                        foreach (var thePetType in petTypes)
                        {
                            if (allPetsByType == null)
                            {
                                allPetsByType = _petTypeRepo.FindAllPetsByType(thePetType);
                            }
                            else
                            {
                                allPetsByType = allPetsByType.Concat(_petTypeRepo.FindAllPetsByType(thePetType)).ToList();
                            }
                        }
                        return(allPetsByType);
                    }
                }

            case "birthday":
                DateTime dateValue = DateTime.Now;
                if (DateTime.TryParse(searchValue, out dateValue))
                {
                    return(_petRepo.SearchPetsBytBirthday(dateValue).ToList());
                }
                else
                {
                    throw new Exception(message: "Sure you put the correct date? Cause the program's not!");
                }

            case "solddate":
                DateTime soldValue = DateTime.Now;
                if (DateTime.TryParse(searchValue, out soldValue))
                {
                    return(_petRepo.FindPetsBySoldDate(soldValue).ToList());
                }
                else
                {
                    throw new Exception(message: "Congrats, you made another error! You win a prize: NOT");
                }

            case "previousOwner":
                return(_petRepo.FindPetsByFormerOwner(searchValue).ToList());

            case "owner":
                int          searchForOwner;
                List <Owner> petOwner = null;
                if (int.TryParse(searchValue, out searchForOwner) && searchForOwner != 0)
                {
                    petOwner = _ownerRepo.FindOwnerById(searchForOwner);
                    if (petOwner.Count < 1)
                    {
                        throw new Exception(message: "Try writing even more like a toddler, the program can't read baby. You made a mistake, no owner with that id!");
                    }
                    else
                    {
                        return(_ownerRepo.FindAllPetsByOwner(petOwner[0]));
                    }
                }
                else
                {
                    petOwner = _ownerRepo.FindOwnerByName(searchValue).ToList();
                    if (petOwner.Count < 1)
                    {
                        throw new Exception(message: "Try writing even more like a toddler, the program can't read baby. You made a mistake, no owner with that name!");
                    }
                    else
                    {
                        List <Pet> allPetsByOwners = null;
                        foreach (var owner in petOwner)
                        {
                            if (allPetsByOwners == null)
                            {
                                allPetsByOwners = _ownerRepo.FindAllPetsByOwner(owner);
                            }
                            else
                            {
                                allPetsByOwners.Concat(_ownerRepo.FindAllPetsByOwner(owner));
                            }
                        }
                        return(allPetsByOwners);
                    }
                }

            case "price":
                long priceValue = 0;
                if (long.TryParse(searchValue, out priceValue))
                {
                    return(_petRepo.FindPetsByPrice(priceValue).ToList());
                }
                else
                {
                    throw new Exception(message: "Forgetting something? Maybe actually putting in a price!");
                }

            case "id":
                int searchPetId;
                if (int.TryParse(searchValue, out searchPetId) && searchPetId != 0)
                {
                    return(_petRepo.FindPetById(searchPetId));
                }
                else
                {
                    throw new Exception(message: "Nope, that Id does not exist");
                }

            default:
                throw new Exception(message: "No property for this");
            }
        }
 public List <Pet> FindAllPetsByType(PetType theType)
 {
     return(_petTypeRepo.FindAllPetsByType(theType));
 }
Exemple #3
0
 private List <Pet> FindAllPetsByPetType(PetType desiredPetType)
 {
     return(_petTypeRepo.FindAllPetsByType(desiredPetType));
 }
        //Searches for pets, for owner and type both id, and name are valid searches.
        public List <Pet> SearchForPet(FilterModel filter)
        {
            string searchValue    = filter.SearchValue.ToLower();
            string toSearchString = filter.SearchTerm.ToLower();

            switch (toSearchString)
            {
            case "name":
                return(_petRepo.FindPetsByName(searchValue, filter).ToList());

            case "color":
                return(_petRepo.FindPetsByColor(filter).ToList());

            case "type":
                int            theSearch;
                List <PetType> thePetType = null;
                if (int.TryParse(searchValue, out theSearch) && theSearch != 0)
                {
                    thePetType = _petTypeRepo.FindPetTypeById(theSearch);
                    if (thePetType.Count < 1)
                    {
                        throw new Exception(message: "Sorry could not find id of that PetType.");
                    }
                    else
                    {
                        return(_petTypeRepo.FindAllPetsByType(thePetType[0], filter));
                    }
                }
                else
                {
                    thePetType = _petTypeRepo.FindPetTypeByName(searchValue);
                    if (thePetType.Count < 1)
                    {
                        throw new Exception(message: "Sorry could not find name of that PetType.");
                    }
                    else
                    {
                        IEnumerable <Pet> allPetsByType = null;
                        foreach (var petType in thePetType)
                        {
                            if (allPetsByType == null)
                            {
                                allPetsByType = _petTypeRepo.FindAllPetsByType(petType);
                            }
                            else
                            {
                                allPetsByType = allPetsByType.Concat(_petTypeRepo.FindAllPetsByType(petType));
                            }
                        }
                        if (filter.CurrentPage != 0 && filter.ItemsPrPage != 0)
                        {
                            allPetsByType = allPetsByType.Skip((filter.CurrentPage - 1) * filter.ItemsPrPage).Take(filter.ItemsPrPage);
                        }
                        return(allPetsByType.ToList());
                    }
                }

            case "birthday":
                DateTime theDateValue = DateTime.Now;
                if (DateTime.TryParse(searchValue, out theDateValue))
                {
                    return(_petRepo.SearchPetsByBirthYear(theDateValue, filter).ToList());
                }
                else
                {
                    throw new InvalidDataException(message: "You have not entered a valid date.");
                }

            case "solddate":
                DateTime theSoldValue = DateTime.Now;
                if (DateTime.TryParse(searchValue, out theSoldValue))
                {
                    return(_petRepo.FindPetsBySoldDate(theSoldValue, filter).ToList());
                }
                else
                {
                    throw new InvalidDataException(message: "You have not entered a valid date.");
                }

            case "previousowner":
                return(_petRepo.FindPetsByPreviousOwner(searchValue, filter).ToList());

            case "owner":
                int          theSearchForOwner;
                List <Owner> thePetOwner = null;
                if (int.TryParse(searchValue, out theSearchForOwner) && theSearchForOwner != 0)
                {
                    thePetOwner = _ownerRepo.FindOwnerByID(theSearchForOwner);
                    if (thePetOwner.Count < 1 || thePetOwner == null)
                    {
                        throw new Exception(message: "Sorry could not find id of that PetType.");
                    }
                    else
                    {
                        return(_ownerRepo.FindAllPetsByOwner(thePetOwner[0], filter));
                    }
                }
                else
                {
                    thePetOwner = _ownerRepo.FindOwnerByName(searchValue).ToList();
                    if (thePetOwner.Count < 1 || thePetOwner == null)
                    {
                        throw new Exception(message: "Sorry could not find name of that Owner.");
                    }
                    else
                    {
                        IEnumerable <Pet> allPetsByTheOwners = null;
                        foreach (var owner in thePetOwner)
                        {
                            if (allPetsByTheOwners == null)
                            {
                                allPetsByTheOwners = _ownerRepo.FindAllPetsByOwner(owner);
                            }
                            else
                            {
                                allPetsByTheOwners = allPetsByTheOwners.Concat(_ownerRepo.FindAllPetsByOwner(owner));
                            }
                        }
                        if (filter.CurrentPage != 0 && filter.ItemsPrPage != 0)
                        {
                            allPetsByTheOwners = allPetsByTheOwners.Skip((filter.CurrentPage - 1) * filter.ItemsPrPage).Take(filter.ItemsPrPage);
                        }
                        return(allPetsByTheOwners.ToList());
                    }
                }

            case "price":
                long thePriceValue = 0;
                if (long.TryParse(searchValue, out thePriceValue))
                {
                    return(_petRepo.FindPetsByPrice(thePriceValue, filter).ToList());
                }
                else
                {
                    throw new InvalidDataException(message: "You have not entered a valid price.");
                }

            case "id":
                int searchId;
                if (int.TryParse(searchValue, out searchId) && searchId != 0)
                {
                    return(_petRepo.FindPetByID(searchId));
                }
                else
                {
                    throw new InvalidDataException(message: "Id out of bounds.");
                }


            default:
                throw new InvalidDataException(message: "I don't have that property to seach for.");
            }
        }