public async Task <IServiceResult> CreateAsync(SavedShoppingCartDto shoppingCartDto) { try { var shoppingCart = _mapper.Map <SavedShoppingCartDto, ShoppingCart>(shoppingCartDto); await _repository.AddAsync(shoppingCart); if (shoppingCart.Carts.Count > 0) { foreach (var cartItem in shoppingCart.Carts) { shoppingCart.AddItem(cartItem.ProductId, cartItem.Quantity); _logger.LogError($"Added product with id {cartItem.ProductId} to shopping cart with id {shoppingCart.Id}."); } } if (!await _unitOfWork.CompleteAsync()) { throw new SaveFailedException(nameof(shoppingCart)); } _logger.LogInformation($"Added new {nameof(shoppingCart)} with id: {shoppingCart.Id}."); var result = await GetSingleAsync(shoppingCart.Id.ToString()); return(new ServiceResult(payload: result.Payload)); } catch (Exception e) { _logger.LogError($"Adding new shopping cart failed. {e.Message}"); return(new ServiceResult(false, e.Message)); } }
public async Task <bool> AddItemToCart(Movie movie, string cartId) { var success = false; var existingCartItem = await _shoppingCartRepository.GetOneAsync(movie, cartId); if (existingCartItem == null) { var newCartItem = new ShoppingCartItem { ShoppingCartId = cartId, MovieId = movie.Id, Quantity = 1, OrderDate = DateTime.Now }; await _shoppingCartRepository.AddAsync(newCartItem); success = true; } else { existingCartItem.Quantity++; await _shoppingCartRepository.UpdateAsync(existingCartItem); success = true; } return(success); }
/// <summary> /// add to shopping cart /// </summary> /// <param name="shoppingCartModel"></param> /// <returns></returns> public async Task Add(ShoppingCartModel shoppingCartModel) { ShoppingCart model = new ShoppingCart(); ObjectMapper.Mapper.Map <ShoppingCartModel, ShoppingCart>(shoppingCartModel, model); var resp = await _shoppingCartRepository.AddAsync(model); }