public IActionResult Create(int userId, [FromBody] AnimalCreationDTO animalCreation)
        {
            //Check object states
            if (animalCreation == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                animalTypeManager.GetAnimalTypeById(animalCreation.AnimalTypeId);
            }
            catch (InvalidOperationException)
            {
                return(NotFound($"AnimalType: {animalCreation.AnimalTypeId} was not found"));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }

            //Check if User exists
            try
            {
                userManager.GetUserById(userId);
            }
            //Manager will throw exceptions to manage responses
            catch (InvalidOperationException)
            {
                return(NotFound($"User: {userId} was not found"));
            }

            catch (Exception)
            {
                return(StatusCode(500));
            }


            var returnValue = animalManager.CreateAnimal(userId, animalCreation);

            return(CreatedAtRoute("GetAnimalById", new { userId, animalId = returnValue.Id }, returnValue));
        }
        public void CreateAnimal_ShouldCreateAnimal_WhenValidDTO()
        {
            AnimalCreationDTO animalCreationDTO = new AnimalCreationDTO()
            {
                Name         = "Gareth",
                AnimalTypeId = 1
            };

            AnimalDTO animalDTO = animalManager.CreateAnimal(1, animalCreationDTO);

            //Check if the model is valid
            Assert.IsTrue(HelperMethods.CheckModelValidation(animalDTO));
            Assert.AreEqual(animalCreationDTO.Name, animalDTO.Name);
            Assert.AreEqual(animalCreationDTO.AnimalTypeId, animalDTO.AnimalTypeId);
            Assert.AreEqual(mockAnimals.Count, animalDTO.Id);

            //Should be default Value
            Assert.AreEqual(HelperMethods.DEFAULT_HAPPINESS, animalDTO.Happiness);
            Assert.AreEqual(HelperMethods.DEFAULT_HUNGER, animalDTO.Hunger);
        }
Exemplo n.º 3
0
 public IHttpActionResult Post([FromBody] Animal value)
 {
     _animalManager.CreateAnimal(value);
     // could set the Id here in the retrun content
     return(Created <Animal>(Request.RequestUri, value));
 }
Exemplo n.º 4
0
        public Animal CreateAnimal(Animal animal)
        {
            Animal createAnimal = animalManager.CreateAnimal(animal);

            return(createAnimal);
        }