예제 #1
0
        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);
                }
            }
        }
예제 #2
0
        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);
            });
        }
예제 #3
0
        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);
                }
            }
        }
예제 #4
0
        public async Task <IActionResult> CreateCountry([FromBody] CreateCountryModel createCountryModel)
        {
            CreateCountryDto createCountryDto = _mapper.Map <CreateCountryDto>(createCountryModel);
            await _countryService.CreateCountryAsync(createCountryDto);

            return(Ok());
        }
예제 #5
0
        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);
        }
예제 #6
0
        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)));
            }
        }
예제 #7
0
        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));
        }
예제 #8
0
        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();
        }
예제 #9
0
        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)));
            }
        }
예제 #10
0
        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);
        }
예제 #11
0
        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());
        }
예제 #12
0
        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);
        }
예제 #14
0
        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);
 }