Esempio n. 1
0
        /// <summary>
        /// Finds an animal by its id and then updates it with the model
        /// parameter. If the animal can not be found the method throws
        /// a 404 Not Found.
        /// </summary>
        /// <param name="model"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <int> UpdateAnimal(AnimalBm model, int id)
        {
            var animal = await GetAnimalData(id);

            // will be better to put in constructor of DTO or Bm
            animal.UpdatedAt = DateTime.Now;

            if (animal != null)
            {
                animal = mapper.Map(model, animal);
                context.Update(animal);
                await context.SaveChangesAsync();

                var animalLocation = mapper.Map <LocationBm>(model);
                animalLocation.AnimalId = animal.Id;
                await locationRepository.UpdateLocation(animalLocation);

                logger.LogInfo($"Animal with id {id} successfully updated.");
                return(id);
            }
            else
            {
                throw new NotFoundException($"Animal with id {id} does not exist.");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Updates animal's location. This method gets called on
        /// animal update.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task UpdateLocation(LocationBm model)
        {
            var animalLocation = await context.Locations.FirstOrDefaultAsync(x => x.AnimalId == model.AnimalId);

            if (animalLocation != null)
            {
                mapper.Map(model, animalLocation);
                context.Update(animalLocation);
                await context.SaveChangesAsync();
            }
            else
            {
                throw new NotFoundException($"Animal location does not exist");
            }
        }