Esempio n. 1
0
        public PetColor FindPetColorById(int id)
        {
            List <PetColor> petColors = _petColorRepo.FindPetColorById(id);

            if (petColors.Count != 1)
            {
                return(null);
            }
            else
            {
                return(petColors[0]);
            }
        }
Esempio n. 2
0
        // 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));
        }