Exemplo n.º 1
0
        private async Task CheckCityExist(CityRequestModel requestModel, string id = null)
        {
            var existingCity = await _cityRepository.FirstOrDefaultAsync(city =>
                                                                         (string.IsNullOrEmpty(id) || city.Id != id) &&
                                                                         city.CountryId == requestModel.CountryId &&
                                                                         (city.Name == requestModel.Name || city.CityCode == requestModel.CityCode));

            if (existingCity != null)
            {
                string allreadyExistingData = existingCity.Name == requestModel.Name
                    ? requestModel.Name
                    : requestModel.CityCode;

                throw new ItemAlreadyExistsException(allreadyExistingData);
            }
        }
Exemplo n.º 2
0
        public async Task <CityDto> CreateAsync(CityRequestModel requestModel)
        {
            Country country = await _countryRepository.FirstOrDefaultAsync(c => c.Id == requestModel.CountryId);

            if (country == null)
            {
                throw new NotFoundException($"Country not found for Id:{requestModel.CountryId}");
            }

            await CheckCityExist(requestModel);

            var createdCity = _mapper.Map <City>(requestModel);

            createdCity.CountryCode = country.Iso2;

            return(_mapper.Map <CityDto>(await _cityRepository.AddAsync(createdCity)));
        }
Exemplo n.º 3
0
        public async Task <CityDto> UpdateAsync(string id, CityRequestModel requestModel)
        {
            if (!await _cityRepository.AnyAsync(city => city.Id == id))
            {
                throw new ItemNotFoundException(requestModel.Name);
            }

            if (!await _countryRepository.AnyAsync(country => country.Id == requestModel.CountryId))
            {
                throw new NotFoundException($"Country not found for Id:{requestModel.CountryId}");
            }

            await CheckCityExist(requestModel, id);

            var updatedCity = await _cityRepository.UpdateAsync(id, _mapper.Map <City>(requestModel));

            return(_mapper.Map <CityDto>(updatedCity));
        }
Exemplo n.º 4
0
 public ActionResult <City> Post([FromBody] CityRequestModel city)
 {
     try
     {
         City _city = new City
         {
             GeoLocation = city.Location,
             Name        = city.Name
         };
         using (var scope = container.BeginLifetimeScope())
         {
             using (var repository = scope.Resolve <ICityRepository>())
             {
                 _city = repository.Read(repository.Create(_city));
             }
         }
         return(_city);
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
Exemplo n.º 5
0
 public IActionResult Put([FromBody] CityRequestModel city)
 {
     try
     {
         City _city = new City
         {
             GeoLocation = city.Location,
             Name        = city.Name,
             Id          = city.Id
         };
         using (var scope = container.BeginLifetimeScope())
         {
             using (var repository = scope.Resolve <ICityRepository>())
             {
                 repository.Update(_city);
             }
         }
         return(Ok());
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
Exemplo n.º 6
0
 public virtual async Task <CityDto> UpdateAsync(string id, [FromBody] CityRequestModel request)
 => await _cityService.UpdateAsync(id, request);
Exemplo n.º 7
0
 public virtual async Task <CityDto> CreateAsync([FromBody] CityRequestModel request)
 => await _cityService.CreateAsync(request);
Exemplo n.º 8
0
        public async Task AddAsync(CityRequestModel cityModel)
        {
            await _context.AddAsync(cityModel);

            await _context.SaveChangesAsync();
        }