public async Task <Country> UpdateCountryByIdAsync(int countryId, UpdateCountryRequest country) { var dbCountries = await _cntryCtx.Countries.Where(c => c.CountryId == countryId).ToArrayAsync(); if (dbCountries.Length < 1) { throw new IdNotFoundException("Such country not found"); } var updatedCountry = _mapper.Map <UpdateCountryRequest, CountryDTO>(country, dbCountries[0]); var result = await _cntryCtx.SaveChangesAsync(); if (result == 0) { throw new DuplicateFoundException("Can not update country with requested parameters"); } return(_mapper.Map <Country>(updatedCountry)); }
public async Task <Country> CreateCountryAsync(UpdateCountryRequest country) { var dbCountry = _mapper.Map <CountryDTO>(country); var added = _cntryCtx.Countries.Add(dbCountry); if (added is null) { throw new DuplicateFoundException("Can not add country with requested parameters"); } var result = await _cntryCtx.SaveChangesAsync(); if (result == 0) { throw new DuplicateFoundException("Can not add country with requested parameters"); } return(_mapper.Map <Country>(added)); }