public OperationResult AddToCart() { OperationResult operationResult = new OperationResult(); using (var transaction = DbContext.Database.BeginTransaction()) { try { var CartRepo = new GeneralRepository <Cart>(DbContext); var GetUserKey = GetCookieKey(); var cart = CartRepo.GetFirst(x => x.CartId.ToString() == GetUserKey); if (cart == null) { cart = new Cart { CartId = Guid.Parse(GetUserKey), UserId = HttpContext.Current.User.Identity.Name }; CartRepo.Create(cart); } CartRepo.SaveContext(); operationResult.isSuccessful = true; transaction.Commit(); return(operationResult); } catch (Exception ex) { operationResult.isSuccessful = false; operationResult.exception = ex; transaction.Rollback(); return(operationResult); } } }
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); }
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(); }
//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 OperationResult AddToCartItems(string values) { OperationResult operationResult = new OperationResult(); using (var transaction = DbContext.Database.BeginTransaction()) { try { var GetUserKey = GetCookieKey(); var CartItems = JsonConvert.DeserializeObject <List <TempCartItems> >(values); GeneralRepository <CartItems> CartItemsRepo = new GeneralRepository <CartItems>(DbContext); GeneralRepository <Product> ProductRepo = new GeneralRepository <Product>(DbContext); foreach (var item in CartItems) { var ProductCheck = CartItemsRepo.GetFirst(x => x.ProductId.ToString() == item.ProductId && x.CartId.ToString() == GetUserKey); if (ProductCheck == null) { Guid randomId = Guid.NewGuid(); CartItems cartItem = new CartItems { CartId = Guid.Parse(GetUserKey), ProductId = Guid.Parse(item.ProductId), Quantity = item.Count, Id = randomId }; CartItemsRepo.Create(cartItem); } else { ProductCheck.Quantity = item.Count; } } CartItemsRepo.SaveContext(); operationResult.isSuccessful = true; transaction.Commit(); } catch (Exception ex) { operationResult.isSuccessful = false; operationResult.exception = ex; transaction.Rollback(); } } return(operationResult); }
private void PutImgs(Product product) { var productImgs = new ProductImgs(); 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 = new ProductImgs() { // ProductImgId = 0, imgLink = i, ProductId = product.ProductId, }; Imgrepo.Create(productImgs); } ImgstringList = null; Imgrepo.SaveContext(); }
public OperationResult Create(Tags input) { var result = new OperationResult(); try { XbooxLibraryDBContext context = new XbooxLibraryDBContext(); GeneralRepository <Tags> repository = new GeneralRepository <Tags>(context); Tags entity = new Tags() { TagId = Guid.NewGuid(), TagName = input.TagName }; repository.Create(entity); repository.SaveContext(); result.IsSuccessful = true; } catch (Exception ex) { result.IsSuccessful = false; result.exception = ex; } return(result); }
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); } }