public List<Phanhoi> ThemPhanHoi(int BinhLuanId,string NoiDung){ var dbContext = new shopContext(); var userName = HttpContext.Session.GetString("username"); var taiKhoan = (from tk in dbContext.Taikhoan where tk.Username == userName select tk).ToList(); var phanHoi = new Phanhoi(){ BinhLuanId = BinhLuanId, TaiKhoanId = taiKhoan[0].TaiKhoanId, NoiDung = NoiDung }; dbContext.Phanhoi.Add(phanHoi); dbContext.SaveChanges(); var DSPhanHoi = (from ph in dbContext.Phanhoi join tk in dbContext.Taikhoan on ph.TaiKhoanId equals tk.TaiKhoanId where ph.BinhLuanId == BinhLuanId orderby ph.PhanHoiId descending select new { NoiDung = ph.NoiDung, userName = tk.Username }).ToList(); List<Phanhoi> ListPhanHoi = new List<Phanhoi>(); foreach (var item in DSPhanHoi) { Phanhoi ph = new Phanhoi(); ph.BinhLuanId = BinhLuanId; ph.NoiDung = item.NoiDung; Taikhoan tk = new Taikhoan(); tk.Username = item.userName; ph.TaiKhoan = tk; ListPhanHoi.Add(ph); } dbContext.SaveChanges(); return ListPhanHoi; }
public static (int, bool) AddOrderToDbOrGetId(shopContext shopContext, Order order) { int draftId = FindDraftId(shopContext, order.UserId); order.User = null; order.BillingAddress = null; order.ShippingAddress = null; order.Date = DateTime.Now; if (draftId == 0) { shopContext.Orders.Add(order); shopContext.SaveChanges(); shopContext.Entry(order).GetDatabaseValues(); return(order.OrderId, false); } else { order.OrderId = (int)draftId; order.Draft = false; shopContext.Orders.Update(order); shopContext.SaveChanges(); return((int)draftId, true); } }
public void XacNhanHuyDonHang(int mahd) { var dbContext = new shopContext(); var hoadon = (from hd in dbContext.Hoadon where hd.HoaDonId == mahd select hd).ToList(); //lay chi tiet hoa don cua hoa don xac nhan huy var cthd = (from ct in dbContext.Chitiethoadon where ct.HoaDonId == mahd select ct).ToList(); //xoa chi tiet hoa don foreach (var item in cthd) { var ct = dbContext.Chitiethoadon.First(a => a.ChiTietHoaDonId == item.ChiTietHoaDonId); dbContext.Chitiethoadon.Remove(ct); dbContext.SaveChanges(); } //xoa hoa don var hdon = dbContext.Hoadon.First(a => a.HoaDonId == hoadon[0].HoaDonId); dbContext.Hoadon.Remove(hdon); dbContext.SaveChanges(); dbContext.Dispose(); }
public IActionResult ThemNhanVien(Nhanvien model, string Username, string Password) { var dbContext = new shopContext(); var taikhoan1 = (from tk in dbContext.Taikhoan where tk.Username == Username select tk).ToList(); if (taikhoan1.Count > 0) { ModelState.AddModelError("Username", "Username da ton tai"); ViewBag.errUsername = "******"; } if (ModelState.IsValid) { // chuyển mật khẩu nhập vào thành mã băm MD5 mh = MD5.Create(); byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(Password); byte[] hash = mh.ComputeHash(inputBytes); StringBuilder sb = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { sb.Append(hash[i].ToString("x2")); } string newPassword = sb.ToString(); var taikhoan = new Taikhoan() { Username = Username, Password = newPassword, NgayTao = DateTime.Now, KichHoat = 0, LoaiTaiKhoanId = 1 }; //cập nhật mật khẩu mới dbContext.Taikhoan.Add(taikhoan); dbContext.SaveChanges(); var tk = (from x in dbContext.Taikhoan where x.Username == Username select x).ToList(); var nhanvien = new Nhanvien() { HoTen = model.HoTen, NgaySinh = model.NgaySinh, Email = model.Email, Sdt = model.Sdt, DiaChi = model.DiaChi, TaiKhoanId = tk[0].TaiKhoanId }; dbContext.Nhanvien.Add(nhanvien); dbContext.SaveChanges(); string success = "Thêm nhân viên thành công"; return(RedirectToAction("ThemNhanVien", "NhanVien", new{ success = success })); } string alert = "Thêm nhân viên không thành công"; return(RedirectToAction("ThemNhanVien", "NhanVien", new{ alert = alert })); }
public IActionResult SuaSanPham(Sanpham model, string TenAnh, string[] tenKichThuoc, float[] giaThem) { var dbContext = new shopContext(); if (ModelState.IsValid) { // kiểm tra hình ảnh đã có trong database chưa var dsHinhAnh = (from dsha in dbContext.Hinhanh where dsha.TenFile == TenAnh select dsha).ToList(); // thêm ảnh vào table hinhanh if (dsHinhAnh.Count != 0) { var hinhanh = new Hinhanh() { TenFile = TenAnh }; dbContext.Hinhanh.Add(hinhanh); dbContext.SaveChanges(); } //lấy id của ảnh var anh = (from ha in dbContext.Hinhanh where ha.TenFile == TenAnh select ha).ToList(); //sua sản phẩm var sanpham = dbContext.Sanpham.First(a => a.SanPhamId == model.SanPhamId); sanpham.TenSanPham = model.TenSanPham; sanpham.Mota = model.Mota; sanpham.GiaBanLe = model.GiaBanLe; sanpham.TrangThai = model.TrangThai; sanpham.PhanLoaiId = model.PhanLoaiId; sanpham.KhuyenMaiId = model.KhuyenMaiId; sanpham.HinhAnhId = anh[0].HinhAnhId; dbContext.SaveChanges(); //thêm kích thước vào bảng kichthuoc var x = (from sp in dbContext.Sanpham where sp.TenSanPham == model.TenSanPham select sp).ToList(); for (int i = 0; i < tenKichThuoc.Length; i++) { //lấy id của kích thước tương ứng với tên kích thước var kichthuoc1 = (from kt in dbContext.Kichthuoc where kt.TenKichThuoc == tenKichThuoc[i] && kt.SanPhamId == model.SanPhamId select kt).ToList(); var kichthuoc = dbContext.Kichthuoc.First(a => a.KichThuocid == kichthuoc1[0].KichThuocid); kichthuoc.GiaThem = giaThem[i]; dbContext.SaveChanges(); } return(RedirectToAction("Suasanpham", "sanpham", new { id = model.SanPhamId, success = "Sửa sản phẩm thành công" })); } return(RedirectToAction("Suasanpham", "sanpham", new { id = model.SanPhamId, error = "Sửa sản phẩm không thành công" })); }
public int AddProduct(Product product) { try { db.Product.Add(product); db.SaveChanges(); return(1); } catch { throw; } }
public int AddStore(Store store) { try { db.Store.Add(store); db.SaveChanges(); return(1); } catch { throw; } }
public int AddCustomer(Customer customer) { try { db.Customer.Add(customer); db.SaveChanges(); return(1); } catch { throw; } }
public int AddSale(Sales sale) { try { db.Sales.Add(sale); db.SaveChanges(); return(1); } catch { throw; } }
public IActionResult ThemSanPham(Sanpham model, string TenAnh, string[] tenKichThuoc, float[] giaThem) { var dbContext = new shopContext(); if (ModelState.IsValid) { // thêm ảnh vào table hinhanh var hinhanh = new Hinhanh() { TenFile = TenAnh }; dbContext.Hinhanh.Add(hinhanh); dbContext.SaveChanges(); //lấy id của ảnh var anh = (from ha in dbContext.Hinhanh where ha.TenFile == TenAnh select ha).ToList(); //thêm sản phẩm var sanpham = new Sanpham() { TenSanPham = model.TenSanPham, Mota = model.Mota, GiaBanLe = model.GiaBanLe, TrangThai = model.TrangThai, PhanLoaiId = model.PhanLoaiId, KhuyenMaiId = model.KhuyenMaiId, HinhAnhId = anh[0].HinhAnhId }; dbContext.Sanpham.Add(sanpham); dbContext.SaveChanges(); //thêm kích thước vào bảng kichthuoc var x = (from sp in dbContext.Sanpham where sp.TenSanPham == model.TenSanPham select sp).ToList(); for (int i = 0; i < tenKichThuoc.Length; i++) { var kichthuoc = new Kichthuoc() { SanPhamId = x[0].SanPhamId, TenKichThuoc = tenKichThuoc[i], GiaThem = giaThem[i] }; dbContext.Kichthuoc.Add(kichthuoc); dbContext.SaveChanges(); } return(RedirectToAction("Themsanpham", "sanpham", new { success = "Thêm sản phẩm thành công" })); } return(RedirectToAction("Themsanpham", "sanpham", new { error = "Thêm sản phẩm không thành công" })); }
public void YeuCauHuyDonHang(int mahd) { var dbContext = new shopContext(); var hoadon = dbContext.Hoadon.First(a => a.HoaDonId == mahd); hoadon.TrangThai = "Yêu cầu hủy"; dbContext.SaveChanges(); dbContext.Dispose(); }
public int AddToAddressDBOrGetID(Address data) { int?addressId = _dbContext.Addresses .Where(a => a.Country.Equals(data.Country) && a.City.Equals(data.City) && a.ZipCode.Equals(data.ZipCode) && a.Street.Equals(data.Street)) .Select(a => (int?)a.AddressId) .FirstOrDefault(); if (addressId.HasValue) { return(addressId.Value); } else { _dbContext.Addresses.Add(data); _dbContext.SaveChanges(); _dbContext.Entry(data).GetDatabaseValues(); return(data.AddressId); } }
public void Add(int orderId, string msg) { _ctx.Logs.Add(new Log() { OrderId = orderId, Msg = msg, Timestamp = DateTime.Now }); _ctx.SaveChanges(); }
public void XacNhanNhanHang(int mahd) { var dbContext = new shopContext(); var hoadon = dbContext.Hoadon.First(a => a.HoaDonId == mahd); hoadon.TrangThai = "Đã nhận hàng"; hoadon.NgayGiao = DateTime.Now; dbContext.SaveChanges(); dbContext.Dispose(); }
public IActionResult XoaBinhLuan(int id){ var dbContext = new shopContext(); // mục đích: lấy mã sản phẩm từ bình luận cần xóa var binhluan =( from bl in dbContext.Binhluan where bl.BinhLuanId == id select bl).ToList(); // lay danh sach phan hoi var phanhoi = (from ph in dbContext.Phanhoi where ph.BinhLuanId == id select ph).ToList(); // xoa phan hoi for(int i =0 ;i<phanhoi.Count;i++){ var removePhanHoi = dbContext.Phanhoi.First(a => a.PhanHoiId == phanhoi[i].PhanHoiId); dbContext.Phanhoi.Remove(removePhanHoi); dbContext.SaveChanges(); } // xóa bình luận var removeBinhLuan = dbContext.Binhluan.First(a => a.BinhLuanId == id); dbContext.Binhluan.Remove(removeBinhLuan); dbContext.SaveChanges(); return RedirectToAction("chitietsanpham","sanpham",new{id = binhluan[0].SanPhamId}); }
public void XacNhanHoaDon(int key) { var dbContext = new shopContext(); string username = HttpContext.Session.GetString("username1"); var taikhoan = (from tk in dbContext.Taikhoan where tk.Username == username select tk).ToList(); var nhanvien = (from nv in dbContext.Nhanvien where nv.TaiKhoanId == taikhoan[0].TaiKhoanId select nv).ToList(); var hoadon = dbContext.Hoadon.First(a => a.HoaDonId == key); hoadon.NhanVienId = nhanvien[0].NhanVienId; hoadon.TrangThai = "Đang giao hàng"; dbContext.SaveChanges(); }
public static void AddNewUser(shopContext shopContext, ApplicationDbContext applicationDbContext, string email) { var userAuth = applicationDbContext.Users.FirstOrDefault(u => u.Email.Equals(email)); if (userAuth != null) { User user = new User() { Email = email, UserAuthId = userAuth.Id }; shopContext.Users.Add(user); shopContext.SaveChanges(); } }
public static void AddOrderedBooksToDb(shopContext shopContext, int orderId, List <OrderedBook> basket) { foreach (var item in basket) { IList <BooksOrdered> booksOrdereds = new List <BooksOrdered>(); for (var i = 0; i < item.Quantity; i++) { var book = new BooksOrdered(); book.BookId = item.BookId; book.OrderId = orderId; booksOrdereds.Add(book); } shopContext.BooksOrdered.AddRange(booksOrdereds); shopContext.SaveChanges(); } }
public IActionResult SuaKhachHang(Khachhang model) { var dbContext = new shopContext(); if (ModelState.IsValid) { var khachhang = dbContext.Khachhang.First(a => a.KhachHangId == model.KhachHangId); khachhang.HoTen = model.HoTen; khachhang.Sdt = model.Sdt; khachhang.DiaChi = model.DiaChi; khachhang.Email = model.Email; khachhang.NgaySinh = model.NgaySinh; dbContext.SaveChanges(); return(RedirectToAction("suakhachhang", "khachhang", new{ id = model.KhachHangId, success = "Sửa thông tin khách hàng thành công" })); } return(RedirectToAction("suakhachhang", "khachhang", new{ id = model.KhachHangId, err = "Sửa thông tin khách hàng không thành công" })); }
public IActionResult XoaPhanHoi(int id){ var dbContext = new shopContext(); // lay danh sach phan hoi var phanhoi = (from ph in dbContext.Phanhoi join bl in dbContext.Binhluan on ph.BinhLuanId equals bl.BinhLuanId where ph.PhanHoiId == id select new{ phanHoiId = ph.PhanHoiId, sanphamId = bl.SanPhamId }).ToList(); // xoa phan hoi var removePhanHoi = dbContext.Phanhoi.First(a => a.PhanHoiId == phanhoi[0].phanHoiId); dbContext.Phanhoi.Remove(removePhanHoi); dbContext.SaveChanges(); return RedirectToAction("chitietsanpham","sanpham",new{id = phanhoi[0].sanphamId}); }
public IActionResult SuaNhanVien(Nhanvien model) { var dbContext = new shopContext(); if (ModelState.IsValid) { var nhanvien = dbContext.Nhanvien.First(a => a.NhanVienId == model.NhanVienId); nhanvien.HoTen = model.HoTen; nhanvien.Sdt = model.Sdt; nhanvien.DiaChi = model.DiaChi; nhanvien.Email = model.Email; nhanvien.NgaySinh = model.NgaySinh; dbContext.SaveChanges(); return(RedirectToAction("suanhanvien", "nhanvien", new{ id = model.NhanVienId, success = "Sửa thông tin nhân viên thành công" })); } return(RedirectToAction("suanhanvien", "nhanvien", new{ id = model.NhanVienId, err = "Sửa thông tin nhân viên không thành công" })); }
public IActionResult SuaKhuyenMai(Khuyenmai model) { var dbContext = new shopContext(); double?changePhanTramGiam = (double)model.PhanTramGiam / 100; if (ModelState.IsValid) { var khuyenmai = dbContext.Khuyenmai.First(a => a.KhuyenMaiId == model.KhuyenMaiId); khuyenmai.TenKhuyenMai = model.TenKhuyenMai; khuyenmai.MoTa = model.MoTa; khuyenmai.PhanTramGiam = (float?)changePhanTramGiam; khuyenmai.NgayBatDau = model.NgayBatDau; khuyenmai.NgayKetThuc = model.NgayKetThuc; khuyenmai.TrangThai = model.TrangThai; dbContext.SaveChanges(); return(RedirectToAction("SuaKhuyenMai", "Khuyenmai", new{ id = model.KhuyenMaiId, success = "Cập nhật khuyến mãi thành công" })); } return(RedirectToAction("SuaKhuyenMai", "Khuyenmai", new{ id = model.KhuyenMaiId, err = "Cập nhật khuyến mãi không thành công" })); }
public static void SaveBasketOfLoggedInUser(shopContext shopContext, List <OrderedBook> list, string authId) { int userId = GetUserIdById(shopContext, authId); var order = new Order(); order.UserId = userId; order.Draft = true; order.Date = DateTime.Now; //TODO: real addresses order.BillingAddressId = 1; order.ShippingAddressId = 1; shopContext.Orders.Add(order); shopContext.SaveChanges(); shopContext.Entry(order).GetDatabaseValues(); int orderId = order.OrderId; AddOrderedBooksToDb(shopContext, orderId, list); }
public IActionResult ThemKhuyenMai(Khuyenmai model) { var dbContext = new shopContext(); double?changePhanTramGiam = (double)model.PhanTramGiam / 100; if (ModelState.IsValid) { var Khuyenmai = new Khuyenmai() { TenKhuyenMai = model.TenKhuyenMai, MoTa = model.MoTa, NgayBatDau = model.NgayBatDau, NgayKetThuc = model.NgayKetThuc, PhanTramGiam = (float)changePhanTramGiam, TrangThai = model.TrangThai }; dbContext.Khuyenmai.Add(Khuyenmai); dbContext.SaveChanges(); } return(RedirectToAction("ThemKhuyenMai", "Khuyenmai", new{ success = "Thêm thành công" })); }
public IActionResult ThemBinhLuan(Binhluan model){ if (HttpContext.Session.GetString("username") == null) { return RedirectToAction("chitietsanpham","sanpham",new {id=model.SanPhamId}); } else{ if (ModelState.IsValid) { var dbContext = new shopContext(); var userName = HttpContext.Session.GetString("username"); var taiKhoan = (from tk in dbContext.Taikhoan where tk.Username == userName select tk).ToList(); var binhLuan = new Binhluan(){ SanPhamId = model.SanPhamId, TaiKhoanId = taiKhoan[0].TaiKhoanId, NoiDung = model.NoiDung }; dbContext.Binhluan.Add(binhLuan); dbContext.SaveChanges(); } return RedirectToAction("chitietsanpham","sanpham",new{id = model.SanPhamId}); } }
public void Save() { db.SaveChanges(); }
public IActionResult CheckOut(ShoppingCartViewModel scvm) { double totalPrice = scvm.TotalPrice(); if (!ModelState.IsValid) { return(View("ShoppingCart", scvm)); } Address billAdd = scvm.Order.BillingAddress; scvm.Order.BillingAddressId = AddToAddressDBOrGetID(billAdd); if (scvm.ShippingEqualBilling || scvm.Order.BillingAddress == scvm.Order.ShippingAddress) { scvm.Order.ShippingAddressId = scvm.Order.BillingAddressId; } else { Address shipAdd = scvm.Order.ShippingAddress; scvm.Order.ShippingAddressId = AddToAddressDBOrGetID(shipAdd); } User user = scvm.Order.User; int? userId = _dbContext.Users .Where(u => u.UserName.Equals(user.UserName) && u.Email.Equals(user.Email) && u.Phone.Equals(user.Phone)) .Select(u => (int?)u.UserId) .FirstOrDefault(); if (userId.HasValue) { scvm.Order.UserId = userId.Value; } else { _dbContext.Users.Add(user); _dbContext.SaveChanges(); _dbContext.Entry(user).GetDatabaseValues(); scvm.Order.UserId = user.UserId; } var order = scvm.Order; scvm.Order.User = null; scvm.Order.BillingAddress = null; scvm.Order.ShippingAddress = null; _dbContext.Orders.Add(order); _dbContext.SaveChanges(); _dbContext.Entry(order).GetDatabaseValues(); int orrderId = order.OrderId; foreach (var item in scvm.Basket) { var book = new BooksOrdered(); book.BookId = item.BookId; book.OrderId = orrderId; for (var i = 0; i < item.Quantity; i++) { _dbContext.BooksOrdereds.Add(book); } _dbContext.SaveChanges(); } return(RedirectToAction("Payment", new { orderId = orrderId, totalPrice = scvm.TotalPrice() })); }
public IActionResult Index() { var dbContext = new shopContext(); var khuyenmai = (from km in dbContext.Khuyenmai select km).ToList(); foreach (var item in khuyenmai) { if (item.NgayBatDau <= DateTime.Now && item.NgayKetThuc >= DateTime.Now) { //update khuyến mãi kích hoạt đã đến ngày khuyến mãi var km = dbContext.Khuyenmai.First(a => a.KhuyenMaiId == item.KhuyenMaiId); km.TrangThai = 1; dbContext.SaveChanges(); } else { var km = dbContext.Khuyenmai.First(a => a.KhuyenMaiId == item.KhuyenMaiId); km.TrangThai = 0; dbContext.SaveChanges(); //lấy danh sách sản phẩm có khuyến mãi id = item.khuyenmaiId; var sanpham = (from sp in dbContext.Sanpham where sp.KhuyenMaiId == item.KhuyenMaiId select sp).ToList(); //update sản phẩm đã hết hạn khuyến mãi foreach (var sp in sanpham) { var ItemSanPham = dbContext.Sanpham.First(a => a.SanPhamId == sp.SanPhamId); ItemSanPham.KhuyenMaiId = null; dbContext.SaveChanges(); } // var sanpham = dbContext.Sanpham.First(a => a.KhuyenMaiId == item.KhuyenMaiId); // sanpham.KhuyenMaiId = null; // dbContext.SaveChanges(); // dbContext.Dispose(); } } var SanPhamm = (from sp in dbContext.Sanpham join h in dbContext.Hinhanh on sp.HinhAnhId equals h.HinhAnhId join km in dbContext.Khuyenmai on sp.KhuyenMaiId equals km.KhuyenMaiId into x from subkm in x.DefaultIfEmpty() where sp.TrangThai == 1 select new{ SanPhamId = sp.SanPhamId, TenSanPham = sp.TenSanPham, Mota = sp.Mota, GiaBanLe = sp.GiaBanLe, Hinhanh = h.TenFile, KhuyenMaiId = sp.KhuyenMaiId, PhanTramGiam = subkm.PhanTramGiam }).Take(7); List <Sanpham> List = new List <Sanpham>(); foreach (var sp in SanPhamm) { Sanpham s = new Sanpham(); Hinhanh h = new Hinhanh(); Khuyenmai k = new Khuyenmai(); s.SanPhamId = sp.SanPhamId; s.TenSanPham = sp.TenSanPham; s.Mota = sp.Mota; s.GiaGoc = sp.GiaBanLe - sp.GiaBanLe * sp.PhanTramGiam; s.GiaBanLe = sp.GiaBanLe; s.KhuyenMaiId = sp.KhuyenMaiId; h.TenFile = sp.Hinhanh; //xử lý chuyển phần trăm giảm sang % k.PhanTramGiam = sp.PhanTramGiam * 100; s.KhuyenMai = k; s.HinhAnh = h; List.Add(s); } ViewData["x"] = List; return(View()); }
public shopContext GetTestShopContext() { var options = new DbContextOptionsBuilder <shopContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; var context = new shopContext(options); var genre1 = new Genre { GenreId = 1, GenreName = "Computing" }; var genre2 = new Genre { GenreId = 2, GenreName = "Travel & Holiday Guides" }; var genre3 = new Genre { GenreId = 3, GenreName = "Fantasy" }; var genre4 = new Genre { GenreId = 4, GenreName = "Historical" }; var genre5 = new Genre { GenreId = 5, GenreName = "Horror" }; var genre6 = new Genre { GenreId = 6, GenreName = "Romance" }; context.Genres.AddRange(genre1, genre2, genre3, genre4, genre5, genre6); var publisher1 = new Publisher { PublisherId = 1, PublisherName = "Springer Verlag Berlin and Heidelberg GmbH Co KG" }; var publisher2 = new Publisher { PublisherId = 2, PublisherName = "Neustar, Inc." }; var publisher3 = new Publisher { PublisherId = 3, PublisherName = "St Martins Press" }; var publisher4 = new Publisher { PublisherId = 4, PublisherName = "Penguin Books Ltd" }; var publisher5 = new Publisher { PublisherId = 5, PublisherName = "Wordsworth Editions Ltd" }; var publisher6 = new Publisher { PublisherId = 6, PublisherName = "Little Brown Book Group" }; context.Publishers.AddRange(publisher1, publisher2, publisher3, publisher4, publisher5, publisher6); var bookId01 = new Book { BookId = 1, Title = "C# (C Sharp Programming) : A Step by Step Guide for Beginners", GenreId = 1, PublisherId = 5, Price = 109.13f, Description = @"C# Have you always wanted to learn c sharp programming language but are afraid it'll be too difficult for you? Or perhaps you know other programming languages but are interested in learning C Sharp language fast? This book is for you. You no longer have to waste your time and money learning C# from boring books that are 600 pages long, expensive online courses or complicated C# tutorials that just leave you more confused. What this book offers... C Sharp for Beginners Complex concepts are broken down into simple steps to ensure that you can easily master the C# language even if you have never coded before. Carefully Chosen C# Examples Examples are carefully chosen to illustrate all concepts. In addition, the output for all examples are provided immediately so you do not have to wait till you have access to your computer to test the examples.", FigUrl = "https://d1w7fb2mkkr3kw.cloudfront.net/assets/images/book/lrg/9781/6934/9781693465185.jpg", Genre = genre1, Publisher = publisher5 }; var bookId02 = new Book { BookId = 2, Title = "The Atlas of Beauty : Women of the World in 500 Portraits", GenreId = 2, PublisherId = 4, Price = 98.67f, Description = @"Photographs and stories of 500 women from around the world, based on the author's hugely popular website. Since 2013 Mihaela Noroc has travelled the world with her backpack and camera taking photos of everyday women to showcase the diversity and beauty all around us. The Atlas of Beauty is a collection of her photographs that celebrates women from fifty countries across the globe and shows that beauty is everywhere, regardless of money, race or social status, and comes in many different sizes and colours. Mihaela's portraits feature women in their native environments, from the Amazon rain forest to markets in India, London city streets and parks in Harlem, creating a mirror of our varied cultures and proving that beauty has no rules. 'Stunning . . . aims to challenge the ideals of beauty dictated by the women's fashion magazine industry' Independent", FigUrl = "https://d1w7fb2mkkr3kw.cloudfront.net/assets/images/book/lrg/9781/8461/9781846149412.jpg", Genre = genre2, Publisher = publisher4 }; var bookId03 = new Book { BookId = 3, Title = "The House in the Cerulean Sea", GenreId = 3, PublisherId = 3, Price = 70.1, Description = @"Linus Baker leads a quiet, solitary life. At forty, he lives in a tiny house with a devious cat and his old records. As a Case Worker at the Department in Charge Of Magical Youth, he spends his days overseeing the well-being of children in government-sanctioned orphanages. When Linus is unexpectedly summoned by Extremely Upper Management he's given a curious and highly classified assignment: travel to Marsyas Island Orphanage, where six dangerous children reside: a gnome, a sprite, a wyvern, an unidentifiable green blob, a were-Pomeranian, and the Antichrist. Linus must set aside his fears and determine whether or not they're likely to bring about the end of days. But the children aren't the only secret the island keeps. Their caretaker is the charming and enigmatic Arthur Parnassus, who will do anything to keep his wards safe. As Arthur and Linus grow closer, long-held secrets are exposed, and Linus must make a choice: destroy a home or watch the world burn.", FigUrl = "https://d1w7fb2mkkr3kw.cloudfront.net/assets/images/book/lrg/9781/2502/9781250217318.jpg", Genre = genre3, Publisher = publisher3 }; var bookId04 = new Book { BookId = 4, Title = "Letters from a Stoic : Epistulae Morales Ad Lucilium", GenreId = 4, PublisherId = 2, Price = 44.67f, Description = @"'It is philosophy that has the duty of protecting us ... without it no one can lead a life free of fear or worry' For several years of his turbulent life, in which he was dogged by ill health, exile and danger, Seneca was the guiding hand of the Roman Empire. This selection of Seneca's letters shows him upholding the ideals of Stoicism - the wisdom of the self-possessed person immune to life's setbacks - while valuing friendship and courage, and criticizing the harsh treatment of slaves and the cruelties in the gladiatorial arena. The humanity and wit revealed in Seneca's interpretation of Stoicism is a moving and inspiring declaration of the dignity of the individual mind. ", FigUrl = "https://d1w7fb2mkkr3kw.cloudfront.net/assets/images/book/lrg/9780/1404/9780140442106.jpg", Genre = genre4, Publisher = publisher2 }; var bookId05 = new Book { BookId = 5, Title = "The Walking Dead Volume 5: The Best Defense", GenreId = 5, PublisherId = 1, Price = 61.34f, Description = @"The world we knew is gone. The world of commerce and frivolous necessity has been replaced by a world of survival and responsibility. An epidemic of apocalyptic proportions has swept the globe, causing the dead to rise and feed on the living. In a matter of months society has crumbled: no government, no grocery stores, no mail delivery, no cable TV. In a world ruled by the dead, the survivors are forced to finally start living. As the survivors settle into their prison home something has drawn them out into the open... out of the prison... out of their sanctuary. This is a major turning point for the overall story of The Walking Dead, setting the stage for years to come. Reprint Edition", FigUrl = "https://d1w7fb2mkkr3kw.cloudfront.net/assets/images/book/lrg/9781/5824/9781582406121.jpg", Genre = genre5, Publisher = publisher1 }; var bookId06 = new Book { BookId = 6, Title = "Peace Talks : The Dresden Files, Book Sixteen", GenreId = 5, PublisherId = 2, Price = 39.9, Description = @"HARRY DRESDEN IS BACK AND READY FOR ACTION, in the new entry in the #1 New York Times bestselling Dresden Files. When the Supernatural nations of the world meet up to negotiate an end to ongoing hostilities, Harry Dresden, Chicago's only professional wizard, joins the White Council's security team to make sure the talks stay civil. But can he succeed, when dark political manipulations threaten the very existence of Chicago - and all he holds dear? ", FigUrl = "https://d1w7fb2mkkr3kw.cloudfront.net/assets/images/book/lrg/9780/3565/9780356515298.jpg", Genre = genre5, Publisher = publisher2 }; var bookId07 = new Book { BookId = 7, Title = "The Second World War", GenreId = 4, PublisherId = 3, Price = 43.57f, Description = @"A magisterial, single-volume history of the greatest conflict the world has ever known by our foremost military historian. The Second World War began in August 1939 on the edge of Manchuria and ended there exactly six years later with the Soviet invasion of northern China. The war in Europe appeared completely divorced from the war in the Pacific and China, and yet events on opposite sides of the world had profound effects. Using the most up-to-date scholarship and research, Beevor assembles the whole picture in a gripping narrative that extends from the North Atlantic to the South Pacific and from the snowbound steppe to the North African Desert. Although filling the broadest canvas on a heroic scale, Beevor's The Second World War never loses sight of the fate of the ordinary soldiers and civilians whose lives were crushed by the titanic forces unleashed in this, the most terrible war in history.", FigUrl = "https://d1w7fb2mkkr3kw.cloudfront.net/assets/images/book/lrg/9781/7802/9781780225647.jpg", Genre = genre4, Publisher = publisher3 }; var bookId08 = new Book { BookId = 8, Title = "Harry Potter and the Goblet of Fire", GenreId = 3, PublisherId = 3, Price = 51.15, Description = @"The Triwizard Tournament is to be held at Hogwarts. Only wizards who are over seventeen are allowed to enter - but that doesn't stop Harry dreaming that he will win the competition. Then at Hallowe'en, when the Goblet of Fire makes its selection, Harry is amazed to find his name is one of those that the magical cup picks out. He will face death-defying tasks, dragons and Dark wizards, but with the help of his best friends, Ron and Hermione, he might just make it through - alive! These new editions of the classic and internationally bestselling, multi-award-winning series feature instantly pick-up-able new jackets by Jonny Duddle, with huge child appeal, to bring Harry Potter to the next generation of readers. It's time to PASS THE MAGIC ON ... ", FigUrl = "https://d1w7fb2mkkr3kw.cloudfront.net/assets/images/book/lrg/9781/4088/9781408855683.jpg", Genre = genre3, Publisher = publisher3 }; var bookId09 = new Book { BookId = 9, Title = "Rick Stein's India", GenreId = 2, PublisherId = 2, Price = 78.99f, Description = @"Whenever I hear the word curry, I'm filled with a longing for spicy hot food with the fragrance of cumin, cloves and cinnamon. I see deep red colours from lots of Kashmiri chillis, tinged with a suggestion of yellow from turmeric. I think of the tandoor oven, and slightly scorched naan shining with ghee and garlic. When Indians talk of their food, they talk about their life. To understand this country, you need to understand curry. What makes a good curry? Sensual spicy aromas or thick, creamy sauces? Rich, dark dals or crispy fried street snacks? Rick journeys through India to find the answer, searching this colourful, chaotic nation in search of the truths behind our love affair with its food.", FigUrl = "https://d1w7fb2mkkr3kw.cloudfront.net/assets/images/book/lrg/9781/8499/9781849905787.jpg", Genre = genre2, Publisher = publisher2 }; var bookId10 = new Book { BookId = 10, Title = "Pro C# 5.0 and the .NET 4.5 Framework", GenreId = 1, PublisherId = 1, Price = 93.39, Description = @"This new edition of Pro C# 5.0 and the .NET 4.5 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET Framework. You'll find new chapters covering all the important new features that make .NET 4.5 the most comprehensive release yet, including: .NET APIs for Windows 8 style UI apps New asynchronous task-based model for async operations How HTML5 support is being wrapped into C# web applications New programming interfaces for HTTP applications, including improved IPv6 support Expanded WPF, WCF and WF libraries giving C# more power than ever before This comes on top of award winning coverage of core C# features, both old and new, that have made the previous editions of this book so popular (you'll find everything from generics to pLINQ covered here).", FigUrl = "https://d1w7fb2mkkr3kw.cloudfront.net/assets/images/book/lrg/9781/4302/9781430242338.jpg", Genre = genre1, Publisher = publisher1 }; var bookId11 = new Book { BookId = 11, Title = "Bridgerton: When He Was Wicked: Inspiration for the Netflix Original Series Bridgerton", GenreId = 6, PublisherId = 6, Price = 38.45, Description = @"The sixth novel in Julia Quinn's globally beloved and bestselling Bridgerton Family series, set in Regency times and now a series created by Shonda Rhimes for Netflix. Welcome to Francesca's story . . . In every life there is a turning point. A moment so tremendous, so sharp and breath-taking, that one knows one's life will never be the same. For Michael Stirling, London's most infamous bachelor, that moment came the first time he laid eyes on Francesca Bridgerton. After a lifetime of chasing women, of smiling slyly as they chased him, of allowing himself to be caught but never permitting his heart to become engaged, he took one look at Francesca Bridgerton and fell so fast and hard into love it was a wonder he managed to remain standing. Unfortunately for Michael, however, Francesca's surname was to remain Bridgerton for only a mere thirty-six hours longer - the occasion of their meeting was, lamentably, a supper celebrating her imminent wedding to his cousin . . .", FigUrl = "https://d1w7fb2mkkr3kw.cloudfront.net/assets/images/book/lrg/9780/3494/9780349429472.jpg", Genre = genre6, Publisher = publisher6 }; context.Books.AddRange(bookId01, bookId02, bookId03, bookId04, bookId05, bookId06, bookId07, bookId08, bookId09, bookId10, bookId11); var user1 = new User { UserId = 1, UserName = null, Email = "*****@*****.**", Phone = null, UserAuthId = "6040582f-0c20-44dc-8acc-b684479454b0" }; var user2 = new User { UserId = 2, UserName = null, Email = "*****@*****.**", Phone = null, UserAuthId = "8befe3cc-024b-4084-ab38-8f5c7663cd1d" }; context.Users.AddRange(user1, user2); var address1 = new Address { AddressId = 1, Street = "Aspekt 20", City = "Warsaw", Country = "Poland", ZipCode = "01-904" }; var address2 = new Address { AddressId = 2, Street = "Pl. Zwyciestwa 12", City = "Cracow", Country = "Poland", ZipCode = "12-345" }; var address3 = new Address { AddressId = 3, Street = "Sokratesa 15", City = "Warsaw", Country = "Poland", ZipCode = "01-920" }; var address4 = new Address { AddressId = 4, Street = "Spacerowa 23", City = "Warsaw", Country = "Poland", ZipCode = "12-678" }; var address5 = new Address { AddressId = 5, Street = "Bergen 2", City = "Copenhagen", Country = "Denmark", ZipCode = "345-234" }; context.Addresses.AddRange(address1, address2, address3, address4, address5); var order1 = new Order { OrderId = 1, BillingAddressId = 1, ShippingAddressId = 1, UserId = 1, Payment = false, Draft = false, Date = new DateTime(2021, 2, 18, 16, 32, 15), Status = null, TotalPrice = 0.00, BillingAddress = address1, ShippingAddress = address1, User = user1 }; var order2 = new Order { OrderId = 2, BillingAddressId = 2, ShippingAddressId = 2, UserId = 2, Payment = false, Draft = false, Date = new DateTime(2021, 2, 20, 12, 30, 23), Status = null, TotalPrice = 0.00, BillingAddress = address2, ShippingAddress = address2, User = user2 }; var order3 = new Order { OrderId = 3, BillingAddressId = 3, ShippingAddressId = 3, UserId = 1, Payment = false, Draft = false, Date = new DateTime(2021, 2, 21, 9, 12, 20), Status = null, TotalPrice = 0.00, BillingAddress = address3, ShippingAddress = address3, User = user1 }; var order4 = new Order { OrderId = 4, BillingAddressId = 4, ShippingAddressId = 5, UserId = 2, Payment = false, Draft = false, Date = new DateTime(2021, 2, 25, 16, 35, 59), Status = null, TotalPrice = 0.00, BillingAddress = address4, ShippingAddress = address5, User = user2 }; context.Orders.AddRange(order1, order2, order3, order4); var booksOrdered01 = new BooksOrdered { BooksOrderedId = 1, OrderId = 1, BookId = 1, Book = bookId01, Order = order1 }; var booksOrdered02 = new BooksOrdered { BooksOrderedId = 2, OrderId = 1, BookId = 6, Book = bookId06, Order = order1 }; var booksOrdered03 = new BooksOrdered { BooksOrderedId = 3, OrderId = 2, BookId = 7, Book = bookId07, Order = order2 }; var booksOrdered04 = new BooksOrdered { BooksOrderedId = 4, OrderId = 2, BookId = 11, Book = bookId11, Order = order2 }; var booksOrdered05 = new BooksOrdered { BooksOrderedId = 5, OrderId = 2, BookId = 1, Book = bookId01, Order = order2 }; var booksOrdered06 = new BooksOrdered { BooksOrderedId = 6, OrderId = 2, BookId = 1, Book = bookId01, Order = order2 }; var booksOrdered07 = new BooksOrdered { BooksOrderedId = 7, OrderId = 3, BookId = 3, Book = bookId03, Order = order3 }; var booksOrdered08 = new BooksOrdered { BooksOrderedId = 8, OrderId = 3, BookId = 9, Book = bookId09, Order = order3 }; var booksOrdered09 = new BooksOrdered { BooksOrderedId = 9, OrderId = 3, BookId = 9, Book = bookId09, Order = order3 }; var booksOrdered10 = new BooksOrdered { BooksOrderedId = 10, OrderId = 4, BookId = 4, Book = bookId04, Order = order4 }; var booksOrdered11 = new BooksOrdered { BooksOrderedId = 11, OrderId = 4, BookId = 6, Book = bookId06, Order = order4 }; var booksOrdered12 = new BooksOrdered { BooksOrderedId = 12, OrderId = 4, BookId = 6, Book = bookId06, Order = order4 }; var booksOrdered13 = new BooksOrdered { BooksOrderedId = 13, OrderId = 4, BookId = 6, Book = bookId06, Order = order4 }; context.BooksOrdered.AddRange(booksOrdered01, booksOrdered02, booksOrdered03, booksOrdered04, booksOrdered05, booksOrdered06, booksOrdered07, booksOrdered08, booksOrdered09, booksOrdered10, booksOrdered11, booksOrdered12, booksOrdered13); var log01 = new Log { LogId = 1, OrderId = 1, Msg = "Order data accepted", Timestamp = new DateTime(2021, 02, 26, 11, 53, 08), Order = order1 }; var log02 = new Log { LogId = 2, OrderId = 1, Msg = "Start payment", Timestamp = new DateTime(2021, 02, 26, 11, 53, 30), Order = order1 }; var log03 = new Log { LogId = 3, OrderId = 1, Msg = "Payment accepted", Timestamp = new DateTime(2021, 02, 26, 11, 53, 45), Order = order1 }; var log04 = new Log { LogId = 4, OrderId = 1, Msg = "Sent order confirmation", Timestamp = new DateTime(2021, 02, 26, 11, 53, 55), Order = order1 }; var log05 = new Log { LogId = 5, OrderId = 2, Msg = "Order data accepted", Timestamp = new DateTime(2021, 02, 26, 11, 59, 56), Order = order2 }; var log06 = new Log { LogId = 6, OrderId = 2, Msg = "Start payment", Timestamp = new DateTime(2021, 02, 26, 12, 00, 5), Order = order2 }; var log07 = new Log { LogId = 7, OrderId = 2, Msg = "Payment accepted", Timestamp = new DateTime(2021, 02, 26, 12, 00, 28), Order = order2 }; var log08 = new Log { LogId = 8, OrderId = 2, Msg = "Sent order confirmation", Timestamp = new DateTime(2021, 02, 26, 12, 00, 31), Order = order2 }; var log09 = new Log { LogId = 9, OrderId = 3, Msg = "Order data accepted", Timestamp = new DateTime(2021, 02, 26, 12, 27, 21), Order = order3 }; var log10 = new Log { LogId = 10, OrderId = 3, Msg = "Start payment", Timestamp = new DateTime(2021, 02, 26, 12, 27, 38), Order = order3 }; var log11 = new Log { LogId = 11, OrderId = 3, Msg = "Payment accepted", Timestamp = new DateTime(2021, 02, 26, 12, 28, 44), Order = order3 }; var log12 = new Log { LogId = 12, OrderId = 3, Msg = "Sent order confirmation", Timestamp = new DateTime(2021, 02, 26, 12, 28, 49), Order = order3 }; var log13 = new Log { LogId = 13, OrderId = 4, Msg = "Order data accepted", Timestamp = new DateTime(2021, 02, 26, 17, 32, 11), Order = order4 }; var log14 = new Log { LogId = 14, OrderId = 4, Msg = "Start payment", Timestamp = new DateTime(2021, 02, 26, 17, 32, 27), Order = order4 }; var log15 = new Log { LogId = 15, OrderId = 4, Msg = "Payment accepted", Timestamp = new DateTime(2021, 02, 26, 17, 32, 51), Order = order4 }; var log16 = new Log { LogId = 16, OrderId = 4, Msg = "Sent order confirmation", Timestamp = new DateTime(2021, 02, 26, 17, 32, 53), Order = order4 }; context.Logs.AddRange(log01, log02, log03, log04, log05, log06, log07, log08, log09, log10, log11, log12, log13, log14, log15, log16); context.SaveChanges(); return(context); }
public IActionResult XacNhanDatHang(Hoadon model, string GiamGia) { var dbContext = new shopContext(); List <Chitiethoadon> ListChiTietHoaDon = JsonConvert.DeserializeObject <List <Chitiethoadon> >(HttpContext.Session.GetString("gioHangSession")); if (HttpContext.Session.GetString("username") == null || ListChiTietHoaDon.Count == 0) { return(RedirectToAction("XacNhanDatHang", "sanpham")); } else { //lay khachhangid var username = HttpContext.Session.GetString("username"); var TKId = (from tk in dbContext.Taikhoan join kh in dbContext.Khachhang on tk.TaiKhoanId equals kh.TaiKhoanId where tk.Username == username select kh).ToList(); int khId = TKId[0].KhachHangId; //lay ngay tao DateTime ngayTao = DateTime.Now; //lay quan var quan = (from q in dbContext.Phiship where q.PhiShipId == model.PhiShipId select q).ToList(); var TenQuan = quan[0].Quan; // lay tien ship var tienShip = quan[0].ChiPhi; float?tongTienChuaShip = 0; foreach (Chitiethoadon ct in ListChiTietHoaDon) { tongTienChuaShip = tongTienChuaShip + ct.TongTien; } //lay tien giam gia float?TienGiamGia = 0; if (GiamGia != null) { var magiamgia = (from gg in dbContext.Magiamgia where gg.MaGiamGia1 == GiamGia select gg).ToList(); if (magiamgia.Count == 0) { TienGiamGia = 0; } else { TienGiamGia = tongTienChuaShip * magiamgia[0].PhanTram; //xóa mã giảm giá khi đã được sử dụng var discount = dbContext.Magiamgia.First(a => a.MaGiamGiaId == magiamgia[0].MaGiamGiaId); dbContext.Magiamgia.Remove(discount); dbContext.SaveChanges(); } } //lay tong tien thanh toan float?TongTienThanhToan = tongTienChuaShip - TienGiamGia + tienShip; //insert hoa don var HoaDon = new Hoadon() { NgayTao = ngayTao, SoNha = model.SoNha, Quan = TenQuan, PhiShipId = model.PhiShipId, TongTienThanhToan = TongTienThanhToan, TrangThai = "Chờ xử lý", KhachHangId = khId, TenNguoiNhan = model.TenNguoiNhan, Sdt = model.Sdt, TongTienChuaShip = tongTienChuaShip, TienGiamGia = TienGiamGia // SessionId = sessionId }; dbContext.Hoadon.Add(HoaDon); dbContext.SaveChanges(); // lay mahoadon var hd = (from h in dbContext.Hoadon orderby h.HoaDonId descending select h).ToList(); var hoadonId = hd[0].HoaDonId; //insert chitiethoadon foreach (var item in ListChiTietHoaDon) { //lay phan tram giam gia cua san pham float?phanTramGiam = 0; var sp = (from x in dbContext.Sanpham join km in dbContext.Khuyenmai on x.KhuyenMaiId equals km.KhuyenMaiId where x.SanPhamId == item.SanPhamId select new{ khuyenmaiid = x.KhuyenMaiId, phantram = km.PhanTramGiam }).ToList(); if (sp.Count != 0) { phanTramGiam = sp[0].phantram; } // lay gia them cua moi kich thuoc san pham var kt = (from k in dbContext.Kichthuoc where k.KichThuocid == item.KichThuocId select k).ToList(); float?GiaThem = kt[0].GiaThem; var cthd = new Chitiethoadon() { HoaDonId = hoadonId, SanPhamId = item.SanPhamId, SoLuong = item.SoLuong, TienKhuyenMai = phanTramGiam * item.SanPham.GiaBanLe * item.SoLuong, TongTien = item.SanPham.GiaBanLe * item.SoLuong + item.SoLuong * GiaThem, TongSauKm = item.TongTien, KichThuocId = item.KichThuocId }; dbContext.Chitiethoadon.Add(cthd); dbContext.SaveChanges(); } // tạo mã giảm giá sau khi mua hàng string randomMaGiamGia = RandomString(6, true); var MaGiamGia = new Magiamgia() { MaGiamGia1 = randomMaGiamGia, PhanTram = (float?)0.05 }; dbContext.Magiamgia.Add(MaGiamGia); dbContext.SaveChanges(); // HttpContext.Session.Remove("idSession"); HttpContext.Session.Remove("gioHangSession"); ViewBag.maGiamGia = randomMaGiamGia; return(RedirectToAction("DatHangThanhCong", "sanpham", new { discountId = randomMaGiamGia })); } }