public async Task AddAsync_Should_Add_City() { var city = City.Builder() .SetId(Guid.NewGuid()) .SetRowVersion(Array.Empty <byte>()) .SetName("CityRepositoryTestAddAsync") .SetPolishName("CityRepositoryTestAddAsync") .SetStateId(_stateEntity.Id) .Build(); var cityEntity = new CityEntity { Id = city.Id, Name = city.Name, PolishName = city.PolishName, RowVersion = city.RowVersion.ToArray(), StateId = city.StateId }; _mapperMock.Setup(x => x.Map <City, CityEntity>(It.IsAny <City>())) .Returns(cityEntity); _memoryCacheMock.Setup(x => x.Remove(It.IsAny <object>())); Func <Task> result = async() => await _repository.AddAsync(city); await result.Should().NotThrowAsync <Exception>(); var addedCity = await _dbContext.Cities.FindAsync(city.Id); addedCity.Should().NotBeNull(); _memoryCacheMock.Verify(x => x.Remove(It.Is <object>(key => ReferenceEquals(key, CacheKeys.CitiesKey)))); }
public async Task HandleAsync(CreateCityCommand command, CancellationToken cancellationToken = default) { await CheckStateExistenceAsync(command.StateId); await CheckDuplicateNamesAsync(command.Name, command.PolishName, command.StateId); var city = _mapper.Map <CreateCityCommand, City>(command); await _cityRepository.AddAsync(city); }
public async Task AddAsync(string name) { var city = await _cityRepository.GetAsync(name); if (city != null) { throw new Exception($"City {city} already exists."); } city = new City(name); await _cityRepository.AddAsync(city); }
/// <summary> /// Save weather data to DB /// </summary> public async Task <bool> SaveWeatherDataAsync(List <DbCity> cities) { try { var existedCitiesDb = await _cityRepository.GetExistedCitiesAsync(cities.Select(c => c.Name).ToList()); if (existedCitiesDb.Any()) { var existedCities = cities.Where(c => existedCitiesDb.Values.Contains(c.Name)).ToList(); foreach (var city in existedCities) { var existedCityId = existedCitiesDb.First(e => e.Value == city.Name).Key; EnrichCityWeatherInfo(city, existedCityId); } await _cityWeatherInfoRepository.AddMultipleAsync(existedCities.SelectMany(e => e.CityWeatherInfos) .ToList()); } var notExistedCities = existedCitiesDb.Any() ? cities.Where(c => !existedCitiesDb.Values.Contains(c.Name)).ToList() : cities; if (!notExistedCities.Any()) { return(true); } foreach (var city in notExistedCities) { var addedCityId = await _cityRepository.AddAsync(city); if (addedCityId != CityParameters.AddCityErrorId) { EnrichCityWeatherInfo(city, addedCityId); } } await _cityWeatherInfoRepository.AddMultipleAsync(notExistedCities.SelectMany(n => n.CityWeatherInfos) .Where(n => n.CityId != CityParameters.AddCityErrorId) .ToList()); return(true); } catch (Exception e) { _logger.LogError($"WeatherSiteDataSaveService SaveWeatherDataAsync error - [{e}]"); return(false); } }
public async Task <IActionResult> Post(CityDto city) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var cityToInsert = _mapper.Map <City>(city); await _cityRepository.AddAsync(cityToInsert); await _unitOfWork.SaveChangesAsync(); return(Ok(cityToInsert)); }
public async Task SetCity(string city, string stateName) { var isCityExists = (await _cityRepo.GetByAsync(x => x.Name == city)) != null; if (!isCityExists) { var state = await _stateRepo.GetByAsync(x => x.Name == stateName || x.ShortName == stateName); if (state == null) { var newState = new State { Name = RemoveDigits(stateName), ShortName = RemoveDigits(stateName) }; var stateId = await _stateRepo.AddAsync(newState); newState.Id = stateId; await _cityRepo.AddAsync(new City { Name = RemoveDigits(city), State = newState }); } else { await _cityRepo.AddAsync(new City { Name = RemoveDigits(city), State = state }); } } }
public async Task <AddResult> AddAsync(City city) { CityEntity cityDal = _mapper.Map <CityEntity>(city); bool duplicate = await _cityRepository.CheckDuplicateAsync(cityDal); if (duplicate) { return(new AddResult(ResultTypes.Duplicate, null)); } int addedCityId = await _cityRepository.AddAsync(cityDal); return(new AddResult(ResultTypes.Ok, addedCityId)); }
public async Task <CityResponse> SaveAsync(City city) { try { await _cityRepository.AddAsync(city); await _unitOfWork.CompleteAsync(); return(new CityResponse(city)); } catch (Exception e) { return(new CityResponse($"An error ocurred while saving the city: {e.Message}")); } }
public async Task <SaveCityResponse> SaveAsync(City city) { try { await _cityRepository.AddAsync(city); await _unitOfWork.CompleteAsync(); return(new SaveCityResponse(city)); } catch (Exception ex) { // Do some logging stuff return(new SaveCityResponse($"An error occurred when saving theb{ex.Message}")); } }
public async Task <CityDto> Handle(CreateCityCommand request, CancellationToken cancellationToken) { var validator = new CreateCityCommandValidator(_cityRepository, _countryRepository); var validationResult = await validator.ValidateAsync(request); if (validationResult.Errors.Count > 0) { throw new ValidationException(validationResult); } var city = _mapper.Map <City>(request); city = await _cityRepository.AddAsync(city); var cityDto = _mapper.Map <CityDto>(city); return(cityDto); }
public async Task <CityResponse> SaveAsync(int provinceId, City city) { var existingProvince = await _provinceRepository.FindById(provinceId); if (existingProvince == null) { return(new CityResponse("Province not found, a city depent of a Province")); } try { city.ProvinceId = provinceId; await _cityRepository.AddAsync(city); await _unitOfWork.CompleteAsync(); return(new CityResponse(city)); } catch (Exception ex) { return(new CityResponse($"An error ocurred while saving city: {ex.Message}")); } }
public async Task <ResponseModel <CityResource> > SaveAsync(City city) { try { var existingCountry = await countryRepository.FindByIdAsync(city.CountryId); if (existingCountry == null) { return new ResponseModel <CityResource>() { Success = false, Message = "Invalid country." } } ; await cityRepository.AddAsync(city); await unitOfWork.CompleteAsync(); var cityResource = mapper.Map <City, CityResource>(city); return(new ResponseModel <CityResource>() { Success = true, Data = cityResource, Message = "Successfully added!" }); } catch (Exception ex) { return(new ResponseModel <CityResource>() { Success = false, Message = $"An error occurred when saving the city: { ex.Message}" }); } }
public async Task <ActionResult <Guid> > Post(City city) { await cityRepository.AddAsync(city); return(city.Id); }