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 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 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 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); }
void lnkDeleteAllSelected_Click(object sender, EventArgs e) { string query = Request["cid"].ToString(); using (GeneralRepository <DAL.Sql.Modles.Course> rp = new GeneralRepository <DAL.Sql.Modles.Course>()) { if (query.Contains(',')) { string[] cids = query.Split(','); foreach (var i in cids) { int id = Convert.ToInt32(i); rp.Delete(x => x.CourseID == id); } } else { int id = Convert.ToInt32(query); rp.Delete(x => x.CourseID == id); } } PopulateControls(); }
void lnkDeleteSelected_Click(object sender, EventArgs e) { string cids = Request["cid"].ToString(); string[] ids = null; using (GeneralRepository <Category> repository = new GeneralRepository <Category>()) { if (cids.Contains(',')) { ids = cids.Split(','); foreach (string i in ids) { int id = Convert.ToInt32(i); repository.Delete(x => x.CategoryID == id); } } else { int id = Convert.ToInt32(cids); repository.Delete(x => x.CategoryID == id); } } PopulateControls(); }
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 void AddedTag(Product product, List <Guid> SelectedTags) { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <ProductTags> pdtagRepo = new GeneralRepository <ProductTags>(context); if (SelectedTags == null) { return; } var pTagList = pdtagRepo.GetAll().Where(x => x.ProductId == product.ProductId).Select(x => (Guid)x.TagId).ToList(); if (pTagList.Count == 0) { //create foreach (var t in SelectedTags) { ProductTags entity = new ProductTags() { ProductId = product.ProductId, TagId = t, Id = Guid.NewGuid() }; pdtagRepo.Create(entity); //context.ProductTags.Add(entity); } } else { //找出原本有選但現在沒選的Tag var newTagList1 = pTagList.Except(SelectedTags); //把現在沒選的Tag移除 foreach (var t in newTagList1) { var item = pdtagRepo.GetAll().Where(x => x.TagId == t && x.ProductId == product.ProductId).FirstOrDefault(); pdtagRepo.Delete(item); // context.ProductTags.Remove(item); } //找出現在有選但原本沒選的Tag var newTagList2 = SelectedTags.Except(pTagList); //加入沒選過的tag foreach (var t in newTagList2) { ProductTags entity = new ProductTags() { ProductId = product.ProductId, TagId = t, Id = Guid.NewGuid() }; pdtagRepo.Create(entity); } } pdtagRepo.SaveContext(); // context.SaveChanges(); }
/// <summary> /// 建立訂單 /// </summary> /// <param name="httpcontext"></param> /// <param name="order"></param> /// <param name="ecpayNumber"></param> /// <returns></returns> public OperationResult CreateOrder(HttpContextBase httpcontext, OrderViewModel order, string ecpayNumber) { 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 cartRepo = new GeneralRepository <Cart>(dbContext); var cartItemRepo = new GeneralRepository <CartItems>(dbContext); var productRepo = new GeneralRepository <Product>(dbContext); var couponRepo = new GeneralRepository <Coupons>(dbContext); Guid newOrderID = Guid.NewGuid(); var userId = httpcontext.User.Identity.GetUserId(); // 建立一筆新訂單 Order newOrder = new Order() { OrderId = newOrderID, EcpayOrderNumber = ecpayNumber, UserId = userId, OrderDate = DateTime.UtcNow, PurchaserName = order.PurchaserName, City = order.City, District = order.District, Road = order.Road, PurchaserEmail = order.PurchaserEmail, PurchaserPhone = order.PurchaserPhone, Paid = false, Payment = order.Payment, Build = true, Remember = order.Remember }; orderRepo.Create(newOrder); orderRepo.SaveContext(); // 先拿會員CartItems 裡資料 var cartItems = cartItemRepo.GetAll().Where(item => item.CartId.ToString() == userId).ToList(); var cart = cartRepo.GetFirst(item => item.CartId.ToString() == userId); var Coupon = couponRepo.GetFirst(item => item.CouponCode == order.Discount); foreach (var item in cartItems) { var products = productRepo.GetAll().Where(pd => pd.ProductId == item.ProductId); foreach (var p in products) { if (p.UnitInStock >= item.Quantity) { p.UnitInStock = p.UnitInStock - item.Quantity; OrderDetails orderDetails = new OrderDetails() { OrderId = newOrderID, ProductId = p.ProductId, Quantity = item.Quantity, }; if (Coupon != null) { orderDetails.Discount = Coupon.Id; } orderDetailRepo.Create(orderDetails); item.Quantity = 0; Debug.WriteLine(dbContext.Entry(p).State); } else { break; } } cartItemRepo.Delete(item); } cartRepo.Delete(cart); orderRepo.SaveContext(); operationResult.isSuccessful = true; transaction.Commit(); } catch (Exception ex) { operationResult.isSuccessful = false; operationResult.exception = ex; transaction.Rollback(); } return(operationResult); } }