Exemplo n.º 1
0
 public PetType GetPetTypeById(int id)
 {
     if (id < 1)
     {
         throw new InvalidDataException("ID cannot be lower than 1");
     }
     return _petTypeRepository.ReadPetTypeById(id);
 }
Exemplo n.º 2
0
        //Validates new pet objects
        public Pet ValidatePet(Pet petToValidate, bool validateId)
        {
            string errorMessage = "";

            if (validateId)
            {
                if (petToValidate.PetId < 1)
                {
                    errorMessage += "Pet ID must be a positive integer greater than zero\n";
                }
            }

            if (petToValidate.PreviousOwner == null || petToValidate.PreviousOwner.OwnerId < 1)
            {
                errorMessage += "Pet must have a previous owner and owner ID must be a positive integer greater than zero\n";
            }
            else
            {
                var foundOwner = _ownerRepository.ReadOwnerById(petToValidate.PreviousOwner.OwnerId);
                if (foundOwner == null)
                {
                    errorMessage += "No owner found with owner ID. Previous owner must be an existing owner\n";
                }
                else
                {
                    petToValidate.PreviousOwner = foundOwner;
                }
            }

            if (petToValidate.Type == null || petToValidate.Type.PetTypeId < 1)
            {
                errorMessage += "Pet must have a type and pet type ID must be a positive integer greater than zero\n";
            }
            else
            {
                var foundPetType = _petTypeRepository.ReadPetTypeById(petToValidate.Type.PetTypeId);
                if (foundPetType == null)
                {
                    errorMessage += "No pet type found with pet type ID. Type must be an existing pet type\n";
                }
                else
                {
                    petToValidate.Type = foundPetType;
                }
            }

            if (string.IsNullOrEmpty(petToValidate.Name) || petToValidate.Name.Length < 2)
            {
                errorMessage += "Name of pet must be at least two characters\n";
            }
            if (petToValidate.BirthDate == null || petToValidate.BirthDate == DateTime.MinValue || petToValidate.BirthDate > DateTime.Now)
            {
                errorMessage += "Pet must have a birth date which cannot be in the future\n";
            }
            //solddate is optional - maybe initialize as equal to birthdate instead of going to default value
            if (petToValidate.SoldDate != null && petToValidate.SoldDate != DateTime.MinValue && petToValidate.SoldDate < petToValidate.BirthDate)
            {
                errorMessage += "Last selling date of the pet cannot predate it's birth\n";
            }
            if (string.IsNullOrEmpty(petToValidate.Color) || petToValidate.Color.Length < 3)
            {
                errorMessage += "Name of color must be at least three characters\n";
            }
            if (petToValidate.Price < 0)
            {
                errorMessage += "Price of pet cannot be negative\n";
            }
            if (errorMessage.Length > 0)
            {
                throw new ArgumentException(errorMessage);
            }
            return(petToValidate);
        }