Пример #1
0
        public async Task UpdateCityAsync(int id, CityUpdateDto cityUpdateDto)
        {
            var existingCity = await _citiesRepository.FindByIdAsync(id);

            if (existingCity == null)
            {
                throw new ObjectNotFoundException($"City with id {id} does not exist");
            }
            await _citiesRepository.UpdateAsync(id, _mapper.Map <City>(cityUpdateDto));
        }
Пример #2
0
        /// <summary>
        /// Add or Edit an existing city
        /// </summary>
        /// <param name="cityId"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <Response> AddOrEditCityAsync(int cityId, CreateCityRequest request, CancellationToken cancellationToken = default)
        {
            var responseModel = new Response();

            if (await _citiesRepo.CityNameExists(request.CountryId, request.Name))
            {
                responseModel.AddError(ExceptionCreator.CreateBadRequestError("city", "city name does already exist"));
                return(responseModel);
            }

            if (cityId != 0)
            {
                var city = await _dbContext.Cities.FirstOrDefaultAsync(c => c.Id == cityId);

                try
                {
                    if (city != null)
                    {
                        city.CountryId      = request.CountryId;
                        city.Name           = request.Name;
                        city.LastModifiedBy = _httpContextAccessor.HttpContext.User.Identity.Name;
                        city.LastModifiedOn = DateTime.UtcNow;

                        await _citiesRepo.UpdateAsync(city);
                    }
                    else
                    {
                        responseModel.AddError(ExceptionCreator.CreateNotFoundError(nameof(city), $"city of Id: { cityId} not found"));
                        return(responseModel);
                    }
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }
            else
            {
                try
                {
                    await _citiesRepo.AddAsync(CreateCity(request));
                }
                catch (Exception ex)
                {
                    responseModel.AddError(ExceptionCreator.CreateInternalServerError(ex.ToString()));
                }
            }

            return(responseModel);
        }