//post addto cart details public int PostCartDetails(AddToCartDto dtls) { try { var result = (from r in appDbContext.AddToCart where (r.ProductId == dtls.ProductId && r.OrderDetailsId == dtls.OrderDetailsId && r.IsPurchased == false) select r).ToList(); if (result.Count == 0) { var product = appDbContext.Product.Where(x => x.Id == dtls.ProductId).FirstOrDefault(); var price = product.Price; var qnty = dtls.Quantity; var totalPrice = Convert.ToDouble(price) * qnty; var info = new AddToCart { OrderDetailsId = dtls.OrderDetailsId, ProductId = dtls.ProductId, Quantity = dtls.Quantity, TotalPrice = Convert.ToString(totalPrice), }; appDbContext.AddToCart.Add(info); appDbContext.SaveChanges(); return(1); } else { return(0); } } catch (Exception ex) { throw; } }
public async Task <BaseDtoResponse <AddToCartDto> > Add(AddToCartRequest request) { try { Product product = await _productRepository.GetById(request.ProductId); if (product != null) { CartItem item = _mapper.Map <AddToCartRequest, CartItem>(request); item.Product = product; CartItem carItem = await _cartItemRepository.Add(item); if (carItem == null) { return(new BaseDtoResponse <AddToCartDto>("Error occured while creating the CartItem, try again")); } AddToCartDto response = _mapper.Map <CartItem, AddToCartDto>(carItem); return(new BaseDtoResponse <AddToCartDto>(response)); } else { return(new BaseDtoResponse <AddToCartDto>("Unable to find product in the Catalog, please try again")); } //IList<CartItem> items = await _cartItemRepository.List(new ItemsInCartForUserSpecification(request.IdentityId)); //if (items != null) //{ // CartItem sameProduct = items.Cast<CartItem>().SingleOrDefault(e => e.ProductId == request.ProductId); //} } catch (Exception ex) { return(new BaseDtoResponse <AddToCartDto>(ex.Message)); } }
public async Task <BaseDtoListResponse <AddToCartDto> > ListAllByUserId(string identityId) { try { IList <CartItem> items = await _cartItemRepository.List(new ItemsInCartForUserSpecification(identityId)); List <AddToCartDto> cartItems = new List <AddToCartDto>(); if (items != null) { foreach (CartItem item in items) { AddToCartDto c = _mapper.Map <CartItem, AddToCartDto>(item); cartItems.Add(c); } return(new BaseDtoListResponse <AddToCartDto>(cartItems)); } else { return(new BaseDtoListResponse <AddToCartDto>("Cannot find any items in the cart for the user")); } } catch (Exception ex) { return(new BaseDtoListResponse <AddToCartDto>(ex.Message)); } }
public ActionResult AddToCart(AddToCartDto postModel) { try { using (var uow = _uowProvider.Create()) { var store = CurrentPage.GetStore(); var order = _sessionManager.GetOrCreateCurrentOrder(store.Id) .AsWritable(uow) .AddProduct(postModel.ProductReference, postModel.ProductVariantReference, 1); _orderService.SaveOrder(order); uow.Complete(); } } catch (ValidationException ex) { ModelState.AddModelError("productReference", "Failed to add product to cart"); return(CurrentUmbracoPage()); } TempData["addedProductReference"] = postModel.ProductReference; return(RedirectToCurrentUmbracoPage()); }
public static Cart ToEntity(this AddToCartDto source) { return(new Cart { ProductId = source.ProductId, UserId = source.UserId }); }
//! Akcja ActionResult typu HttpGet. /*! * Służy do usunięcia pozycji z koszyka zamówień. */ public ActionResult Remove(AddToCartDto cartItem) { List <AddToCartDto> cartItems = (List <AddToCartDto>)Session["cart"]; cartItems.RemoveAll(x => x.ID == cartItem.ID); Session["cart"] = cartItems; Session["count"] = Convert.ToInt32(Session["count"]) - 1; return(RedirectToAction("UserCart", "Cart")); }
public IActionResult delete(AddToCartDto delt) { try { return(Ok(productService.deleteFromCart(delt))); } catch (Exception ex) { logger.LogError(ex, ex.Message); return(StatusCode(500)); throw; } }
public string deleteFromCart(AddToCartDto delt) { try { var findElement = appDbContext.AddToCart.Where(x => x.ProductId == delt.ProductId && x.OrderDetailsId == delt.OrderDetailsId && x.IsPurchased == false).FirstOrDefault(); var remove = appDbContext.AddToCart.Remove(findElement); appDbContext.SaveChanges(); return("true"); } catch { return("false"); } }
public ProductDetailsDto() { DefaultPictureModel = new PictureDto(); PictureModels = new List <PictureDto>(); GiftCard = new GiftCardDto(); ProductPrice = new ProductPriceDto(); AddToCart = new AddToCartDto(); ProductAttributes = new List <ProductAttributeDto>(); AssociatedProducts = new List <ProductDetailsDto>(); VendorDto = new VendorBriefInfoDto(); Breadcrumb = new ProductBreadcrumbDto(); ProductTags = new List <ProductTagDto>(); ProductSpecifications = new List <ProductSpecificationDto>(); ProductManufacturers = new List <ManufacturerDto>(); ProductReviewOverview = new ProductReviewOverviewDto(); TierPrices = new List <TierPriceDto>(); }
public async Task <IList <OrderItemDto> > AddToCartAsync(int orderId, AddToCartDto model) { var addedItems = model.Items .Select(i => new OrderItem { OrderId = orderId, ProductId = i.ProductId, Quantity = i.Quantity }) .ToList(); await _context.AddRangeAsync(addedItems); await _context.SaveChangesAsync(); return(addedItems.MapToOrderItemDtoCollection()); }
public ServiceResult AddToCart(AddToCartDto dto) { var serviceResult = new ServiceResult(true); var product = _dbContext.Products.FirstOrDefault(c => c.Id == dto.ProductId); if (product == null) { serviceResult.AddError("محصولی یافت نشد"); } else { if (product.Count <= 0) { serviceResult.AddError("کالا ناموجود است"); } else { if (!_dbContext.Users.Any(c => c.Id == dto.UserId)) { serviceResult.AddError("کاربری یافت نشد"); } else { var countCart = _dbContext.Carts.Count(c => c.UserId == dto.UserId && c.ProductId == dto.ProductId); if (countCart >= product.Count) { serviceResult.AddError("کالا ناموجود است"); } else { var entity = dto.ToEntity(); entity.Date = DateTime.Now; Insert(entity); serviceResult = Save("یک کالا با موفقیت به سبد خرید اضافه شد"); } } } } return(serviceResult); }
public async Task <BaseDtoResponse <AddToCartDto> > GetById(Guid id) { try { CartItem item = await _cartItemRepository.GetById(id); if (item == null) { return(new BaseDtoResponse <AddToCartDto>("Category Not Found")); } AddToCartDto result = _mapper.Map <CartItem, AddToCartDto>(item); return(new BaseDtoResponse <AddToCartDto>(result)); } catch (Exception ex) { return(new BaseDtoResponse <AddToCartDto>(ex.Message)); } }
public ActionResult AddToCart(AddToCartDto postModel) { try { using (var uow = _uowProvider.Create()) { var store = CurrentPage.GetStore(); var order = _sessionManager.GetOrCreateCurrentOrder(store.Id) .AsWritable(uow); // var product = GetProduct(postModel.ProductReference.As<Guid>()); // if (product != null) // { // if (product.IsBundle) // AddBundle(postModel, order, product); // else // order.AddProduct(postModel.ProductReference, 1); // } // else order.AddProduct(postModel.ProductReference, 1); _orderService.SaveOrder(order); uow.Complete(); } } catch (ValidationException ex) { ModelState.AddModelError("productReference", "Failed to add product to cart"); Logger.Error(typeof(CartSurfaceController), ex, "AddToCart Error"); return(CurrentUmbracoPage()); } var cartPage = CurrentPage.GetCartPage(); if (cartPage != null) { return(Redirect(cartPage.Url())); } TempData["addedProductReference"] = postModel.ProductReference; return(RedirectToCurrentUmbracoPage()); }
public bool Post([FromBody] AddToCartDto add) { if (add != null) { var r = productService.PostCartDetails(add); if (r == 1) { return(true); } else { return(false); } } else { return(false); } }
public async Task <BaseDtoResponse <AddToCartDto> > Delete(Guid id) { try { CartItem item = await _cartItemRepository.GetById(id); if (item != null) { await _cartItemRepository.Delete(item); AddToCartDto result = _mapper.Map <CartItem, AddToCartDto>(item); return(new BaseDtoResponse <AddToCartDto>(result)); } else { return(new BaseDtoResponse <AddToCartDto>("Cart Item Not found")); } } catch (Exception ex) { return(new BaseDtoResponse <AddToCartDto>($"An error occurred when deleting the cart item: {ex.Message}")); } }
public async Task <BaseDtoResponse <AddToCartDto> > Update(Guid id, UpdateCartRequest request) { try { CartItem existingItem = await _cartItemRepository.GetById(id); if (existingItem == null) { return(new BaseDtoResponse <AddToCartDto>("Unable to update the item in the cart, try later!")); } existingItem.Quantity = request.Quantity; await _cartItemRepository.Update(existingItem); CartItem updatedItem = await _cartItemRepository.GetById(id); AddToCartDto response = _mapper.Map <CartItem, AddToCartDto>(updatedItem); return(new BaseDtoResponse <AddToCartDto>(response)); } catch (Exception ex) { return(new BaseDtoResponse <AddToCartDto>(ex.Message)); } }
public async Task <ActionResult <OrderItemDto> > AddToCart([FromRoute] int id, [FromBody] AddToCartDto model) { try { var orderItems = await _orderService.AddToCartAsync(id, model); return(Created(string.Empty, orderItems)); } catch (Exception) { return(BadRequest()); } }