示例#1
0
        public PetType EditPetType(int idOfPetTypeToEdit, PetType editedPetType)
        {
            if (_petTypeRepository.SearchById(editedPetType.PetTypeId) == null)
            {
                throw new KeyNotFoundException("An petType with this ID does not exist");
            }

            if (editedPetType.Pets != null)
            {
                if (editedPetType.Pets.Count > 0)
                {
                    throw new InvalidDataException("You cant add pets to a petType like this, go ad an owner id to a pet instead");
                }
            }

            return(_petTypeRepository.EditPetType(idOfPetTypeToEdit, editedPetType));
        }
 public PetType SearchById(int id)
 {
     if (!_petTypeRepository.GetAllPetTypes().Exists(x => x.ID == id))
     {
         throw new KeyNotFoundException("No pets with this id exist");
     }
     else
     {
         return(_petTypeRepository.SearchById(id));
     }
 }
        public Pet AddPet(Pet pet)
        {
            Pet addedPet;



            if (pet.Equals(null))
            {
                throw new InvalidDataException("Pet cannot be null");
            }

            if (pet.Name.Length < 1)
            {
                throw new InvalidDataException("Pet name has to be longer than one");
            }

            if (pet.PetId != 0)
            {
                throw new InvalidDataException("A new pet cannot have an id, that is only for already existing pets");
            }

            if (pet.PetType == null)
            {
                throw new InvalidDataException("A pet has to have a petType");
            }

            if (pet.PetType != null)
            {
                if (_petTypeRepository.SearchById(pet.PetType.PetTypeId) == null)
                {
                    throw new InvalidDataException("The petType has to be an existing petType in the database");
                }
            }
            //if (pet.PetType != null)
            //{
            //    var petType = _petTypeRepository.SearchByIdWithoutRelations(pet.PetType.OwnerId);
            //    if (petType == null)
            //    {
            //        throw new InvalidDataException("The petType has to be an existing petType in the database");
            //    }

            //    pet.PetType = petType;

            //}
            //else
            //{
            //    throw new InvalidDataException("New Pet has to have a petType");
            //}

            //if (pet.Owner != null)
            //{
            //    var owner = _ownerRepository.SearchByIdWithoutRelations(pet.Owner.OwnerId);
            //    if (owner == null)
            //    {
            //        throw new InvalidDataException("The owner has to be an existing owner in the database");
            //    }

            //    pet.Owner = owner;

            //}


            addedPet = _petRepository.AddPet(pet);
            if (addedPet == null)
            {
                throw new DataBaseException("Something went wrong in the database");
            }

            return(addedPet);
        }