public void Set_AddNull_NotStoredAndNotError() { bool error = false; List <GeneralClass> preList = null; List <GeneralClass> postList = null; try { GeneralClass item = null; var repository = new GeneralRepository(); repository.Reset(); preList = repository.GetAll().ToList(); repository.Set(item); postList = repository.GetAll().ToList(); } catch (Exception e) { error = true; } Assert.IsFalse(error); Assert.IsNotNull(preList); Assert.IsNotNull(postList); Assert.IsTrue(preList.Count() == 0); Assert.IsTrue(postList.Count() == 0); }
/// <summary> /// Transform All books' type to ProductDetailViewModel type /// </summary> /// <returns></returns> public static IEnumerable <ProductDetailViewModel> FindBookDetail() { var ProductRepo = new GeneralRepository <Product>(_context); var ProductImgsRepo = new GeneralRepository <ProductImgs>(_context); var CategoryRepo = new GeneralRepository <Category>(_context); var ProductTagsRepo = new GeneralRepository <ProductTags>(_context); var TagsRepo = new GeneralRepository <Tags>(_context); var Products = ProductRepo.GetAll().ToList().Select(x => new ProductDetailViewModel() { Name = x.Name, Price = x.Price, CategoryID = x.CategoryId.ToString(), ProductId = x.ProductId.ToString(), UnitInStock = x.UnitInStock, ISBN = x.ISBN, Publisher = x.Publisher, Description = x.Description, Specification = x.Specification, Author = x.Author, Intro = x.Intro, Language = x.Language, ImgLinks = ProductImgsRepo.GetAll().Where(q => q.ProductId == x.ProductId).Select(j => j.imgLink).ToList(), ImgLink = ProductImgsRepo.GetFirst(y => y.ProductId == x.ProductId).imgLink, PublishedDate = x.PublishedDate.ToString("yyyy/MM/dd"), CategoryName = CategoryRepo.GetFirst(z => z.CategoryId == x.CategoryId).Name, TagId = ProductTagsRepo.GetFirst(z => z.ProductId == x.ProductId).TagId.ToString(), TagName = TagsRepo.GetFirst(y => y.TagId == _context.ProductTags.FirstOrDefault(k => k.ProductId == x.ProductId).TagId).TagName }); return(Products); }
//刪除 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 OperationResult EmptyCart(string id) { OperationResult operationResult = new OperationResult(); using (var transaction = DbContext.Database.BeginTransaction()) { try { var CartItemsRepo = new GeneralRepository <CartItems>(DbContext); var GetUserKey = GetCookieKey(); var cartItems = CartItemsRepo.GetAll().Where(x => x.CartId.ToString() == GetUserKey && x.ProductId.ToString() == id).ToList(); foreach (var cartItem in cartItems) { CartItemsRepo.Delete(cartItem); } CartItemsRepo.SaveContext(); operationResult.isSuccessful = true; transaction.Commit(); return(operationResult); } catch (Exception ex) { operationResult.isSuccessful = false; operationResult.exception = ex; transaction.Rollback(); return(operationResult); } } }
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); }
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 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 { } }
/// <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); } }
/// <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); } }
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); }
/// <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 void Set_AddSameTwice_OnlyOneItem() { bool error = false; List <GeneralClass> list01 = null; Guid id = Guid.NewGuid(); var name = "Name 1"; try { var item = new GeneralClass { Id = id, Name = name }; var repository01 = new GeneralRepository(); repository01.Reset(); repository01.Set(item); repository01.Set(item); list01 = repository01.GetAll().ToList(); } catch (Exception e) { error = true; } Assert.IsFalse(error); Assert.IsNotNull(list01); Assert.IsTrue(list01.Count() == 1); }
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 List <Coupons> GetAllCoupons() { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Coupons> couponRepo = new GeneralRepository <Coupons>(context); var couponList = couponRepo.GetAll().ToList(); return(couponList); }
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 void Delete_AddTwoDeleteOne_RemainsOne() { bool error = false; List <GeneralClass> preList = null; List <GeneralClass> postList = null; Guid id01 = Guid.NewGuid(); var name01 = "Name 1"; Guid id02 = Guid.NewGuid(); var name02 = "Name 2"; try { var item01 = new GeneralClass { Id = id01, Name = name01 }; var item02 = new GeneralClass { Id = id02, Name = name02 }; var repository = new GeneralRepository(); repository.Reset(); repository.Set(item01); repository.Set(item02); preList = repository.GetAll().ToList(); repository.Delete(id02); postList = repository.GetAll().ToList(); } catch (Exception e) { error = true; } Assert.IsFalse(error); Assert.IsNotNull(preList); Assert.IsNotNull(postList); Assert.IsTrue(preList.Count() == 2); Assert.IsTrue(postList.Count() == 1); Assert.IsTrue(postList.First().Id == id01); Assert.IsTrue(postList.First().Name == name01); }
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(); }
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 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 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 void Set_AddOnlyOne_ContainsOnlyThat() { bool error = false; List <GeneralClass> preList = null; List <GeneralClass> postList = null; Guid id = Guid.NewGuid(); var name = "Name 1"; try { var item = new GeneralClass { Id = id, Name = name }; var repository = new GeneralRepository(); repository.Reset(); preList = repository.GetAll().ToList(); repository.Set(item); postList = repository.GetAll().ToList(); } catch (Exception e) { error = true; } Assert.IsFalse(error); Assert.IsNotNull(preList); Assert.IsNotNull(postList); Assert.IsTrue(preList.Count() == 0); Assert.IsTrue(postList.Count() == 1); Assert.IsTrue(postList.First().Id == id); Assert.IsTrue(postList.First().Name == name); }
public TitleData GetTitleData() { XbooxLibraryDBContext db = new XbooxLibraryDBContext(); GeneralRepository <AspNetUsers> mtable = new GeneralRepository <AspNetUsers>(db); GeneralRepository <Product> ptable = new GeneralRepository <Product>(db); GeneralRepository <Order> otable = new GeneralRepository <Order>(db); GeneralRepository <OrderDetails> odtable = new GeneralRepository <OrderDetails>(db); var revenue = (from o in otable.GetAll().AsEnumerable() join od in odtable.GetAll().AsEnumerable() on o.OrderId equals od.OrderId join p in ptable.GetAll().AsEnumerable() on od.ProductId equals p.ProductId where o.OrderDate.Month == DateTime.UtcNow.Month where o.Paid == true //paid 付款狀態 註解掉可以取得較多data select new { Revenue = p.Price * od.Quantity }).Sum(x => x.Revenue); var members = mtable.GetAll().Count(); var products = ptable.GetAll().Count(); var orders = otable.GetAll().Where(x => x.OrderDate.Month == DateTime.UtcNow.Month && x.Paid == true).Count(); TitleData title = new TitleData() { products = products, orders = orders, members = members, revenue = revenue }; return(title); }
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); }
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 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 <CartViewModel> GetCartItems(HttpContextBase context) { var watch = new Stopwatch(); watch.Start(); var GetUserKey = GetCookieKey(); var cartRepo = new GeneralRepository <Cart>(DbContext); var cartItemsRepo = new GeneralRepository <CartItems>(DbContext); var productRepo = new GeneralRepository <Product>(DbContext); var productImgRepo = new GeneralRepository <ProductImgs>(DbContext); using (var db = new XbooxLibraryDBContext()) { List <CartViewModel> CartItemList = new List <CartViewModel>(); var tempList = (from c in cartItemsRepo.GetAll() join p in productRepo.GetAll() on c.ProductId equals p.ProductId join i in productImgRepo.GetAll() on p.ProductId equals i.ProductId where p.ProductId == i.ProductId where c.CartId.ToString() == GetUserKey select new CartViewModel { ProductId = p.ProductId, ProductImgLink = i.imgLink, Name = p.Name, Price = p.Price, Quantity = c.Quantity, TotalPrice = c.Quantity * p.Price }).GroupBy(item => item.Name); foreach (var pdList in tempList) { var firstItem = pdList.FirstOrDefault(item => !item.Name.Contains("-0")); CartItemList.Add(firstItem); } watch.Stop(); Debug.WriteLine(watch.ElapsedMilliseconds); return(CartItemList); } }
public void Set_AddOnlyOneInOneWithTwoRepositories_BothContainingSame() { bool error = false; List <GeneralClass> list01 = null; List <GeneralClass> list02 = null; Guid id = Guid.NewGuid(); var name = "Name 1"; try { var item = new GeneralClass { Id = id, Name = name }; var repository01 = new GeneralRepository(); repository01.Reset(); var repository02 = new GeneralRepository(); repository02.Reset(); repository01.Set(item); list01 = repository01.GetAll().ToList(); list02 = repository02.GetAll().ToList(); } catch (Exception e) { error = true; } Assert.IsFalse(error); Assert.IsNotNull(list01); Assert.IsNotNull(list02); Assert.IsTrue(list01.Count() == 1); Assert.IsTrue(list02.Count() == 1); Assert.IsTrue(list01.First().Id == list02.First().Id); Assert.IsTrue(list01.First().Name == list02.First().Name); }