//刪除 public OperationResult DeleteOrder(string id) { OperationResult operationResult = new OperationResult(); var dbContext = new XbooxLibraryDBContext(); using (var transaction = dbContext.Database.BeginTransaction()) { try { var orderRepo = new GeneralRepository <Order>(dbContext); var orderDetailRepo = new GeneralRepository <OrderDetails>(dbContext); var productRepo = new GeneralRepository <Product>(dbContext); var orderDetails = orderDetailRepo.GetAll().Where(item => item.OrderId.ToString() == id); var order = orderRepo.GetFirst(item => item.OrderId.ToString() == id); if (order != null && orderDetails != null) { foreach (var item in orderDetails) { orderDetailRepo.Delete(item); } orderRepo.Delete(order); productRepo.SaveContext(); operationResult.IsSuccessful = true; transaction.Commit(); } } catch (Exception ex) { operationResult.IsSuccessful = false; operationResult.exception = ex; transaction.Rollback(); } } return(operationResult); }
public CartController() { if (context == null) { context = new XbooxLibraryDBContext(); } }
public OrderController() { orderservice = new OrderService(); shoppingCartService = new ShoppingCartService(); _context = new XbooxLibraryDBContext(); filterOrderService = new FilterOrderDataService(); }
public IEnumerable <SalesRevenueViewModel> GetSalesRevenue() { XbooxLibraryDBContext db = new XbooxLibraryDBContext(); GeneralRepository <Order> otable = new GeneralRepository <Order>(db); GeneralRepository <OrderDetails> odtable = new GeneralRepository <OrderDetails>(db); GeneralRepository <Product> ptable = new GeneralRepository <Product>(db); var temp = from od in odtable.GetAll().AsEnumerable() join o in otable.GetAll().AsEnumerable() on od.OrderId equals o.OrderId join p in ptable.GetAll().AsEnumerable() on od.ProductId equals p.ProductId where o.Paid == true //paid 付款狀態 註解掉可以取得較多data select new { Year = o.OrderDate.Year, Month = o.OrderDate.Month, Total = p.Price * od.Quantity }; var Revenue = from t in temp group t by new { t.Month, t.Year } into g select new SalesRevenueViewModel { Year = g.Key.Year, Month = g.Key.Month, Revenue = g.Sum(x => x.Total) }; return(Revenue); }
/// <summary> /// 取得所有訂單 /// </summary> /// <returns></returns> public List <OrderViewModel> GetOrder() { using (var dbContext = new XbooxLibraryDBContext()) { var orderRepo = new GeneralRepository <Order>(dbContext); var userRepo = new GeneralRepository <AspNetUsers>(dbContext); var orderList = (from o in orderRepo.GetAll().AsEnumerable() join user in userRepo.GetAll().AsEnumerable() on o.UserId equals user.Id select new OrderViewModel { OrderId = o.OrderId, OrderDate = (DateTime)TimeCheckerService.GetTaipeiTime(o.OrderDate), UserName = user.UserName, PurchaserName = o.PurchaserName, PurchaserEmail = o.PurchaserEmail, PurchaserAddress = $"{o.City}{o.District}{o.Road}", PurchaserPhone = o.PurchaserPhone, Payment = o.Payment, Paid = o.Paid, PayDate = TimeCheckerService.GetTaipeiTime(o.PayDate), Build = o.Build }).OrderBy(item => item.OrderDate).ToList(); return(orderList); } }
public IQueryable <TopProducts> GetTopProducts() { XbooxLibraryDBContext db = new XbooxLibraryDBContext(); GeneralRepository <Order> otable = new GeneralRepository <Order>(db); GeneralRepository <OrderDetails> odtable = new GeneralRepository <OrderDetails>(db); GeneralRepository <Product> ptable = new GeneralRepository <Product>(db); var temp = from od in odtable.GetAll() join o in otable.GetAll() on od.OrderId equals o.OrderId join p in ptable.GetAll() on od.ProductId equals p.ProductId where o.Paid == true //paid 付款狀態 註解掉可以取得較多data select new { Year = o.OrderDate.Year, Month = o.OrderDate.Month, Name = p.Name, Quantity = od.Quantity }; var topproducts = from t in temp group t by new { t.Year, t.Month, t.Name } into g select new TopProducts { Month = g.Key.Month, Name = g.Key.Name, Quantity = g.Sum(x => x.Quantity) }; return(topproducts); }
public void Delete(Guid id) { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Product> repository = new GeneralRepository <Product>(context); GeneralRepository <ProductImgs> Imgrepository = new GeneralRepository <ProductImgs>(context); GeneralRepository <ProductTags> Ptrepository = new GeneralRepository <ProductTags>(context); var deleteProduct = repository.GetFirst(x => x.ProductId == id); var deleteImg = Imgrepository.GetAll().Where(x => x.ProductId == id); var deleteTag = Ptrepository.GetAll().Where(x => x.ProductId == id); if (deleteImg != null) { foreach (var i in deleteImg) { Imgrepository.Delete(i); } } if (deleteTag != null) { foreach (var i in deleteTag) { Ptrepository.Delete(i); } } //刪除product 刪除 tag repository.Delete(deleteProduct); context.SaveChanges(); }
public void RemoveImg(string imgName) { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Product> repository = new GeneralRepository <Product>(context); GeneralRepository <ProductImgs> Imgrepository = new GeneralRepository <ProductImgs>(context); var charac = imgName.Split('"'); foreach (var i in charac) { Debug.WriteLine(i); } var temp = charac[1]; var imgToDelete = Imgrepository.GetAll().FirstOrDefault(x => x.imgLink == temp); if (imgToDelete != null) { Imgrepository.Delete(imgToDelete); Imgrepository.SaveContext(); } else { } }
public OperationResult CouponsCreate(Coupons input) { var result = new OperationResult(); try { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Coupons> couRepo = new GeneralRepository <Coupons>(context); Coupons entity = new Coupons() { Id = Guid.NewGuid(), CouponName = input.CouponName, Discount = input.Discount, CouponCode = input.CouponCode, StartTime = input.StartTime, EndTime = input.EndTime }; couRepo.Create(entity); couRepo.SaveContext(); result.IsSuccessful = true; } catch (Exception ex) { result.IsSuccessful = false; result.exception = ex; } return(result); }
/// <summary> /// 利用訂單Id取訂單 /// </summary> /// <param name="httpContext"></param> /// <param name="orderId"></param> /// <returns></returns> public List <OrderViewModel> GetOrder(HttpContextBase httpContext, string orderId) { using (var dbContext = new XbooxLibraryDBContext()) { var orderRepo = new GeneralRepository <Order>(dbContext); var userRepo = new GeneralRepository <AspNetUsers>(dbContext); var userId = httpContext.User.Identity.GetUserId(); var orderList = (from o in orderRepo.GetAll().AsEnumerable() where o.UserId == userId && o.OrderId.ToString() == orderId join user in userRepo.GetAll().AsEnumerable() on o.UserId equals user.Id select new OrderViewModel { OrderId = o.OrderId, EcpayOrderNumber = o.EcpayOrderNumber, OrderDate = (DateTime)TimeCheckerService.GetTaipeiTime(o.OrderDate), UserName = user.UserName, PurchaserName = o.PurchaserName, PurchaserEmail = o.PurchaserEmail, City = o.City, District = o.District, Road = o.Road, PurchaserPhone = o.PurchaserPhone, Payment = o.Payment, PayDate = TimeCheckerService.GetTaipeiTime(o.PayDate), Paid = o.Paid, Build = o.Build }).OrderBy(item => item.OrderDate).ToList(); return(orderList); } }
/// <summary> /// 編輯當筆訂單的付款狀態(ecpayNumber) /// </summary> /// <param name="ecpayNumber"></param> /// <returns></returns> public OperationResult EditPaidStateByEcNumber(string ecpayNumber) { OperationResult operationResult = new OperationResult(); using (var dbContext = new XbooxLibraryDBContext()) { try { var orderRepo = new GeneralRepository <Order>(dbContext); var order = orderRepo.GetFirst(x => x.EcpayOrderNumber.ToString() == ecpayNumber); if (order != null) { if (!order.Paid) { order.Paid = true; order.PayDate = DateTime.UtcNow; } else { order.Paid = false; } orderRepo.SaveContext(); operationResult.isSuccessful = true; } } catch (Exception ex) { operationResult.isSuccessful = false; operationResult.exception = ex; } return(operationResult); } }
/// <summary> /// 拿到記住訂單的資訊 /// </summary> /// <param name="httpContext"></param> /// <returns></returns> public OrderViewModel GetRecordInfo(HttpContextBase httpContext) { using (var dbContext = new XbooxLibraryDBContext()) { var orderRepo = new GeneralRepository <Order>(dbContext); var userRepo = new GeneralRepository <AspNetUsers>(dbContext); var userId = httpContext.User.Identity.GetUserId(); var orderList = orderRepo.GetAll() .Where(item => item.UserId == userId) .OrderByDescending(item => item.OrderDate); var order = orderList.FirstOrDefault(); OrderViewModel orderInfo = new OrderViewModel(); if (order != null) { if (order.Remember) { orderInfo.PurchaserName = order.PurchaserName; orderInfo.City = order.City; orderInfo.District = order.District; orderInfo.Road = order.Road; orderInfo.PurchaserEmail = order.PurchaserEmail; orderInfo.PurchaserPhone = order.PurchaserPhone; orderInfo.Remember = order.Remember; } } return(orderInfo); } }
/// <summary> /// 拿到訂單裡的產品資訊 /// </summary> /// <param name="orderId"></param> /// <returns></returns> public List <OrderDetailsViewModel> GetOrderDetails(string orderId) { using (var dbContext = new XbooxLibraryDBContext()) { var orderDetailRepo = new GeneralRepository <OrderDetails>(dbContext); var productRepo = new GeneralRepository <Product>(dbContext); var imgRepo = new GeneralRepository <ProductImgs>(dbContext); var CouponRepo = new GeneralRepository <Coupons>(dbContext); List <OrderDetailsViewModel> orderDetailsList = new List <OrderDetailsViewModel>(); // 因為多張圖片會重複產品 var tempList = (from od in orderDetailRepo.GetAll().AsEnumerable() where od.OrderId.ToString() == orderId join pd in productRepo.GetAll().AsEnumerable() on od.ProductId equals pd.ProductId join pi in imgRepo.GetAll().AsEnumerable() on pd.ProductId equals pi.ProductId where pd.ProductId == pi.ProductId select new OrderDetailsViewModel { Imagelink = pi.imgLink, Name = pd.Name, Quantity = od.Quantity, Price = pd.Price, Coupon = CouponRepo.GetFirst(item => item.Id == od.Discount), Total = Math.Round(pd.Price * od.Quantity) }).GroupBy(item => item.Name); foreach (var productList in tempList) { var firstProductItem = productList.FirstOrDefault(item => !item.Imagelink.Contains("-0")); orderDetailsList.Add(firstProductItem); } return(orderDetailsList); } }
public List <ProductListViewModel> GetProducts() { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Product> prepository = new GeneralRepository <Product>(context); // var tagList = Tagrepository.GetAll().ToList(); var pList = prepository.GetAll().Select(item => new ProductListViewModel { Name = item.Name, Author = item.Author, CategoryId = item.CategoryId, Intro = item.Intro, ISBN = item.ISBN, Price = item.Price, PublishedDate = item.PublishedDate, Publisher = item.Publisher, UnitInStock = item.UnitInStock, Specification = item.Specification, ProductId = item.ProductId, Description = item.Description, Language = item.Language }).ToList(); return(pList); }
public List <Coupons> GetAllCoupons() { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Coupons> couponRepo = new GeneralRepository <Coupons>(context); var couponList = couponRepo.GetAll().ToList(); return(couponList); }
public NavBarHelper() { if (SideBarCategories == null) { XbooxLibraryDBContext db = new XbooxLibraryDBContext(); SideBarCategories = new GeneralRepository <Category>(db); } }
public GeneralRepository(XbooxLibraryDBContext contexts) { if (contexts == null) { throw new ArgumentNullException(); } context = contexts; }
public Product GetSingleProduct(Guid id) { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Product> repository = new GeneralRepository <Product>(context); var temp = repository.GetAll().FirstOrDefault(x => x.ProductId == id); var temps = repository.GetFirst(x => x.ProductId == id); return(temps); }
public List <string> GetPictures(Guid id) { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Product> repository = new GeneralRepository <Product>(context); GeneralRepository <ProductImgs> Imgrepository = new GeneralRepository <ProductImgs>(context); var imgList = Imgrepository.GetAll().Where(x => x.ProductId == id).Select(x => x.imgLink).ToList(); return(imgList); }
public void EditUserDetails(UserDetails userdetails) { XbooxLibraryDBContext db = new XbooxLibraryDBContext(); GeneralRepository <AspNetUsers> user = new GeneralRepository <AspNetUsers>(db); var edit = user.GetAll().FirstOrDefault(u => u.Id == userdetails.Id); edit.Email = userdetails.Email; edit.PhoneNumber = userdetails.Phone; user.Update(edit); user.SaveContext(); }
private void PutImgs(Product product) { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <ProductImgs> ImgRepo = new GeneralRepository <ProductImgs>(context); // var imgList = getImg().Split(',').Where(x=>x!="").ToList(); //用list傳的 foreach (var i in GetImg()) { ProductImgs productImgs = new ProductImgs() { // ProductImgId = 0, imgLink = i, ProductId = product.ProductId, }; ImgRepo.Create(productImgs); // context.ProductImgs.Add(productImgs); } // 用string 傳的 // foreach (var i in imgList) // { // productImgs = new ProductImgs() //// { // ProductImgId = 0, // imgLink = i, // ProductId = product.ProductId, // }; // context.ProductImgs.Add(productImgs); //productImgs.Add(new ProductImgs() { imgLink = i.ToString(), ProductId = product.ProductId }); // } // context.SaveChanges(); // var PiList = new List<ProductImgs>(); // var img = productImgs /// var productImgs = productImgs.Where(x => x.ProductId == product.ProductId); // freach (var i in productImgs) // { // product.ProductImgId = i.ProductImgId + ","; //product.ProductImgId = Convert.ToInt64(product.ProductImgId) + ","; // } // imgString = null; ImgstringList = null; ImgRepo.SaveContext(); }
public List <CreateListViewModel.CategoryViewModel> GetCatecory() { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Category> Categoryrepository = new GeneralRepository <Category>(context); var cateList = Categoryrepository.GetAll().Select(item => new CreateListViewModel.CategoryViewModel { Name = item.Name, CategoryId = item.CategoryId }).ToList(); return(cateList); }
public List <TagViewModel> GetTags() { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Tags> Tagrepository = new GeneralRepository <Tags>(context); // var tagList = Tagrepository.GetAll().ToList(); var tagList = Tagrepository.GetAll().Select(item => new TagViewModel { TagId = item.TagId, TagName = item.TagName }).ToList(); return(tagList); }
//取消訂單 public OperationResult CancelOrder(string id) { OperationResult operationResult = new OperationResult(); var dbContext = new XbooxLibraryDBContext(); using (var transaction = dbContext.Database.BeginTransaction()) { try { var orderRepo = new GeneralRepository <Order>(dbContext); var orderDetailRepo = new GeneralRepository <OrderDetails>(dbContext); var productRepo = new GeneralRepository <Product>(dbContext); var orderDetails = orderDetailRepo.GetAll().Where(item => item.OrderId.ToString() == id); var order = orderRepo.GetFirst(item => item.OrderId.ToString() == id); if (order != null && orderDetails != null) { if (order.Paid == false) { order.Build = false; foreach (var item in orderDetails) { var products = productRepo.GetAll().Where(pd => pd.ProductId == item.ProductId).OrderBy(pd => pd.PublishedDate); foreach (var pd in products) { if (item.Quantity <= 0) { break; } else { pd.UnitInStock = pd.UnitInStock + item.Quantity; } } } productRepo.SaveContext(); operationResult.IsSuccessful = true; transaction.Commit(); } } } catch (Exception ex) { operationResult.IsSuccessful = false; operationResult.exception = ex; transaction.Rollback(); } } return(operationResult); }
public List <TagViewModel> GetTags() { var result = new TagViewModel(); XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Tags> repository = new GeneralRepository <Tags>(context); var temp = from t in repository.GetAll() select new TagViewModel() { TagId = t.TagId, TagName = t.TagName }; return(temp.ToList()); }
public static UserDetails GetUserDetails(HttpContextBase context) { XbooxLibraryDBContext db = new XbooxLibraryDBContext(); GeneralRepository <AspNetUsers> user = new GeneralRepository <AspNetUsers>(db); var details = (from u in user.GetAll() where u.UserName == context.User.Identity.Name select new UserDetails { Id = u.Id, Account = u.UserName, Email = u.Email, Phone = u.PhoneNumber }).FirstOrDefault(); return(details); }
//trycatch public OperationResult Create(CreateDataModel input) { var result = new OperationResult(); try { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Product> repository = new GeneralRepository <Product>(context); //tag && img Product entity = new Product() { ProductId = Guid.NewGuid(), Name = input.Name, CategoryId = input.CategoryId, ISBN = input.ISBN, Author = input.Author, Specification = input.Specification, Intro = input.Intro, Language = input.Language, UnitInStock = input.UnitInStock, PublishedDate = input.PublishedDate, Description = input.Description, Price = input.Price }; PutImgs(entity); //加入Img repository.Create(entity); repository.SaveContext(); //加入tag AddedTag(entity, input.PostedTagIds); //context.SaveChanges(); // repository.SaveContext(); result.IsSuccessful = true; } catch (Exception ex) { result.IsSuccessful = false; result.exception = ex; } return(result); }
public List <UserListViewModel> GetAllUsers() { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); var userrepo = new GeneralRepository <AspNetUsers>(context); var userList = userrepo.GetAll().ToList(); List <UserListViewModel> viewModel = new List <UserListViewModel>(); foreach (var item in userList) { viewModel.Add(new UserListViewModel() { Id = item.Id, Email = item.Email, UserName = item.UserName, PhoneNumber = item.PhoneNumber }); } return(viewModel); }
public List <TagViewModel> GetSelectedTags(Product product) { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Product> Pdrepository = new GeneralRepository <Product>(context); GeneralRepository <Tags> Tagrepository = new GeneralRepository <Tags>(context); GeneralRepository <ProductTags> ProductTagrepository = new GeneralRepository <ProductTags>(context); List <TagViewModel> TagLists = new List <TagViewModel>(); var temps = ProductTagrepository.GetAll().Where(x => x.ProductId == product.ProductId).Select(x => x.TagId).ToList(); foreach (var t in temps) { TagLists.Add(new TagViewModel() { TagId = (Guid)t, TagName = context.Tags.Where(x => x.TagId == t).Select(x => x.TagName).FirstOrDefault() }); } ; return(TagLists); }
public OperationResult CouponsEdit(Coupons input) { var result = new OperationResult(); try { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Coupons> couRepo = new GeneralRepository <Coupons>(context); couRepo.Update(input); couRepo.SaveContext(); result.IsSuccessful = true; } catch (Exception ex) { result.IsSuccessful = false; result.exception = ex; } return(result); }