Exemple #1
0
        private EntityEntryList OnBeforeSaveChanges()
        {
            //var userId = _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.NameIdentifier).Value;
            EntityEntryList list = new EntityEntryList();

            foreach (var entry in ChangeTracker.Entries())
            {
                if (entry.State == EntityState.Detached || entry.State == EntityState.Unchanged)
                {
                    continue;
                }
                if (entry.Entity is CartProduct)
                {
                    if (entry.State == EntityState.Added)
                    {
                        list.AddedEntityState = true;
                        list.AddedCartProductList.Add((CartProduct)entry.Entity);
                    }
                    else if (entry.State == EntityState.Modified)
                    {
                        list.ModifiedEntityState = true;
                        list.ModifiedCartProductList.Add((CartProduct)entry.Entity);
                    }
                    else if (entry.State == EntityState.Deleted)
                    {
                        list.DeletedEntityState = true;
                        list.DeletedCartProductList.Add((CartProduct)entry.Entity);
                    }
                }
            }
            return(list);
        }
Exemple #2
0
        private void OnAfterSaveChanges(EntityEntryList list)
        {
            //var userId = _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.NameIdentifier).Value;

            Product pro = new Product();

            if (list.AddedCartProductList != null && list.AddedCartProductList.Count > 0 && list.AddedEntityState == true)
            {
                foreach (var item in list.AddedCartProductList)
                {
                    pro              = Products.Find(item.ProductId);
                    pro.StockAmount -= 1;
                    Products.Update(pro);
                }
                SaveChanges();
            }
            else if (list.ModifiedCartProductList != null && list.ModifiedCartProductList.Count > 0 && list.ModifiedEntityState == true)
            {
                foreach (var item in list.ModifiedCartProductList)
                {
                    pro              = Products.Find(item.ProductId);
                    pro.StockAmount -= 1;
                    Products.Update(pro);
                }
                SaveChanges();
            }
            else if (list.DeletedCartProductList != null && list.DeletedCartProductList.Count > 0 && list.DeletedEntityState == true)
            {
                foreach (var item in list.DeletedCartProductList)
                {
                    pro              = Products.Find(item.ProductId);
                    pro.StockAmount += item.ProductAmount;
                    Products.Update(pro);
                }
                SaveChanges();
            }
        }