예제 #1
0
        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));
            }
        }
예제 #2
0
        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));
        }
예제 #3
0
        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));
        }