public async Task UpdateCountryAsync(int id, CreateCountryDto createCountryDto, string email) { using (unitOfWork) { var countryRepository = unitOfWork.CountryRepository; var country = await countryRepository.FindAsync(x => x.Id == id && x.Organization.User.Email == email); if (country == null) { throw new HttpStatusCodeException(404, $"Not found country with id \"{id}\" or user with email {email} don't have permisson to manage this organization"); } mapper.Map <CreateCountryDto, Country>(createCountryDto, country); try { countryRepository.Update(country); await unitOfWork.SaveAsync(); } catch (DALException ex) { throw new HttpStatusCodeException(400, ex.Message); } } }
public void UpdateCountry_Test() { UsingDbContext(async ctx => { //Arrange var Country = new CreateCountryDto { CountryCode = "TestCN", IsActive = false, CountryName = "中国测试", CountryShort = "TestCN", EnglishName = "TestChina" }; // Act var countryResult1 = await _countryAppService.Create(Country); var countryResult2 = await _countryAppService.Update(countryResult1); //Assert countryResult1.ShouldNotBe(null); countryResult2.ShouldNotBe(null); countryResult1.Id.ShouldBe(countryResult2.Id); countryResult1.CountryCode.ShouldBe(countryResult2.CountryCode); countryResult1.CountryName.ShouldNotBe(countryResult2.CountryName); countryResult1.EnglishName.ShouldNotBe(countryResult2.EnglishName); countryResult1.CountryShort.ShouldNotBe(countryResult2.CountryShort); countryResult1.IsActive.ShouldNotBe(countryResult2.IsActive); }); }
public async Task AddCountryAsync(int organizationId, CreateCountryDto createCountryDto, string email) { using (unitOfWork) { var organizationRepository = unitOfWork.OrganizationRepository; var countryRepository = unitOfWork.CountryRepository; var organization = await organizationRepository.FindAsync(x => x.Id == organizationId && x.User.Email == email); if (organization == null) { throw new HttpStatusCodeException(404, $"Not found organization with id \"{organizationId}\" or user with email {email} don't have permisson to manage this organization"); } var country = mapper.Map <CreateCountryDto, Country>(createCountryDto); country.Organization = organization; try { countryRepository.Add(country); await unitOfWork.SaveAsync(); } catch (DALException ex) { throw new HttpStatusCodeException(400, ex.Message); } } }
public async Task <IActionResult> CreateCountry([FromBody] CreateCountryModel createCountryModel) { CreateCountryDto createCountryDto = _mapper.Map <CreateCountryDto>(createCountryModel); await _countryService.CreateCountryAsync(createCountryDto); return(Ok()); }
public async Task <CountryDto> CreateCountry(CreateCountryDto pCountry) { var lCountryEntity = Mapping.Mapper.Map <CountryEntity>(pCountry); lCountryEntity = await this._countryRepository.Add(lCountryEntity); CountryDto lReturn = Mapping.Mapper.Map <CountryDto>(pCountry); lReturn.Id = lCountryEntity.Id; return(lReturn); }
public async Task <IActionResult> Create([FromForm] CreateCountryDto dto) { try { var country = await _countryService.Create(dto); return(Created("Ok", _mapper.Map <Country, CountryToReturn>(country))); } catch (Exception e) { return(BadRequest(new ApiException(StatusCodes.Status400BadRequest, e.Message))); } }
public async Task <IActionResult> CreateCountry([FromBody] CreateCountryDto countryDto) { if (!ModelState.IsValid) { _logger.LogError($"Invalid POST attempt in {nameof(CreateCountry)}"); return(BadRequest(ModelState)); } var country = _mapper.Map <Country>(countryDto); await _unitOfWork.Countries.Insert(country); await _unitOfWork.Save(); return(CreatedAtRoute("GetCountry", new { id = country.Id }, country)); }
public async Task CreateCountryAsync(CreateCountryDto createCountryDto) { if (createCountryDto == null) { throw new ArgumentNullException(nameof(createCountryDto)); } Country countryToBeCreated = new Country() { CountryName = createCountryDto.CountryName }; await _unitOfWork.Repository <Country>().InsertEntityAsync(countryToBeCreated); await _unitOfWork.SaveChangesAsync(); }
public async Task <IActionResult> Update([FromForm] CreateCountryDto dto) { try { var updated = await _countryService.UpdateByCode(dto); return(Ok(new ApiResponse(StatusCodes.Status200OK, "Created", _mapper.Map <Country, CountryToReturn>(updated)))); } catch (ModelNotFoundException e) { return(NotFound(new ApiException(StatusCodes.Status404NotFound, e.Message))); } catch (Exception e) { return(BadRequest(new ApiException(StatusCodes.Status400BadRequest, e.Message))); } }
public async Task <Country> Create(CreateCountryDto dto) { var country = await _countryRepo.GetAsync(dto.Code); if (!(country is null)) { throw new ApplicationException("Страна уже зарегистрированна"); } var newCountry = _mapper.Map <CreateCountryDto, Country>(dto); await _countryRepo.AddAsync(newCountry); await _countryRepo.SaveChangesAsync(); return(newCountry); }
public async Task <Result> CreateAsync(CreateCountryDto createCountryDto) { _logger.LogTrace("[CountryService:CreateAsync] Starting processing the command"); if (await _countryRepository.ExistsByCountryNameAsync(createCountryDto.Name)) { _logger.LogInformation( $"[CountryService:CreateAsync] Error: The country with name {createCountryDto.Name} already exists"); return(Result.Fail(new Error($"The country with name {createCountryDto.Name} already exists") .WithMetadata("errCode", "errCountryAlreadyExistsByName"))); } if (await _countryRepository.ExistsByCountryCodeAsync(createCountryDto.Code)) { _logger.LogInformation( $"[CountryService:CreateAsync] Error: The country with name {createCountryDto.Name} already exists"); return(Result.Fail(new Error($"The country with code {createCountryDto.Code} already exists") .WithMetadata("errCode", "errCountryAlreadyExistsByCode"))); } var country = new Country(createCountryDto.Name, createCountryDto.Code); _countryRepository.Add(country); var result = await _countryRepository.UnitOfWork.SaveEntitiesAsync(); if (!result) { _logger.LogInformation( "[CountryService:CreateAsync] Error: An error happened while trying to create the country"); return(Result.Fail(new Error("An error happened while trying to create the country") .WithMetadata("errCode", "errDbSaveFail"))); } var subject = "country"; _stanIntegrationEventBus.Publish(subject, "created", new CountryCreatedIntegrationEvent(country.Uuid, country.Name, country.Code)); return(Result.Ok()); }
public async Task <IActionResult> Create([FromBody] CreateCountryDto createCountryDto) { try { var result = await _countryCashedService.AddAsync(createCountryDto); if (result.Errors.Any()) { return(Conflict()); } RemoveRelatedCash(); return(Ok(result.Data)); } catch (Exception ex) { _logging.WriteExption(ex); return(BadRequest()); } }
public async Task CreateCountry() { this._countryRepository.Add(Arg.Any <CountryEntity>()).Returns(info => { var myEnt = info.Arg <CountryEntity>(); myEnt.Id = 40; return(myEnt); }); CreateCountryDto lCreateCountryDto = new CreateCountryDto() { Name = "England" }; var lNewlyCreatedCountryDto = await this.referenceServices.CreateCountry(lCreateCountryDto); Assert.AreEqual("England", lNewlyCreatedCountryDto.Name); Assert.AreEqual(40, lNewlyCreatedCountryDto.Id); }
public async Task <Country> UpdateByCode(CreateCountryDto countryDto) { var c = await _countryRepo.GetAsync(countryDto.Code); if (c is null) { throw new ApplicationException("Страна не найдена"); } ; var country = await _countryRepo.GetAsync(countryDto.Code); country.Code2 = countryDto.Code2; country.Code3 = countryDto.Code3; country.Name = countryDto.Name; country.FullName = countryDto.FullName; await _countryRepo.SaveChangesAsync(); return(country); }
public async Task CreateAsync(CreateCountryDto input) { var country = ObjectMapper.Map <Country>(input); await _countryRepository.InsertAsync(country); }