Esempio n. 1
0
        public override int insert(object obj)
        {
            try
            {
                if (obj == null)
                {
                    throw new CustomException(GetType().Name + " : Inserting Null Value");
                }

                BillEntity newBill = obj as BillEntity;

                BillHistory billHistory = new BillHistory
                {
                    CashierID  = newBill.CashierID,
                    BillDate   = newBill.BillDate,
                    TotalPrice = CalculateTotalPrice(newBill.ListProduct)
                };

                using (StoreManagementEntities context = new StoreManagementEntities())
                {
                    context.BillHistories.Add(billHistory);

                    foreach (KeyValuePair <int, int> product in newBill.ListProduct)
                    {
                        BillDetail billDetail = new BillDetail
                        {
                            BillID    = billHistory.BillID,
                            ProductID = product.Key,
                            Quantity  = product.Value
                        };

                        Product pro = context.Products.Find(product.Key);
                        pro.Quantity -= product.Value;

                        context.BillDetails.Add(billDetail);
                    }

                    context.SaveChanges();
                }

                return(billHistory.BillID);
            }
            catch (Exception e)
            {
                CustomException ex = new CustomException(GetType().Name + " : Insert " + obj.ToString() + "\n" + e.Message);
                ex.showPopupError();
            }

            return(-1);
        }
Esempio n. 2
0
        public override void update(object obj)
        {
            try
            {
                UserEntity user = obj as UserEntity;

                using (StoreManagementEntities context = new StoreManagementEntities())
                {
                    User oldUser = context.Users.Find(user.UserID);
                    context.Entry(oldUser).CurrentValues.SetValues(user);
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                CustomException ex = new CustomException(GetType().Name + " : Update " + obj.ToString() + "\n" + e.Message);
                ex.showPopupError();
            }
        }
        public override void update(object obj)
        {
            try
            {
                ProductEntity product = obj as ProductEntity;

                using (StoreManagementEntities context = new StoreManagementEntities())
                {
                    Product oldProduct = context.Products.Find(product.ProductID);
                    context.Entry(oldProduct).CurrentValues.SetValues(product);
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                CustomException ex = new CustomException(GetType().Name + " : Update " + obj.ToString() + "\n" + e.Message);
                ex.showPopupError();
            }
        }
Esempio n. 4
0
        public override void delete(object obj)
        {
            try
            {
                UserEntity user = obj as UserEntity;

                using (StoreManagementEntities context = new StoreManagementEntities())
                {
                    User delete = (from u in context.Users
                                   where user.UserID == u.UserID
                                   select u).Single();

                    context.Users.Remove(delete);
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                CustomException ex = new CustomException(GetType().Name + " : Delete " + obj.ToString() + "\n" + e.Message);
                ex.showPopupError();
            }
        }
        public override int insert(object obj)
        {
            try
            {
                Product product = obj.Cast <Product>();

                using (StoreManagementEntities context = new StoreManagementEntities())
                {
                    context.Products.Add(product);
                    context.SaveChanges();
                }

                return(product.ProductID);
            }
            catch (Exception e)
            {
                CustomException ex = new CustomException(GetType().Name + " : Insert " + obj.ToString() + "\n" + e.Message);
                ex.showPopupError();
            }

            return(-1);
        }
        public override void delete(object obj)
        {
            try
            {
                ProductEntity product = obj as ProductEntity;

                using (StoreManagementEntities context = new StoreManagementEntities())
                {
                    Product delete = (from p in context.Products
                                      where product.ProductID == p.ProductID
                                      select p).Single();

                    context.Products.Remove(delete);
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                CustomException ex = new CustomException(GetType().Name + " : Delete " + obj.ToString() + "\n" + e.Message);
                ex.showPopupError();
            }
        }
Esempio n. 7
0
        public override int insert(object obj)
        {
            try
            {
                UserEntity newUser = obj as UserEntity;
                User user = newUser.Cast<User>();

                using (StoreManagementEntities context = new StoreManagementEntities())
                {
                    context.Users.Add(user);
                    context.SaveChanges();
                }

                return user.UserID;
            }
            catch (Exception e)
            {
                CustomException ex = new CustomException(GetType().Name + " : Insert " + obj.ToString() + "\n" + e.Message);
                ex.showPopupError();
            }

            return -1;
        }
Esempio n. 8
0
        public override void delete(object obj)
        {
            try
            {
                BillEntity entity = obj as BillEntity;

                using (StoreManagementEntities context = new StoreManagementEntities())
                {
                    foreach (KeyValuePair <int, int> product in entity.ListProduct)
                    {
                        BillDetail detail = (from bill in context.BillDetails
                                             where bill.BillID == entity.BillID &&
                                             bill.ProductID == product.Key
                                             select bill)
                                            .Single();

                        context.BillDetails.Remove(detail);

                        Product pro = context.Products.Find(product.Key);
                        pro.Quantity += product.Value;
                    }

                    BillHistory history = (from bill in context.BillHistories
                                           where bill.BillID == entity.BillID
                                           select bill)
                                          .Single();

                    context.BillHistories.Remove(history);
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                CustomException ex = new CustomException(GetType().Name + " : Delete " + obj.ToString() + "\n" + e.Message);
                ex.showPopupError();
            }
        }