Example #1
0
        public async Task <int> UpdateAsync(int id, GarmentFinanceMemorialDetailLocalModel model)
        {
            var exist = DbSet
                        .Include(d => d.Items)
                        .Include(d => d.OtherItems)
                        .Single(o => o.Id == id && !o.IsDeleted);

            foreach (var item in exist.Items)
            {
                GarmentFinanceMemorialDetailLocalItemModel 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)
                {
                    EntityExtension.FlagForCreate(newItem, IdentityService.Username, UserAgent);
                    exist.Items.Add(newItem);
                }
            }

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

                if (otherItemModel == null)
                {
                    EntityExtension.FlagForDelete(otherItem, IdentityService.Username, UserAgent, true);
                }
                else
                {
                    otherItem.Amount     = otherItemModel.Amount;
                    otherItem.TypeAmount = otherItemModel.TypeAmount;
                    EntityExtension.FlagForUpdate(otherItem, IdentityService.Username, UserAgent);
                }
            }
            foreach (var newOtherItem in model.OtherItems)
            {
                if (newOtherItem.Id <= 0)
                {
                    EntityExtension.FlagForCreate(newOtherItem, IdentityService.Username, UserAgent);
                    exist.OtherItems.Add(newOtherItem);
                }
            }
            EntityExtension.FlagForUpdate(exist, IdentityService.Username, UserAgent);
            return(await DbContext.SaveChangesAsync());
        }
Example #2
0
        public async Task Should_Success_Update_Data()
        {
            GarmentFinanceMemorialDetailLocalService service = new GarmentFinanceMemorialDetailLocalService(GetServiceProvider().Object, _dbContext(GetCurrentAsyncMethod()));


            GarmentFinanceMemorialDetailLocalModel model = await _dataUtil(service, GetCurrentMethod()).GetTestData();

            var newModel = await service.ReadByIdAsync(model.Id);

            //newModel.BGCheckNumber = "newBG";
            var Response1 = await service.UpdateAsync(newModel.Id, newModel);

            Assert.NotEqual(0, Response1);

            GarmentFinanceMemorialDetailLocalModel model2 = await _dataUtil(service, GetCurrentMethod()).GetTestData();

            //var newModel2 = await service.ReadByIdAsync(model.Id);
            GarmentFinanceMemorialDetailLocalModel newModel2 = new GarmentFinanceMemorialDetailLocalModel();

            newModel2.Id = model2.Id;

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

            Assert.NotEqual(0, Response);

            GarmentFinanceMemorialDetailLocalItemModel newItem = new GarmentFinanceMemorialDetailLocalItemModel
            {
                LocalSalesNoteId = 3,
                LocalSalesNoteNo = "no3",
                BuyerName        = "Name",
                BuyerCode        = "code",
                BuyerId          = 3,
                CurrencyId       = 1,
                CurrencyCode     = "code",
                CurrencyRate     = 1,
                Amount           = 1
            };


            newModel2.Items.Add(newItem);

            GarmentFinanceMemorialDetailLocalOtherItemModel newOtherItem = new GarmentFinanceMemorialDetailLocalOtherItemModel
            {
                ChartOfAccountId   = 1,
                ChartOfAccountName = "Name",
                ChartOfAccountCode = "code",
                CurrencyId         = 1,
                CurrencyCode       = "code",
                CurrencyRate       = 1,
                Amount             = 1,
                TypeAmount         = "DEBIT"
            };

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

            Assert.NotEqual(0, Response);

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

            newModel3.Id = model2.Id;

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

            Assert.NotEqual(0, Response4);
        }