Exemplo n.º 1
0
        public IActionResult AddOrder(string account_id, string fullname, string address, string email, string tel, string city)
        {
            var gioHang = SessionHelper.Get <List <CartItem> >(HttpContext.Session, "cart");

            if (gioHang == null || gioHang.Count() == 0)
            {
                ViewBag.Success = false;
            }
            else
            {
                KhachHang newKh = new KhachHang
                {
                    TenKhachHang = fullname,
                    DiaChi       = address,
                    Email        = email,
                    SoDt         = tel,
                    MaTinhThanh  = int.Parse(city)
                };
                db.KhachHang.Add(newKh);
                db.SaveChanges();
                GioHang newOrder = new GioHang
                {
                    MaKhachHang = newKh.MaKhachHang,
                    NgayDat     = DateTime.Now,
                    TrangThai   = 0
                };
                if (account_id != null)
                {
                    newOrder.MaTaiKhoan = int.Parse(account_id);
                }
                db.GioHang.Add(newOrder);
                db.SaveChanges();
                var cart = SessionHelper.Get <List <CartItem> >(HttpContext.Session, "cart");
                foreach (var item in cart)
                {
                    ChiTietGioHang newDetail = new ChiTietGioHang
                    {
                        MaGioHang = newOrder.MaGioHang,
                        MaThucDon = item.MaThucDon,
                        SoLuong   = item.SoLuong
                    };
                    db.ChiTietGioHang.Add(newDetail);
                    db.SaveChanges();
                }
                HttpContext.Session.Remove("cart");
                ViewBag.Success = true;
            }
            ViewBag.City = (from tt in db.TinhThanh select tt);
            gioHang      = SessionHelper.Get <List <CartItem> >(HttpContext.Session, "cart");
            ViewBag.Cart = gioHang;
            if (gioHang != null)
            {
                ViewBag.Total = gioHang.Sum(x => x.SoLuong * x.GiaBan);
            }
            else
            {
                ViewBag.Total = 0;
            }
            return(View("Index"));
        }
Exemplo n.º 2
0
        public ActionResult <IEnumerable <MenuItem> > PutMenuItem([FromBody] MenuItem menuItem)
        {
            _context.MenuItems.Update(menuItem);

            _context.SaveChanges();

            return(_context.MenuItems.ToList());
        }
        public ActionResult Create([Bind(Include = "ID,Name,Description,Quantity,Price")] InventoryCoffee inventoryCoffee)
        {
            if (ModelState.IsValid)
            {
                db.InventoryCoffees.Add(inventoryCoffee);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(inventoryCoffee));
        }
Exemplo n.º 4
0
        public ActionResult Create([Bind(Include = "Id,FirstName,LastName,Email,Password,ItemId")] User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                return(RedirectToAction($"Details/{user.Id}"));
            }

            ViewBag.ItemId = new SelectList(db.Items, "Id", "Name", user.ItemId);
            return(View(user));
        }
        public IActionResult Edit(Product product, int id)
        {
            var productToEdit = _coffeeShopContext.Products.Find(id);

            productToEdit.Name        = product.Name;
            productToEdit.Price       = product.Price;
            productToEdit.Quantity    = product.Quantity;
            productToEdit.Description = product.Description;

            _coffeeShopContext.SaveChanges();

            return(RedirectToAction("Products", "Product"));
        }
Exemplo n.º 6
0
        public void AddItems(string item)
        {
            // made db context into using statement
            using (var db = new CoffeeShopContext())
            {
                // wrapped our database actions in a transaction
                using (var scope = db.Database.BeginTransaction())
                {
                    try
                    {
                        var Item = JsonSerializer.Deserialize <Items>(item);
                        Item.id = 0;
                        db.Items.Add(Item);
                        db.SaveChanges();

                        // run commit to SaveChanges made to the database
                        scope.Commit();
                    }
                    catch (Exception ex)
                    {
                        //if we run rollback we can cancel all database actions
                        scope.Rollback();
                    }
                }
            }
        }
Exemplo n.º 7
0
        public void DeleteItem(string itemID)
        {
            using (var db = new CoffeeShopContext())
            {
                using (var scope = db.Database.BeginTransaction())
                {
                    try
                    {
                        // use a Linq where statement to find the Item you need
                        //I also call SingleOrDefault this way if there is no match in the DB
                        //it will return a default object instead
                        var Item = db.Items
                                   .Where(item => item.id == Convert.ToInt32(itemID))
                                   .SingleOrDefault();

                        //pass in the object to be removed
                        db.Items.Remove(Item);
                        db.SaveChanges();
                        //call commit to SaveChanges
                        scope.Commit();
                    }
                    catch (Exception ex)
                    {
                        //call rollback to cancel all DB actions
                        scope.Rollback();
                    }
                }
            }
        }
