public Pet CreatePet(Pet pet)
        {
            //if (DateTime.TryParse(pet.BirthDate.ToString(CultureInfo.InvariantCulture), out var temp))
            //{
            //    pet.BirthDate = Convert.ToDateTime(temp.ToShortDateString());
            //}
            //else
            //{
            //    throw new InvalidDataException("Birthday is not a valid date.");
            //}

            if (!Regex.IsMatch(Convert.ToString(pet.Price, CultureInfo.InvariantCulture), @"^\d+$"))
            {
                throw new InvalidDataException("Price can only contain numbers.");
            }

            if (pet.PetName == null || pet.PetName.Any(char.IsDigit))
            {
                throw new InvalidDataException("Pet must have a name and it can not contain numbers.");
            }

            if (pet.Color == null || pet.Color.Any(char.IsDigit))
            {
                throw new InvalidDataException("Pet must have a color and it can not contain numbers.");
            }

            if (pet.Type == null || pet.Type.Any(char.IsDigit))
            {
                throw new InvalidDataException("Pet must have a type and it can not contain numbers.");
            }

            if (pet.BirthDate == null)
            {
                throw new InvalidDataException("Pet must have a birthday.");
            }

            return(_petShopRepository.CreatePet(pet));
        }
Beispiel #2
0
        public void CreatePet(Pet pet)
        {
            //Check of all of pet values
            if (string.IsNullOrEmpty(pet.name))
            {
                throw new InvalidDataException("Pet needs a name");
            }
            if (string.IsNullOrEmpty(pet.type))
            {
                throw new InvalidDataException("Pet need to specify the type of pet the pet is");
            }
            if (pet.birthDate == DateTime.MinValue)
            {
                throw new InvalidDataException("Pet needs a birth date");
            }
            if (pet.soldDate == DateTime.MinValue)
            {
                throw new InvalidDataException("Pet needs a sold date");
            }
            if (string.IsNullOrEmpty(pet.color))
            {
                throw new InvalidDataException("Pet needs a color");
            }
            if (pet.previousOwner == null || pet.previousOwner.id <= 0)
            {
                throw new InvalidDataException("To create a pet you need a previous owner");
            }
            if (_ownerRepository.OwnerFoundById(pet.previousOwner.id) == null)
            {
                throw new InvalidDataException("Previous owner not found");
            }
            if (pet.price == 0.0)
            {
                throw new InvalidDataException("Pet needs a price");
            }

            _petShopRepository.CreatePet(pet);
        }
 public Pet CreatePet(Pet pet)
 {
     return(_petShopRepository.CreatePet(pet));
 }