public async Task <IActionResult> AddSightToCity([FromBody] SightCreateDto sightCreateDto) { try { if (sightCreateDto == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await _sightService.CreateSightAsync(sightCreateDto); return(Ok()); } catch (ObjectNotFoundException ex) { _logger.LogInformation(ex.Message); return(NotFound(ex.Message)); } catch (Exception ex) { _logger.LogError(ex.Message); return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }
public async Task CreateSightAsync(SightCreateDto sightCreateDto) { var city = await _citiesRepository.FindByIdAsync(sightCreateDto.CityId); if (city == null) { throw new ObjectNotFoundException($"City with id {sightCreateDto.CityId} does not exist"); } if (city.Sights.FirstOrDefault(x => x.Name == sightCreateDto.Name) != null) { throw new ObjectConflictException($"City {city.Name} allready contains sight {sightCreateDto.Name}"); } await _sightsRepository.AddAsync(_mapper.Map <Sight>(sightCreateDto)); }
public async Task <ActionResult> Post(Guid townId, [FromBody] SightCreateDto sightCreateDto) { if (townId == null || sightCreateDto == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var sight = Mapper.Map <Sight>(sightCreateDto); await Repository.CreateSightAsync(townId, sight); await MailService.SendEmailAsync("Sight Created!", $"Sight {sight.Name} with id {sight.Id} was created"); var sightDto = Mapper.Map <SightDto>(sight); return(CreatedAtRoute("GetSight", new { townId, id = sightDto.Id }, sightDto)); }