Example #1
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");
            }
        }
Example #2
0
        /// <summary>
        /// Find an animal by its id and update its location
        /// </summary>
        /// <param name="location">object containing animalId and coordinates</param>
        /// <returns></returns>
        public async Task UpdateAnimalLocation(LocationBm location)
        {
            var animal = await GetAnimalData(location.AnimalId);

            if (animal != null)
            {
                animal.Location = mapper.Map <Location>(location);
                context.Update(animal);
                await context.SaveChangesAsync();
            }
            else
            {
                throw new NotFoundException($"Animal with id {location.AnimalId} does not exist.");
            }
        }
Example #3
0
        /// <summary>
        /// Creates a new location object for a specific
        /// animal and stores it in the database.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task CreateLocation(LocationBm model)
        {
            var animalLocation = await context.Locations.FirstOrDefaultAsync(x => x.AnimalId == model.AnimalId);

            if (animalLocation == null)
            {
                var location = mapper.Map <Location>(model);
                context.Locations.Add(location);
                await context.SaveChangesAsync();

                logger.LogInfo($"Location for animal {model.AnimalId} successfully added.");
            }
            else
            {
                throw new BadRequestException($"Animal with id {model.AnimalId} already has a location.");
            }
        }
Example #4
0
        public async Task <IActionResult> UpdateAnimalLocation([FromBody] LocationBm model)
        {
            await AnimalsService.UpdateAnimalLocation(model);

            return(Ok());
        }