Exemplo n.º 1
0
        public async Task AddAsycTest()
        {
            //Arrange
            InPortDbContext     context           = InPortContextFactory.Create();
            UnitOfWorkContainer unitwork          = new UnitOfWorkContainer(context);
            ICountryRepository  countryRepository = unitwork.Repository.CountryRepository;

            Guid    countryId = new Guid("A3C82D06-6A07-41FB-B7EA-903EC456BFC5");
            Country country2  = new Country("Colombia", "CO");

            country2.ChangeCurrentIdentity(countryId);

            //Act
            await countryRepository.AddAsync(country2);

            await unitwork.SaveChangesAsync();

            Country country = await countryRepository.SingleAsync(e => e.Id == countryId);

            //Assert
            country.ShouldNotBeNull();
            country.CountryName.ShouldBe("Colombia");
            country.CountryISOCode.ShouldBe("CO");
            country.Id.ShouldBe(countryId);
        }
Exemplo n.º 2
0
        public IActionResult CreateCountry([FromBody] CountryApiModelForCreation country)
        {
            if (country == null)
            {
                return(BadRequest());
            }

            var countryEntity = Mapper.Map <Country>(country);

            if (_countryRepository.AddAsync(countryEntity).Result == null)
            {
                throw new Exception("Creating an country failed on save.");
            }
            var countryToReturn = Mapper.Map <CountryApiModel>(countryEntity);

            return(CreatedAtRoute("GetCountry", new { id = countryToReturn.Id }, countryToReturn));
        }
Exemplo n.º 3
0
        public async Task <ActionResult <CountryDto> > AddCountry([FromBody] Country country)
        {
            await _repository.AddAsync(country);

            await _repository.CommitAsync();

            return(CreatedAtAction(nameof(GetCountry),
                                   new { id = country.CountryId },
                                   country.Adapt <CountryDto>()));
        }
Exemplo n.º 4
0
        public async void Post_Country_ShouldInvokeAddAsyncOnRepo()
        {
            //Arrange
            // A.CallTo(() => countryRepo.AddAsync(null)).Returns()
            var countryController = new CountryController(countryRepositoryMock);
            //Act
            var country = new Country("1", "Israel");
            await countryController.Post(country);

            //Assert
            A.CallTo(countryRepositoryMock.AddAsync(country)).MustHaveHappened();
        }
Exemplo n.º 5
0
        public async Task <CountryModel> AddAsync(CountryModel countryModel)
        {
            CountryEntity country = _mapper.Map <CountryEntity>(countryModel);

            bool countryDuplicate = await _countryRepository.CheckDuplicateAsync(country);

            if (countryDuplicate)
            {
                throw new InvalidDataException("This country already exists");
            }

            await _countryRepository.AddAsync(country);

            return(countryModel);
        }
Exemplo n.º 6
0
        public async Task <AddResult> AddAsync(Country country)
        {
            CountryEntity countryDal = _mapper.Map <CountryEntity>(country);

            bool duplicate = await _countryRepository.CheckDuplicateAsync(countryDal);

            if (duplicate)
            {
                return(new AddResult(ResultTypes.Duplicate, null));
            }

            int addedCountryId = await _countryRepository.AddAsync(countryDal);

            return(new AddResult(ResultTypes.Ok, addedCountryId));
        }
        public async Task <CountryDto> Handle(CreateCountryCommand request, CancellationToken cancellationToken)
        {
            var validator        = new CreateCountryCommandValidator(_countryRepository);
            var validationResult = await validator.ValidateAsync(request);

            if (validationResult.Errors.Count > 0)
            {
                throw new ValidationException(validationResult);
            }

            var country = _mapper.Map <Country>(request);

            country = await _countryRepository.AddAsync(country);

            var countryDto = _mapper.Map <CountryDto>(country);

            return(countryDto);
        }
