Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("IdKhachHang,NameKh,Email,Password,Address,SoDiethoai")] Khachhang khachhang)
        {
            if (ModelState.IsValid)
            {
                _context.Add(khachhang);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(khachhang));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("IddanhMuc,TenDm")] Danhmuc danhmuc)
        {
            if (ModelState.IsValid)
            {
                _context.Add(danhmuc);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(danhmuc));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("IdsanPham,TenSp,GiaSp,MoTa,DanhMuc")] Sanpham sanpham)
        {
            if (ModelState.IsValid)
            {
                _context.Add(sanpham);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["DanhMuc"] = new SelectList(_context.Danhmuc, "IddanhMuc", "IddanhMuc", sanpham.DanhMuc);
            return(View(sanpham));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Idhoadon,IdNguoimua,IdchiTiet,TongTien,SoLuong,NgayTao,TinhTrang")] Hoadon hoadon)
        {
            if (ModelState.IsValid)
            {
                _context.Add(hoadon);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["IdNguoimua"] = new SelectList(_context.Khachhang, "IdKhachHang", "Email", hoadon.IdNguoimua);
            return(View(hoadon));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Create([Bind("IdHd,IdSp,Soluong,GiaSp")] Chitiethd chitiethd)
        {
            if (ModelState.IsValid)
            {
                _context.Add(chitiethd);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["IdHd"] = new SelectList(_context.Hoadon, "Idhoadon", "Idhoadon", chitiethd.IdHd);
            ViewData["IdSp"] = new SelectList(_context.Sanpham, "IdsanPham", "IdsanPham", chitiethd.IdSp);
            return(View(chitiethd));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> CreateInvoce(InvoceModel model)
        {
            // Tach tao hoa don ra rieng de nhin va de tranh loi null
            Hoadon           HDtoADD    = HoadonCreator(model);
            List <Chitiethd> chitiethds = new List <Chitiethd>();
            // Lay danh sach san pham trong cart
            var cartLst = await _context.Carts.Include(sp => sp.Sp).ToListAsync();

            foreach (Carts item in cartLst)
            {
                // Tao chi tiet hoa don vs san pham vaf thong tin trong cart
                Chitiethd chitiet = new Chitiethd()
                {
                    IdHd    = HDtoADD.Idhoadon,
                    IdSp    = item.SpId,
                    GiaSp   = item.Sp.GiaSp,
                    Soluong = item.Quantity.Value.ToString(),
                };
                chitiethds.Add(chitiet);
            }
            _context.Carts.RemoveRange(cartLst);
            _context.Hoadon.Add(HDtoADD);
            _context.Chitiethd.AddRange(chitiethds);
            await _context.SaveChangesAsync();

            ViewData["Message"] = "Đã lưu thông tin Hóa Đơn, mã hóa đơn của bạn là " + HDtoADD.Idhoadon.ToString();
            return(RedirectToAction("Index", "HoaDon"));
        }
Exemplo n.º 7
0
        public IActionResult UpdateItems([FromBody] UpdateCartView model)
        {
            if (!UserStatus.getUserStatus(this, "Client"))
            {
                var cartItems = HttpContext.Session.GetObjectFromJson <List <Carts> >(GetCartId());
                if (cartItems != null)
                {
                    //var itemToRM = cartItems.FirstOrDefault(items => items.AutoId.Equals(model.id));

                    var itemIndex = cartItems.FindIndex(sp => sp.AutoId.Equals(model.id.Trim()) && sp.CartId.Equals(ShoppingCartId));
                    cartItems[itemIndex].Quantity = model.quantity;
                    HttpContext.Session.SetObjectAsJson(GetCartId(), cartItems);
                    return(Ok("Cap nhat r nha bo"));
                }
            }
            else
            {
                var items = _context.Carts.FirstOrDefault(sp => sp.AutoId.Equals(model.id) && sp.CartId.Equals(GetCartId()));
                if (items != null)
                {
                    items.Quantity = model.quantity;
                    _context.Carts.Update(items);
                    _context.SaveChangesAsync();
                    return(Ok("Cap nhat r nha bo"));
                }
            }
            return(NotFound("Broo what are u doingg"));
        }
Exemplo n.º 8
0
        private async Task <int> updateImagesPOSAsync(Sanpham spToUpdate, string viTrih, string newFileName, IFormFile images)
        {
            //Luu vao db
            var PathDB = "images/Sanpham/" + newFileName;

            //kiem vi tri de luu tru hinh
            switch (viTrih)
            {
            case "1":
            {
                // Xoa hinh cu
                if (!string.IsNullOrEmpty(spToUpdate.Hinh1))
                {
                    var oldpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", spToUpdate.Hinh1);
                    if (System.IO.File.Exists(oldpath))
                    {
                        System.IO.File.Delete(oldpath);
                    }
                }
                // thay the duong dan hinh moi vao db
                spToUpdate.Hinh1 = PathDB;

                break;
            }

            case "2":
            {
                // Xoa hinh cu
                if (!string.IsNullOrEmpty(spToUpdate.Hinh2))
                {
                    var oldpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", spToUpdate.Hinh2);
                    if (System.IO.File.Exists(oldpath))
                    {
                        System.IO.File.Delete(oldpath);
                    }
                }
                spToUpdate.Hinh2 = PathDB;
                break;
            }

            case "3":
            {
                if (!string.IsNullOrEmpty(spToUpdate.Hinh3))
                {
                    var oldpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", spToUpdate.Hinh3);
                    if (System.IO.File.Exists(oldpath))
                    {
                        System.IO.File.Delete(oldpath);
                    }
                }
                spToUpdate.Hinh3 = PathDB;
                break;
            }

            case "4":
            {
                // Xoa hinh cu
                if (!string.IsNullOrEmpty(spToUpdate.Hinh4))
                {
                    var oldpath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", spToUpdate.Hinh4);
                    if (System.IO.File.Exists(oldpath))
                    {
                        System.IO.File.Delete(oldpath);
                    }
                }
                spToUpdate.Hinh4 = PathDB;
                break;
            }
            }
            db.Sanpham.Update(spToUpdate);

            await db.SaveChangesAsync();

            //path to create new file
            var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images/Sanpham", newFileName);

            using (var stream = new FileStream(path, FileMode.Create))
            {
                await images.CopyToAsync(stream);
            }

            return(0);
        }
Exemplo n.º 9
0
        public async Task <IActionResult> Login(LoginRegisterModel Lmodel)
        {
            if (ModelState.IsValid)
            {
                var kh = checkUserinfo(Lmodel);
                if (kh != null)
                {
                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, kh.NameKh != null? kh.NameKh : kh.Email),
                        new Claim(ClaimTypes.Role, "Client"),
                        new Claim(ClaimTypes.NameIdentifier, kh.IdKhachHang.ToString())
                    };
                    ClaimsIdentity  userIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                    ClaimsPrincipal principal    = new ClaimsPrincipal(userIdentity);

                    var oldCartID = HttpContext.Session.GetString(CartSessionKey);
                    var cartList  = oldCartID != null?HttpContext.Session.GetObjectFromJson <List <Carts> >(oldCartID) : new List <Carts>();

                    List <Carts> lstToadd = new List <Carts>();
                    if (cartList != null && cartList.Count() > 0)
                    {
                        foreach (var item in cartList)
                        {
                            var add = true;
                            item.Sp     = null;
                            item.CartId = kh.IdKhachHang.ToString();
                            var spExisted = _context.Carts.FirstOrDefault(sp => sp.SpId == item.SpId && sp.CartId.Equals(item.CartId));
                            // if user carts already exist we just want to update quanity instead adding new
                            // just exclude it out of the things to add
                            if (spExisted != null)
                            {
                                spExisted.Quantity += item.Quantity;
                                _context.Carts.Update(spExisted);
                                add = false;
                            }
                            if (add)
                            {
                                lstToadd.Add(item);
                            }
                        }
                        //make sure shit in session is clean af so we can go on with items from db
                        HttpContext.Session.SetObjectAsJson(oldCartID, "");
                        await _context.Carts.AddRangeAsync(lstToadd);
                    }

                    await _context.SaveChangesAsync();

                    HttpContext.Session.SetString(CartSessionKey, kh.IdKhachHang.ToString());

                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

                    return(Redirect("/"));
                }
                else
                {
                    ViewData["UserLoginFailed"] = "Sai tên đăng nhập hoặc mật khẩu r nha bạn ";
                }
            }
            return(View());
        }