Exemple #1
0
        public PetType FindPetTypeById(int id)
        {
            List <PetType> allPetsWithID = _petTypeRepo.FindPetTypeById(id);

            if (allPetsWithID.Count != 1)
            {
                throw new Exception("Cannot find pet with that id, source of Error: HUmans!");
            }
            else
            {
                return(allPetsWithID[0]);
            }
        }
Exemple #2
0
        public Pettype CreateType(Pettype type)
        {
            if (_petTypeRepo.FindPetTypeById(type.Id) == null)
            {
                throw new InvalidDataException("Type not found");
            }

            if (type.TypeName == null)
            {
                throw new InvalidDataException("Type needs a name");
            }
            return(_petTypeRepo.AddType(type));
        }
        public PetType FindPetTypeById(int id)
        {
            List <PetType> alltheTypesWithID = _petTypeRepo.FindPetTypeById(id);

            if (alltheTypesWithID.Count != 1)
            {
                return(null);
            }
            else
            {
                return(alltheTypesWithID[0]);
            }
        }
        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");
            }
        }
        // Adds a new pet, Owner, Color and Type are added unless their ID is given, in which case they are found by Id.
        public Pet AddNewPet(Pet theNewPet)
        {
            List <PetType> theType = null;

            if (theNewPet.PetType.PetTypeId != 0)
            {
                theType = _petTypeRepo.FindPetTypeById(theNewPet.PetType.PetTypeId);
                if (theType.Count != 1)
                {
                    throw new Exception("Sorry can't find that type.");
                }
            }
            else
            {
                theType = new List <PetType> {
                    _petTypeRepo.AddNewPetType(theNewPet.PetType)
                };
            }

            if (theType.Count != 1)
            {
                throw new Exception(message: "Could not find the type.");
            }
            else
            {
                theNewPet.PetType = theType[0];
            }

            List <Owner> theOwners = null;

            if (theNewPet.PetOwner.OwnerId == 0)
            {
                theOwners = new List <Owner> {
                    _ownerRepo.AddNewOwner(theNewPet.PetOwner)
                };
            }
            else
            {
                theOwners = _ownerRepo.FindOwnerByID(theNewPet.PetOwner.OwnerId);
            }

            if (theOwners.Count != 1)
            {
                throw new Exception(message: "Could not find the right owner");
            }
            else
            {
                theNewPet.PetOwner = theOwners[0];
            }

            List <PetColor> theColors = null;

            foreach (var color in theNewPet.PetColor)
            {
                PetColor theColor;
                if (color.petColorId != 0)
                {
                    var allColors = _petColorRepo.FindPetColorById(color.petColorId);
                    if (allColors.Count() != 1)
                    {
                        throw new Exception(message: "Wrong nr of id's found");
                    }
                    else
                    {
                        theColor = allColors[0];
                    }
                }
                else
                {
                    theColor = _petColorRepo.AddNewPetColor(color.petColor);
                }
                if (theColors == null)
                {
                    theColors = new List <PetColor> {
                        theColor
                    };
                }
                else
                {
                    theColors.Add(theColor);
                }
            }
            List <PetColorPet> thePetColorPets = new List <PetColorPet>();

            foreach (var color in theColors)
            {
                thePetColorPets.Add(new PetColorPet {
                    petColor = color
                });
            }
            theNewPet.PetColor = thePetColorPets;

            return(_petRepo.AddNewPet(theNewPet));
        }