Esempio n. 1
0
        /// <summary>
        /// Creates a new animal for a user based on the <c>AnimalRequest</c>.
        /// The enum value given there for the type of animal determines what kind of animal class is created
        /// </summary>
        /// <param name="animalRequest">the request that contains which type of animal and for which user.</param>
        /// <returns>the new <c>Animal</c> entity</returns>
        public Animal AddAnimal(AnimalRequest animalRequest)
        {
            assertUserExists(animalRequest);

            //TODO: better with string instead of enum numbers?
            Animal.AnimalType animalType = animalRequest.Type; //(Animal.AnimalType)Enum.ToObject(typeof(Animal.AnimalType), type);

            Animal newPet;

            switch (animalType)
            {
            case Animal.AnimalType.Cat:
                newPet = new Cat();
                break;

            case Animal.AnimalType.Dog:
                newPet = new Dog();
                break;

            case Animal.AnimalType.Parrot:
                newPet = new Parrot();
                break;

            default:
                throw new ArgumentException("Could not create specific animal type. Enum value is not handled.");
            }
            logger.LogInformation("Creating a new animal of type {0} for user {1}", new object[] { animalType.ToString(), animalRequest.UserId.ToString() });

            newPet.Name   = animalRequest.Name;
            newPet.UserId = animalRequest.UserId;

            databaseService.AddAnimal(newPet);
            return(newPet);
        }