Beispiel #1
0
        //===========================================================
        //===========================================================

        #region -- Delete User --
        public bool DeleteUser(int Id)
        {
            EatWellDBContext db   = new EatWellDBContext();
            Users            user = db.Users.FirstOrDefault(x => x.UserId == Id);

            if (user == null)
            {
                return(false);
            }
            db.Users.Remove(user);
            db.SaveChangesAsync();
            return(true);
        }
        //===========================================================
        //===========================================================

        #region -- Delete Category --
        public bool DeleteCategory(int Id)
        {
            EatWellDBContext db       = new EatWellDBContext();
            Categories       category = db.Categories.FirstOrDefault(x => x.CategoryId == Id);

            if (category == null)
            {
                return(false);
            }
            db.Categories.Remove(category);
            db.SaveChangesAsync();
            return(true);
        }
        //===========================================================
        //===========================================================

        #region -- Create Product --

        public SingleRsp CreateProduct(ProductsReq pro)
        {
            var      res     = new SingleRsp();
            Products product = new Products();

            product.CategoryId  = pro.CategoryId;
            product.ProductName = pro.ProductName;
            product.Photo       = pro.Photo;
            product.Description = pro.Description;
            product.ProductSlug = pro.ProductSlug;
            product.IsActive    = pro.IsActive;

            // we must to save a new product before.
            // if we don't, we will not have productID to do anything.
            res = _rep.CreateProduct(product);

            // ProductsReq pro have Options is a list.
            // so we get those options and store it into database.
            foreach (var po in pro.Options)
            {
                using (var context = new EatWellDBContext())
                {
                    using (var tran = context.Database.BeginTransaction())
                    {
                        try
                        {
                            ProductOptions product_option = new ProductOptions();
                            product_option.ProductId = product.ProductId;
                            product_option.OptionId  = po.OptionId;
                            product_option.Price     = po.Price;

                            // add a new record in to ProductOptions table
                            var t = context.ProductOptions.Add(product_option);

                            context.SaveChanges();
                            tran.Commit();
                        }
                        catch (Exception ex)
                        {
                            tran.Rollback();
                            res.SetError(ex.StackTrace);
                        }
                    }
                }
            }

            return(res);
        }
Beispiel #4
0
        //===========================================================
        //===========================================================

        #region -- Get Users With Pagination --
        public object GetAllUsersWithPagination(int page, int size)
        {
            EatWellDBContext db = new EatWellDBContext();
            var user            = db.Users.ToList();
            var offset          = (page - 1) * size;
            var total           = user.Count();
            int totalpage       = (total % size) == 0 ? (total / size) : (int)((total / size) + 1);
            var data            = user.OrderBy(x => x.UserId).Skip(offset).Take(size).ToList();
            var res             = new
            {
                Data        = data,
                TotalRecord = total,
                TotalPage   = totalpage,
                Page        = page,
                Size        = size
            };

            return(res);
        }
        //===========================================================
        //===========================================================

        #region -- Update Product --

        public SingleRsp UpdateProduct(ProductsReq pro)
        {
            var res = new SingleRsp();

            var product = All.Where(x => x.ProductId == pro.ProductId).FirstOrDefault();

            if (product != null)
            {
                product.CategoryId  = pro.CategoryId;
                product.ProductName = pro.ProductName;
                product.Photo       = pro.Photo;
                product.Description = pro.Description;
                product.ProductSlug = pro.ProductSlug;
                product.IsActive    = pro.IsActive;

                res = _rep.UpdateProduct(product);

                foreach (var po in product.ProductOptions)
                {
                    EatWellDBContext db = new EatWellDBContext();
                    var productoption   = new ProductOptions();

                    productoption.ProductId = product.ProductId;
                    productoption.OptionId  = po.OptionId;
                    productoption.Price     = po.Price;

                    db.ProductOptions.Update(productoption);
                    db.SaveChangesAsync();
                }
            }
            else
            {
            }

            return(res);
        }