public async Task <int> UpdateAsync(int id, BankCashReceiptDetailModel model)
        {
            BankCashReceiptDetailModel exist = _dbContext.GarmentFinanceBankCashReceiptDetails
                                               .Include(d => d.Items)
                                               .Include(d => d.OtherItems)
                                               .Single(dispo => dispo.Id == id && !dispo.IsDeleted);

            foreach (var item in exist.Items)
            {
                BankCashReceiptDetailItemModel itemModel = model.Items.FirstOrDefault(prop => prop.Id.Equals(item.Id));

                if (itemModel == null)
                {
                    EntityExtension.FlagForDelete(item, _identityService.Username, UserAgent, true);
                }
                else
                {
                    item.Amount = itemModel.Amount;
                    EntityExtension.FlagForUpdate(item, _identityService.Username, UserAgent);
                }
            }

            foreach (var newItem in model.Items)
            {
                if (newItem.Id == 0)
                {
                    exist.Items.Add(newItem);
                    EntityExtension.FlagForCreate(newItem, _identityService.Username, UserAgent);
                }
            }

            foreach (var otherItem in exist.OtherItems)
            {
                BankCashReceiptDetailOtherItemModel itemModel = model.OtherItems.FirstOrDefault(prop => prop.Id.Equals(otherItem.Id));

                if (itemModel == null)
                {
                    EntityExtension.FlagForDelete(otherItem, _identityService.Username, UserAgent, true);
                }
                else
                {
                    otherItem.Amount     = itemModel.Amount;
                    otherItem.TypeAmount = itemModel.TypeAmount;
                    EntityExtension.FlagForUpdate(otherItem, _identityService.Username, UserAgent);
                }
            }

            foreach (var newOtherItem in model.OtherItems)
            {
                if (newOtherItem.Id == 0)
                {
                    exist.OtherItems.Add(newOtherItem);
                    EntityExtension.FlagForCreate(newOtherItem, _identityService.Username, UserAgent);
                }
            }

            EntityExtension.FlagForUpdate(exist, _identityService.Username, UserAgent);

            return(await _dbContext.SaveChangesAsync());
        }
Пример #2
0
        public async Task Should_Success_Update_Data()
        {
            //Arrange
            var dbContext           = GetDbContext(GetCurrentAsyncMethod());
            var serviceProviderMock = GetServiceProvider();

            var httpClientService = new Mock <IHttpClientService>();

            serviceProviderMock
            .Setup(serviceProvider => serviceProvider.GetService(typeof(IHttpClientService)))
            .Returns(httpClientService.Object);

            serviceProviderMock
            .Setup(serviceProvider => serviceProvider.GetService(typeof(FinanceDbContext)))
            .Returns(dbContext);

            var service = new BankCashReceiptDetailService(serviceProviderMock.Object);

            var model = await _dataUtil(service, GetCurrentAsyncMethod()).GetTestData();

            var model2 = await _dataUtil(service, GetCurrentAsyncMethod()).GetTestData();

            BankCashReceiptDetailModel newModel2 = new BankCashReceiptDetailModel();

            newModel2.Id = model2.Id;
            //Act
            var newModel = await service.ReadByIdAsync(model.Id);

            var Response1 = await service.UpdateAsync(newModel.Id, newModel);

            Assert.NotEqual(0, Response1);

            newModel2.Items = new List <BankCashReceiptDetailItemModel> {
                model2.Items.First()
            };
            newModel2.OtherItems = new List <BankCashReceiptDetailOtherItemModel> {
                model2.OtherItems.First()
            };
            var Response = await service.UpdateAsync(model2.Id, newModel2);

            Assert.NotEqual(0, Response);

            BankCashReceiptDetailItemModel newItem = new BankCashReceiptDetailItemModel
            {
                BankCashReceiptDetailId = 1,
                Amount = 2,
            };

            BankCashReceiptDetailOtherItemModel newOtherItem = new BankCashReceiptDetailOtherItemModel
            {
                BankCashReceiptDetailId = 1,
                Amount     = 2,
                TypeAmount = "KREDIT"
            };

            newModel2.Items.Add(newItem);
            newModel2.OtherItems.Add(newOtherItem);
            var Response3 = await service.UpdateAsync(model2.Id, newModel2);

            Assert.NotEqual(0, Response);

            // delete item and other item
            BankCashReceiptDetailModel newModel3 = new BankCashReceiptDetailModel();

            newModel3.Id = model2.Id;

            newModel3.Items      = new List <BankCashReceiptDetailItemModel>();
            newModel3.OtherItems = new List <BankCashReceiptDetailOtherItemModel>();
            var Response4 = await service.UpdateAsync(model2.Id, newModel3);

            Assert.NotEqual(0, Response4);
        }