Exemplo n.º 1
0
        public AnimalDTO GetAnimalById(int userId, int id)
        {
            Animal animal = animalRepository.GetAnimalById(id);

            if (userId != animal.UserId)
            {
                throw new ResourceNotOwnedException();
            }

            //Find the animalType associated with the animal
            AnimalTypeDTO animalType = animalTypeManager.GetAnimalTypeById(animal.AnimalTypeId);

            //Calculate the new animal state values
            animal = animalStateManager.CalculateAnimalState(animal, animalType);
            //Update the animal in the database
            animal = animalRepository.UpdateAnimal(animal);

            AnimalDTO returnValue = Mapper.Map <AnimalDTO>(animal);

            return(returnValue);
        }
        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));
        }
Exemplo n.º 3
0
        public IActionResult GetById(int id)
        {
            AnimalTypeDTO animalType = null;

            try
            {
                animalType = animalTypeManager.GetAnimalTypeById(id);
            }
            //Manager will throw exceptions to manage responses
            catch (InvalidOperationException)
            {
                return(NotFound($"{id} was not found"));
            }

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

            return(Ok(animalType));
        }