Beispiel #1
0
        public async Task EditAddressAsyncShouldThrowExceptionIfCityIsNull()
        {
            var option = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                         .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            var dbContext = new MyCalisthenicAppDbContext(option);

            var addressService = new AddressesService(dbContext, null);

            var city = new City
            {
                Id       = Guid.NewGuid().ToString(),
                Name     = CityName,
                PostCode = CityPostCode,
            };

            await dbContext.Cities.AddAsync(city);

            await dbContext.SaveChangesAsync();

            var addressViewModel = new AddressAdminEditViewModel
            {
                CityId = Guid.NewGuid().ToString(),
            };

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await addressService.EditAddressAsync(addressViewModel));

            Assert.IsType <ArgumentNullException>(exception);
        }
Beispiel #2
0
        public async Task EditAddressAsyncShouldThrowExceptionIfCityIdDoesNotExists()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            var addressService = new AddressesService(dbContext, null);

            var city = new City
            {
                Id       = Guid.NewGuid().ToString(),
                Name     = CityName,
                PostCode = CityPostCode,
            };

            await dbContext.Cities.AddAsync(city);

            await dbContext.SaveChangesAsync();

            var user = new ApplicationUser
            {
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var address = new Address
            {
                Country = AddressCountryName,
                Street  = AddressStreet,
                CityId  = city.Id,
                UserId  = user.Id,
            };

            await dbContext.Addresses.AddAsync(address);

            await dbContext.SaveChangesAsync();

            var addressViewModel = new AddressAdminEditViewModel
            {
                Id      = address.Id,
                CityId  = CityId,
                Country = AddressEditCountryName,
                UserId  = user.Id,
            };

            var exception = await Assert.ThrowsAsync <ArgumentNullException>(async() => await addressService.EditAddressAsync(addressViewModel));

            Assert.IsType <ArgumentNullException>(exception);
        }
Beispiel #3
0
        public async Task EditAddressAsyncShouldEditAddressSuccessfully()
        {
            var options = new DbContextOptionsBuilder <MyCalisthenicAppDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
                          .Options;

            var dbContext = new MyCalisthenicAppDbContext(options);

            var addressService = new AddressesService(dbContext, null);

            var city = new City
            {
                Id       = Guid.NewGuid().ToString(),
                Name     = CityName,
                PostCode = CityPostCode,
            };

            await dbContext.Cities.AddAsync(city);

            await dbContext.SaveChangesAsync();

            var user = new ApplicationUser
            {
                FirstName = UserFirstName,
                LastName  = UserLastName,
            };

            await dbContext.Users.AddAsync(user);

            await dbContext.SaveChangesAsync();

            var address = new Address
            {
                Country = AddressCountryName,
                Street  = AddressStreet,
                CityId  = city.Id,
                UserId  = user.Id,
            };

            await dbContext.Addresses.AddAsync(address);

            await dbContext.SaveChangesAsync();

            var addressViewModel = new AddressAdminEditViewModel
            {
                Id      = address.Id,
                CityId  = city.Id,
                Country = AddressEditCountryName,
                UserId  = user.Id,
            };

            await addressService.EditAddressAsync(addressViewModel);

            Assert.Equal(address.Country, addressViewModel.Country);
        }
        public async Task <IActionResult> Edit(AddressAdminEditViewModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            await this.addressesService.EditAddressAsync(inputModel);

            return(this.RedirectToAction("Addresses", "Users", new { id = inputModel.UserId }));
        }
Beispiel #5
0
        public async Task EditAddressAsync(AddressAdminEditViewModel inputModel)
        {
            if (!this.dbContext.Users.Any(u => u.Id == inputModel.UserId))
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidUserId, inputModel.UserId));
            }

            var city = await this.dbContext.Cities
                       .Where(c => c.Id == inputModel.CityId)
                       .FirstOrDefaultAsync();

            var address = await this.dbContext.Addresses
                          .FirstOrDefaultAsync(a => a.Id == inputModel.Id);

            if (city == null || !this.dbContext.Cities.Any(c => c.Id == inputModel.CityId))
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidCityId, inputModel.CityId));
            }

            if (address == null)
            {
                throw new ArgumentNullException(string.Format(ServicesConstants.InvalidAddressId, inputModel.Id));
            }

            address.IsDeleted = inputModel.IsDeleted;

            address.DeletedOn = inputModel.DeletedOn;

            address.CreatedOn = inputModel.CreatedOn;

            address.ModifiedOn = inputModel.ModifiedOn;

            address.Street = inputModel.Street;

            address.Country = inputModel.Country;

            address.Description = inputModel.Description;

            address.BuildingNumber = inputModel.BuildingNumber;

            address.CityId = inputModel.CityId;

            address.UserId = inputModel.UserId;

            address.City = city;

            this.dbContext.Update(address);

            await this.dbContext.SaveChangesAsync();
        }