Exemplo n.º 8
0
 public void SaveDiscountOrder(DiscountOrder _order)
 {
     using (CoffeeShopContext db = new CoffeeShopContext())
     {
         db.DiscountOrders.Add(_order);
         db.SaveChanges();
     }
 }
        public IActionResult RemoveReview(int id)
        {
            var thisRv = db.DanhGia.SingleOrDefault(x => x.MaDanhGia == id);

            db.DanhGia.Remove(thisRv);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Exemplo n.º 10
0
 public void SaveCustomer(Customer _customer)
 {
     using (CoffeeShopContext db = new CoffeeShopContext())
     {
         db.Customers.Add(_customer);
         db.SaveChanges();
     }
 }
Exemplo n.º 11
0
 public void SaveProduct(Product _product)
 {
     using (CoffeeShopContext db = new CoffeeShopContext())
     {
         db.Products.Add(_product);
         db.SaveChanges();
     }
 }
Exemplo n.º 12
0
 public ActionResult Register(NewUser newUser)
 {
     if (ModelState.IsValid)
     {
         db.NewUsers.Add(newUser);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View("AddUser", newUser));
 }
Exemplo n.º 13
0
        public void UpdateCountInProduct(int _productId, int _amountOfBothProduct)
        {
            Product _changingProduct = new Product();

            using (CoffeeShopContext db = new CoffeeShopContext())
            {
                _changingProduct        = db.Products.Find(_productId);
                _changingProduct.Amount = _changingProduct.Amount - _amountOfBothProduct;
                db.SaveChanges();
            }
        }
Exemplo n.º 14
0
        public IActionResult Registration(User registration)
        {
            if (!ModelState.IsValid)
            {
                return(View(registration));
            }

            _coffeeShopContext.Users.Add(registration);
            _coffeeShopContext.SaveChanges();

            return(RedirectToAction("Index", "Home", registration));
        }
Exemplo n.º 15
0
        public ActionResult Create([Bind(Include = "Id,Name,Description,Quantity,Price")] Item item)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Items.Add(item);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            catch (DbUpdateException ex)
            {
                if (ExceptionHelper.IsUniqueConstraintViolation(ex))
                {
                    ModelState.AddModelError("Name", $"The Name '{item.Name}' is already in use, please enter a different name.");
                    return(View(nameof(Create), item));
                }
            }

            return(View(item));
        }
Exemplo n.º 16
0
        public IActionResult RegistrationConformation(RegisterViewModel model)
        {
            var newUser = new CoffeeUsersDAL();

            newUser.Email         = model.Email;
            newUser.Password      = model.Password;
            newUser.UserName      = model.UserName;
            newUser.FavoriteDrink = model.FavoriteDrink;

            _coffeeShopContext.Add(newUser);
            _coffeeShopContext.SaveChanges();

            return(View(model));
        }
Exemplo n.º 17
0
        public IActionResult AddRating(string product_id, string rating)
        {
            DanhGia newDg = new DanhGia()
            {
                MaTaiKhoan  = int.Parse(HttpContext.Session.GetString("ACCID_SESSION")),
                LoiNhanXet  = rating,
                MaThucDon   = int.Parse(product_id),
                NgayDanhGia = DateTime.Now
            };

            db.DanhGia.Add(newDg);
            db.SaveChanges();
            return(RedirectToAction("Index", new RouteValueDictionary(
                                        new { controller = "SingleProduct", action = "Index", id = product_id })));
        }
 public void SaveCustomers(IEnumerable <Customer> customers)
 {
     using (var c = new CoffeeShopContext())
     {
         var r = c.CoffeeCustomers;
         c.CoffeeCustomers.RemoveRange(r);
         c.CoffeeCustomers.AddRange(customers.Select(x => new Customer
         {
             FirstName   = x.FirstName,
             LastName    = x.LastName,
             IsDeveloper = x.IsDeveloper
         }));
         c.SaveChanges();
     }
 }
Exemplo n.º 19
0
        public ActionResult AddUser(RegisterUser newUser)
        {
            if (ModelState.IsValid)
            {
                db.RegisterUsers.Add(newUser);
                db.SaveChanges();


                ViewBag.ConfMessage = "Welcome " + newUser.FirstName;
                return(View());
            }
            else
            {
                return(View("Error"));
            }
        }
Exemplo n.º 20
0
        public IActionResult Welcome(User user, string password2, string password)
        {
            CoffeeShopContext db = new CoffeeShopContext();
            User ValidUser       = new User();

            if (password != password2)
            {
                return(ModalAction());
            }
            else
            {
                db.Add(user);
                db.SaveChanges();
            }
            return(View());
        }
Exemplo n.º 21
0
        public IActionResult Purchase(decimal?price, User user)
        {
            CoffeeShopContext db = new CoffeeShopContext();

            user = JsonSerializer.Deserialize <User>(HttpContext.Session.GetString("User"));

            if (user.CartFunds >= price)
            {
                user.CartFunds = user.CartFunds - price;
                db.Update(user);
                db.SaveChanges();
            }
            //else if (foundUser.CartFunds <= price)
            //{
            //    return View();
            //}

            return(View("Shop"));
        }
Exemplo n.º 22
0
        public IActionResult SignUp(string username, string password,
                                    string email, IFormFile avatar)
        {
            var check_exist = db.TaiKhoan.SingleOrDefault(x => x.TenTaiKhoan == username);

            if (check_exist == null)
            {
                TaiKhoan newTk = new TaiKhoan
                {
                    TenTaiKhoan = username,
                    MatKhau     = password,
                    Email       = email,
                    MaPhanQuyen = "kh"
                };
                if (avatar != null)
                {
                    string path_to_image = "wwwroot/uploads/employee/" + avatar.FileName;
                    using (var stream = new FileStream(path_to_image, FileMode.Create))
                    {
                        avatar.CopyTo(stream);
                    }
                    newTk.AnhDaiDien = avatar.FileName;
                }
                else
                {
                    newTk.AnhDaiDien = "none-avatar.jpg";
                }
                db.TaiKhoan.Add(newTk);
                db.SaveChanges();
                ViewBag.RegisterSucess = true;
                return(View("Index"));
            }
            else
            {
                ViewBag.AccExist = false;
                return(View("SignUp"));
            }
        }