Exemplo n.º 8
0
        public async Task AddRangeAsycTest()
        {
            //Arrange
            InPortDbContext     context           = InPortContextFactory.Create();
            UnitOfWorkContainer unitwork          = new UnitOfWorkContainer(context);
            ICountryRepository  countryRepository = unitwork.Repository.CountryRepository;

            Guid    countryId1 = new Guid("A3C82D06-6A07-41FB-B7EA-903EC456BFC5");
            Country country1   = new Country("Colombia", "CO");

            country1.ChangeCurrentIdentity(countryId1);

            Guid    countryId2 = new Guid("B3C82D06-6A07-41FB-B7EA-903EC456BFC5");
            Country country2   = new Country("Venezuela", "VZ");

            country2.ChangeCurrentIdentity(countryId2);

            List <Country> list = new List <Country>()
            {
                country1,
                country2
            };

            //Act
            await countryRepository.AddAsync(list);

            await unitwork.SaveChangesAsync();

            Country countryF1 = await countryRepository.SingleAsync(e => e.Id == countryId1);

            Country countryF2 = await countryRepository.SingleAsync(e => e.Id == countryId2);

            //Assert
            countryF1.ShouldNotBeNull();
            countryF1.CountryName.ShouldBe("Colombia");
            countryF1.CountryISOCode.ShouldBe("CO");
            countryF1.Id.ShouldBe(countryId1);

            countryF2.ShouldNotBeNull();
            countryF2.CountryName.ShouldBe("Venezuela");
            countryF2.CountryISOCode.ShouldBe("VZ");
            countryF2.Id.ShouldBe(countryId2);
        }
Exemplo n.º 9
0
        public override async Task <int> HandleCommand(InsertCountryCommand request, CancellationToken cancellationToken)
        {
            var id = 0;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        request.Model.CreatedDate  = DateTime.Now;
                        request.Model.CreatedBy    = request.LoginSession.Id;
                        request.Model.ModifiedDate = DateTime.Now;
                        request.Model.ModifiedBy   = request.LoginSession.Id;

                        id = await countryRepository.AddAsync(request.Model);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (id > 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try { trans.Rollback(); } catch { }
                        }
                    }
                }
            }

            return(id);
        }
Exemplo n.º 10
0
        private async Task SeedCountryAsync()
        {
            if (await _countryRepository.HasItemsAsync())
            {
                return;
            }
            try
            {
                await _countryRepository.AddAsync(new Country
                {
                    Id      = 1,
                    Name    = "Canada",
                    IsoCode = "CAN",
                    Status  = Constants.RecordStatus.Active
                });

                await _unitOfWork.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error in country seeding {ex}");
            }
        }
Exemplo n.º 11
0
        public async Task <ResponseModel <CountryResource> > SaveAsync(Country country)
        {
            try
            {
                await countryRepository.AddAsync(country);

                await unitOfWork.CompleteAsync();

                return(new ResponseModel <CountryResource>()
                {
                    Success = true,
                    Message = "Successfully added!"
                });
            }
            catch (Exception ex)
            {
                return(new ResponseModel <CountryResource>()
                {
                    Success = false,
                    Message = $"An error occurred when saving the country: {ex.Message}"
                });
            }
        }
Exemplo n.º 12
0
        public IActionResult CreateCountryCollection(
            [FromBody] IEnumerable <CountryApiModelForCreation> countryCollection)
        {
            if (countryCollection == null)
            {
                return(BadRequest());
            }

            var countryEntities = Mapper.Map <IEnumerable <Country> >(countryCollection);

            foreach (var country in countryEntities)
            {
                if (_countryRepository.AddAsync(country).Result == null)
                {
                    throw new Exception("Creating an country collection failed on save.");
                }
            }


            var countryCollectionToReturn = Mapper.Map <IEnumerable <CountryApiModel> >(countryEntities);
            var idsAsString = string.Join(",", countryCollectionToReturn.Select(a => a.Id));

            return(CreatedAtRoute("GetCountryCollection", new{ ids = idsAsString }, countryCollectionToReturn));
        }
Exemplo n.º 13
0
        public async Task <ActionResult <string> > Post(Country country)
        {
            await countryRepository.AddAsync(country);

            return(country.Id);
        }
 public async Task <Country> AddAsync(Country entity)
 {
     _cache.Remove(CountriesCacheKey);
     return(await _countryRepository.AddAsync(entity));
 }
        public async Task <IActionResult> Post(Country country)
        {
            var id = await _countryRepository.AddAsync(country);

            return(Created($"http://localhost:56190/api/DemoCRUD/{id}", id));
        }
 public async Task <Country> AddAsync(Country country)
 {
     return(await _countryRepository.AddAsync(country));
 }