public async Task <IActionResult> AddComment(int idUser, int idProduct, int stars, string content) { int totalComment = await(from c in _context.Comments where c.User_Id == idUser select c).CountAsync(); Comment comment = new Comment { User_Id = idUser, Product_Id = idProduct, Stars = stars, Content = content, DateCreate = DateTime.Now, DateModify = DateTime.Now }; _context.Comments.Add(comment); _context.SaveChanges(); Product product = await(from p in _context.Products where p.Id == idProduct select p).SingleOrDefaultAsync(); product.Stars = Math.Round((product.Stars * totalComment + stars) / (totalComment + 1), 1); _context.Products.Update(product); _context.SaveChanges(); var viewModel = await GetCommentById(idProduct, 1); return(PartialView("Users/_Comments", viewModel)); }
public IActionResult Register(UserRegisterViewModel vm) { if (ModelState.IsValid) { _context.Users.Add(vm.User); _context.SaveChanges(); return(RedirectToAction("Login")); } else { return(View( new UserRegisterViewModel() { User = vm.User, ProductTypes = _context.ProductTypes.ToList() } )); } }
public IActionResult MustSignUp(CheckOutMustSignUpViewModel vm) { if (ModelState.IsValid) { _context.Users.Add(vm.User); _context.SaveChanges(); AnoCart anocart = _context.AnoCarts.Where(ac => ac.Id == vm.AnoCartId).FirstOrDefault(); Cart cart = new Cart(); int userid = _context.Users.Where(u => u.PhoneNumber == vm.User.PhoneNumber).FirstOrDefault().Id; cart.User_Id = userid; cart.CartStatus_Id = anocart.CartStatus_Id; cart.TotalQuantity = anocart.TotalQuantity; cart.TotalPrice = anocart.TotalPrice; cart.DateCreate = anocart.DateCreate; cart.DateModify = anocart.DateModify; _context.Carts.Add(cart); _context.SaveChanges(); int cartid = _context.Carts.Where(c => c.User_Id == userid).FirstOrDefault().Id; if (_context.AnoCartDetails.Where(ac => ac.Cart_Id == anocart.Id).FirstOrDefault() != null) { List <AnoCartDetail> anodetails = _context.AnoCartDetails.Where(ac => ac.Cart_Id == anocart.Id).ToList(); foreach (AnoCartDetail detail in anodetails) { CartDetail cdt = new CartDetail() { Cart_Id = cartid, Product_Id = detail.Product_Id, PriceSingle = detail.PriceSingle, PriceTotal = detail.PriceTotal, Quantity = detail.Quantity, DateCreate = detail.DateCreate, DateModify = detail.DateModify }; _context.CartDetails.Add(cdt); _context.SaveChanges(); } } return(RedirectToAction("AlreadySignUp", "Checkout", new { userid = userid })); } else { return(View( new CheckOutMustSignUpViewModel() { User = vm.User, AnoCartId = vm.AnoCartId, ProductTypes = _context.ProductTypes.ToList() } )); } }
// nhận từ ajax? public async Task <int> AddItem(int id) { AnoCartDetail anoCartDetail; AnoCart anoCart; Cart cart; CartDetail cartDetail; Product product = await(from p in _context.Products where p.Id == id select p).FirstOrDefaultAsync(); if (product == null) { return(-1); } //tai khoan chua dang nhap if (HttpContext.Session.GetInt32("IdTaiKhoan") == null) { //khởi tạo cart lần đầu if (HttpContext.Session.GetInt32("IdCart") == null) { anoCart = new AnoCart { CartStatus_Id = 2, TotalPrice = product.Price - (int)(product.Price * product.Saleoff) / 100, TotalQuantity = 1, DateCreate = DateTime.Now, DateModify = DateTime.Now }; //thêm cart vào database _context.AnoCarts.Add(anoCart); _context.SaveChanges(); //lấy và set id cart vừa tạo vào session int idCart = await(from a in _context.AnoCarts select a.Id).MaxAsync(); HttpContext.Session.SetInt32("IdCart", idCart); //tạo mới anoCartDetail anoCartDetail = new AnoCartDetail { Cart_Id = HttpContext.Session.GetInt32("IdCart").Value, Product_Id = id, Quantity = 1, PriceTotal = product.Price - (int)(product.Price * product.Saleoff) / 100, PriceSingle = product.Price - (int)(product.Price * product.Saleoff) / 100, DateCreate = DateTime.Now, DateModify = DateTime.Now }; _context.AnoCartDetails.Add(anoCartDetail); _context.SaveChanges(); return(1); } //thêm sản phẩm theo id khi cart đã được tạo else { //lấy anoCartDetail theo id sản phẩm và id cart anoCartDetail = await(from d in _context.AnoCartDetails where d.Product_Id == id & d.Cart_Id == HttpContext.Session.GetInt32("IdCart") select d).SingleOrDefaultAsync(); //nếu chưa có anoCartDetail theo sản phẩm và cart //tạo mới if (anoCartDetail == null) { anoCartDetail = new AnoCartDetail { Cart_Id = HttpContext.Session.GetInt32("IdCart").Value, Product_Id = product.Id, Quantity = 1, PriceTotal = product.Price - (int)(product.Price * product.Saleoff) / 100, PriceSingle = product.Price - (int)(product.Price * product.Saleoff) / 100, DateCreate = DateTime.Now, DateModify = DateTime.Now }; //thêm anoCartDrtail vào database _context.AnoCartDetails.Add(anoCartDetail); _context.SaveChanges(); //cập nhật anoCart anoCart = await(from c in _context.AnoCarts where c.Id == HttpContext.Session.GetInt32("IdCart") select c).SingleOrDefaultAsync(); anoCart.TotalPrice += (product.Price - (int)(product.Price * product.Saleoff) / 100); anoCart.TotalQuantity += 1; _context.AnoCarts.Update(anoCart); _context.SaveChanges(); return(1); } //đã có anoCartDetail tang so luong, gia tien (anoCart, anoCartDetails) else { //Cap nhat anoCartDetails anoCartDetail = await(from d in _context.AnoCartDetails where d.Product_Id == id & d.Cart_Id == HttpContext.Session.GetInt32("IdCart") select d).SingleOrDefaultAsync(); anoCartDetail.Quantity += 1; anoCartDetail.PriceTotal = anoCartDetail.Quantity * anoCartDetail.PriceSingle; //cap nhat anoCart anoCart = await(from c in _context.AnoCarts where c.Id == HttpContext.Session.GetInt32("IdCart") select c).SingleOrDefaultAsync(); anoCart.TotalPrice += (product.Price - (int)(product.Price * product.Saleoff) / 100); anoCart.TotalQuantity += 1; _context.AnoCarts.Update(anoCart); _context.SaveChanges(); return(1); } } } //tai khoan da dang nhap else { cart = await(from c in _context.Carts where c.User_Id == HttpContext.Session.GetInt32("IdTaiKhoan") select c).SingleOrDefaultAsync(); //tai khoan nay chua co cart , thi tao moi if (cart == null) { //them moi cart cart = new Cart { CartStatus_Id = 2, User_Id = HttpContext.Session.GetInt32("IdTaiKhoan").Value, TotalPrice = product.Price - (int)(product.Price * product.Saleoff) / 100, TotalQuantity = 1, DateCreate = DateTime.Now, DateModify = DateTime.Now }; _context.Carts.Add(cart); _context.SaveChanges(); //them moi cartDetail cartDetail = new CartDetail { Cart_Id = cart.Id, Product_Id = id, Quantity = 1, PriceTotal = product.Price - (int)(product.Price * product.Saleoff) / 100, PriceSingle = product.Price - (int)(product.Price * product.Saleoff) / 100, DateCreate = DateTime.Now, DateModify = DateTime.Now }; _context.CartDetails.Add(cartDetail); _context.SaveChanges(); return(1); } //tai khoan nay da tao cart else if (cart != null) { //lấy cartDetail theo id sản phẩm và id cart cartDetail = await(from d in _context.CartDetails where d.Product_Id == id & d.Cart_Id == cart.Id select d).SingleOrDefaultAsync(); //Chua co cartDetail them moi if (cartDetail == null) { cartDetail = new CartDetail { Cart_Id = cart.Id, Product_Id = product.Id, Quantity = 1, PriceTotal = product.Price - (int)(product.Price * product.Saleoff) / 100, PriceSingle = product.Price - (int)(product.Price * product.Saleoff) / 100, DateCreate = DateTime.Now, DateModify = DateTime.Now }; //thêm cartDrtail vào database _context.CartDetails.Add(cartDetail); _context.SaveChanges(); //cập nhật Cart cart = await(from c in _context.Carts where c.Id == cart.Id select c).SingleOrDefaultAsync(); cart.TotalPrice += (product.Price - (int)(product.Price * product.Saleoff) / 100); cart.TotalQuantity += 1; _context.Carts.Update(cart); _context.SaveChanges(); return(1); } //đã có cartDetail tang so luong, gia tien (cart, cartDetails) else { //Cap nhat CartDetails cartDetail = await(from d in _context.CartDetails where d.Product_Id == id & d.Cart_Id == cart.Id select d).SingleOrDefaultAsync(); cartDetail.Quantity += 1; cartDetail.PriceTotal = cartDetail.Quantity * cartDetail.PriceSingle; //cap nhat Cart cart = await(from c in _context.Carts where c.Id == cart.Id select c).SingleOrDefaultAsync(); cart.TotalPrice += (product.Price - (int)(product.Price * product.Saleoff) / 100); cart.TotalQuantity += 1; _context.Carts.Update(cart); _context.SaveChanges(); return(1); } } } return(0); }
// Xóa sản phẩm public void RemoveProduct(Product product) { // Xóa ảnh RemoveImage(product); // Xóa các bình luận List <Comment> comments = _context.Comments.Where(c => c.Product_Id == product.Id).ToList(); if (comments != null) { foreach (var comment in comments) { _context.Comments.Remove(comment); _context.SaveChanges(); } } // Xóa slug Slug slug_delete = _context.Slugs.Where(s => s.Id == product.Slug_Id).FirstOrDefault(); _context.Slugs.Remove(slug_delete); // Xóa trong CartDetail List <CartDetail> cartdetails = _context.CartDetails.Where(c => c.Product_Id == product.Id).ToList(); if (cartdetails != null) { foreach (var detail in cartdetails) { _context.CartDetails.Remove(detail); _context.SaveChanges(); } } // Xóa trong AnoCartDetail List <AnoCartDetail> anocartdetails = _context.AnoCartDetails.Where(c => c.Product_Id == product.Id).ToList(); if (cartdetails != null) { foreach (var detail in anocartdetails) { _context.AnoCartDetails.Remove(detail); _context.SaveChanges(); } } _context.Products.Remove(product); _context.SaveChanges(); }