private void AddOrRemoveCartItems(ShoppingCart shoppingCart, SavedShoppingCartDto shoppingCartDto)
        {
            try
            {
                var addedCartItems =
                    shoppingCartDto.Carts
                    .Where(cd =>
                           shoppingCart.Carts.All(oi => oi.ProductId != cd.ProductId))
                    .ToList();
                foreach (var cartItemDto in addedCartItems)
                {
                    shoppingCart.AddItem(cartItemDto.ProductId, cartItemDto.Quantity);
                }

                var removedCartItems =
                    shoppingCart.Carts.Where(oi => shoppingCartDto.Carts.Any(oir => oir.ProductId != oi.ProductId))
                    .ToList();
                foreach (var item in removedCartItems)
                {
                    shoppingCart.RemoveItem(item);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        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 <IServiceResult> UpdateAsync(string id, SavedShoppingCartDto shoppingCartDto)
        {
            try
            {
                var shoppingCartId = id.ToGuid();
                var shoppingCart   = await _repository.GetSingleAsync(shoppingCartId);

                _mapper.Map(shoppingCartDto, shoppingCart);

                AddOrRemoveCartItems(shoppingCart, shoppingCartDto);

                if (!await _unitOfWork.CompleteAsync())
                {
                    throw new SaveFailedException(nameof(shoppingCart));
                }
                _logger.LogInformation($"Updated {nameof(shoppingCart)} with id: {shoppingCart.Id}");

                var result = await GetSingleAsync(shoppingCart.Id.ToString());

                return(new ServiceResult(payload: result.Payload));
            }
            catch (Exception e)
            {
                _logger.LogError($"Updating shopping cart with id: {id} failed. {e.Message}");

                return(new ServiceResult(false, e.Message));
            }
        }