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

            foreach (var item in exist.Items)
            {
                GarmentFinanceBankCashReceiptDetailLocalItemModel 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)
            {
                GarmentFinanceBankCashReceiptDetailLocalOtherItemModel 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());
        }
Beispiel #2
0
        public async Task Shou0ld_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 GarmentFinanceBankCashReceiptDetailLocalService(serviceProviderMock.Object);

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

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

            GarmentFinanceBankCashReceiptDetailLocalModel newModel2 = new GarmentFinanceBankCashReceiptDetailLocalModel();

            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 <GarmentFinanceBankCashReceiptDetailLocalItemModel> {
                model2.Items.First()
            };
            newModel2.OtherItems = new List <GarmentFinanceBankCashReceiptDetailLocalOtherItemModel> {
                model2.OtherItems.First()
            };
            var Response = await service.UpdateAsync(model2.Id, newModel2);

            Assert.NotEqual(0, Response);

            GarmentFinanceBankCashReceiptDetailLocalItemModel newItem = new GarmentFinanceBankCashReceiptDetailLocalItemModel
            {
                BankCashReceiptDetailLocalId = 1,
                Amount = 2,
            };

            GarmentFinanceBankCashReceiptDetailLocalOtherItemModel newOtherItem = new GarmentFinanceBankCashReceiptDetailLocalOtherItemModel
            {
                BankCashReceiptDetailLocalId = 1,
                Amount     = 2,
                TypeAmount = "DEBIT"
            };

            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
            GarmentFinanceBankCashReceiptDetailLocalModel newModel3 = new GarmentFinanceBankCashReceiptDetailLocalModel();

            newModel3.Id = model2.Id;

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

            Assert.NotEqual(0, Response4);
        }