Esempio n. 1
0
        public void AddCartItem(string email, ShoppingCartItemInputModel shoppingCartItem, float priceInUsd)
        {
            // Find user and cart
            var user = _dbContext.Users.FirstOrDefault(u => u.Email == email);
            var cart = _dbContext.ShoppingCarts.FirstOrDefault(s => s.UserId == user.Id);

            // If shopping cart doesn't exist create new one
            if (cart == null)
            {
                var newCart = new ShoppingCart
                {
                    UserId = user.Id,
                };
                _dbContext.ShoppingCarts.Add(newCart);
                _dbContext.SaveChanges();
            }

            cart = _dbContext.ShoppingCarts.FirstOrDefault(s => s.UserId == user.Id);

            var newCartItem = new ShoppingCartItem
            {
                ProductIdentifier = shoppingCartItem.ProductIdentifier,
                Quantity          = (float)shoppingCartItem.Quantity,
                UnitPrice         = priceInUsd,
                ShoppingCartId    = cart.Id
            };

            _dbContext.ShoppingCartItems.Add(newCartItem);
            _dbContext.SaveChanges();
        }
        public IActionResult Put([FromBody] ShoppingCartItemInputModel inputModel)
        {
            var item = _mapper.Map <ShoppingCartItem>(inputModel);

            _service.UpdateItemQuantity(User.Identity.Name, item);
            return(Ok());
        }
        public IActionResult AddCartItem([FromBody] ShoppingCartItemInputModel item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Cart item is not properly constructed!"));
            }

            _shoppingCartService.AddCartItem(User.Identity.Name, item);
            return(StatusCode(201, item));
        }
        public async Task <IActionResult> AddItemToCart([FromBody] ShoppingCartItemInputModel shoppingCartItem)
        // Adds an item to the shopping cart
        {
            if (!ModelState.IsValid)
            {
                return(StatusCode(412, shoppingCartItem));
            }
            await _shoppingCartService.AddCartItem(User.Identity.Name, shoppingCartItem);

            return(StatusCode(201, "Item has been added to the shopping cart."));
        }
        public IActionResult AddItemToCart([FromBody] ShoppingCartItemInputModel cartItem)
        {
            if (!ModelState.IsValid)
            {
                ErrorHandler.GetModelErrors(ModelState);
            }

            var email       = ClaimsHelper.GetClaim(User, "name");
            var newCartItem = _shoppingCartService.AddCartItem(email, cartItem);

            return(CreatedAtRoute("AddItemToCart", new { id = newCartItem.Id }, null));
        }
        public IActionResult AddCartItem([FromBody] ShoppingCartItemInputModel inputModel)
        {
            // Check if inputModel is correct
            if (!ModelState.IsValid)
            {
                throw new ModelFormatException("Cart item not properly formatted.");
            }

            // Get user email from claims
            var email = User.Claims.FirstOrDefault(c => c.Type == "name").Value;

            _shoppingCartService.AddCartItem(email, inputModel);
            return(StatusCode(201));
        }
Esempio n. 7
0
        public ShoppingCartItemDto AddCartItem(string email, ShoppingCartItemInputModel shoppingCartItemItem)
        {
            var productIdentifier = shoppingCartItemItem.ProductIdentifier;

            if (!CryptocurrencyHelper.AllowedCurrencies.Contains(productIdentifier.ToLower()))
            {
                throw new InvalidProductIdentifierException();
            }

            var response = Client.GetAsync($"https://data.messari.io/api/v1/assets/" +
                                           $"{productIdentifier}/metrics?fields=market_data/price_usd").Result;

            var data = response.DeserializeJsonToObject <CryptoCurrencyResponse>(true).Result;

            return(_shoppingCartRepository.AddCartItem(email, shoppingCartItemItem, data.PriceInUsd));
        }
        public void AddCartItem(string email, ShoppingCartItemInputModel shoppingCartItem, float priceInUsd)
        {
            // Add a cart item to the database
            var user = _dbContext.Users.FirstOrDefault(u => u.Email == email);

            var cartItem = new ShoppingCartItemEntity
            {
                ShoppingCartId    = user.ShoppingCart.Id,
                ProductIdentifier = shoppingCartItem.ProductIdentifier,
                Quantity          = shoppingCartItem.Quantity,
                UnitPrice         = priceInUsd
            };

            _dbContext.ShoppingCartItems.Add(cartItem);
            _dbContext.SaveChanges();
        }
Esempio n. 9
0
        public ShoppingCartItemDto AddCartItem(string email, ShoppingCartItemInputModel shoppingCartItemItem, float priceInUsd)
        {
            var user = _dbContext.Users.FirstOrDefault(u => u.Email == email);

            if (user == null)
            {
                throw new ResourceNotFoundException($"User with email {email} not found");
            }

            var cart = _dbContext.ShoppingCarts.FirstOrDefault(s => s.User.Email == email) ?? CreateCart(user.Id);

            var cartItem = _mapper.Map <ShoppingCartItem>(shoppingCartItemItem);

            cartItem.ShoppingCartId = cart.Id;
            cartItem.UnitPrice      = priceInUsd;
            cartItem.TotalPrice     = priceInUsd * cartItem.Quantity;
            _dbContext.Add(cartItem);
            _dbContext.SaveChanges();
            return(_mapper.Map <ShoppingCartItemDto>(cartItem));
        }
Esempio n. 10
0
        public async Task AddCartItem(string email, ShoppingCartItemInputModel shoppingCartItem)
        {
            var identifier = shoppingCartItem.ProductIdentifier;

            if (identifier.ToUpper() != "BTC" && identifier.ToUpper() != "ETH" && identifier.ToUpper() != "USDT" && identifier.ToUpper() != "XMR")
            {
                throw new ResourceNotFoundException("Invalid product identifier.");
            }

            // Call the external API using the product identifier as an URL parameter to
            string URL           = "https://data.messari.io/api/v1/assets/" + shoppingCartItem.ProductIdentifier + "/metrics";
            string urlParameters = "?fields=id,symbol,name,slug,market_data/price_usd";

            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(URL);

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json")
                );

            // List data response.
            var response = await client.GetAsync(urlParameters);

            if (response.IsSuccessStatusCode)
            {
                // Receive the current price in USD for this particular cryptocurrency
                // Deserialize the response to a CryptoCurrencyDto model
                var responseObject = await HttpResponseMessageExtensions.DeserializeJsonToObject <CryptoCurrencyDto>(response, true);

                client.Dispose();
                _shoppingCartRepository.AddCartItem(email, shoppingCartItem, responseObject.PriceInUsd);
            }
            else
            {
                throw new Exception("Response failed to external API.");
            }
        }
Esempio n. 11
0
        public void AddCartItem(string email, ShoppingCartItemInputModel shoppingCartItemItem, float priceInUsd)
        {
            // Get user for user id
            var user = _dbContext.Users.FirstOrDefault(u => u.Email == email);

            if (user == null)
            {
                throw new ResourceMissingException("No user was found with this email.");
            }

            // Get user shoppingCart or create new if it doesnt exit
            var cart = _dbContext.ShoppingCarts.FirstOrDefault(c => c.UserId == user.Id);

            if (cart == null)
            {
                cart = CreateShoppingCart(user.Id);
            }

            // Deal with nullable variable
            float quantity = 0.01F;

            if (shoppingCartItemItem.Quantity != null)
            {
                quantity = (float)shoppingCartItemItem.Quantity;
            }

            // Create new ShoppingCartItem
            var entity = new ShoppingCartItem
            {
                ShoppingCartId    = cart.Id,
                ProductIdentifier = shoppingCartItemItem.ProductIdentifier,
                Quantity          = quantity,
                UnitPrice         = priceInUsd
            };

            _dbContext.ShoppingCartItems.Add(entity);
            _dbContext.SaveChanges();
        }
Esempio n. 12
0
        public Task AddCartItem(string email, ShoppingCartItemInputModel shoppingCartItem)
        {
            // *** TODO ***

            /* var price = 0;
             * // Call the external API using the product identifier as an URL parameter to receive the
             * //        current price in USD for this particular cryptocurrency
             * client.DefaultRequestHeaders.Accept.Clear();
             * client.DefaultRequestHeaders.Add("x-messari-api-key", "13d40275-6ada-445f-89c1-8809470621e8");
             * var shop = shoppingCartItem.ProductIdentifier;
             * var js = client.GetStringAsync("https://data.messari.io/api/v1/assets/"+ shop +"/metrics/market-data");
             *
             * //Deserialize the response to a CryptoCurrencyDto model
             * var des = new CryptoCurrencyDto {
             *   Id = shoppingCartItem.ProductIdentifier,
             *
             * } = HttpResponseMessageExtensions.DeserializeJsonToObject<CryptoCurrencyDto>(js);
             *   price = des.PriceInUsd;
             * //Add it to the database using the appropriate repository class
             * return _shoppingCartRepository.AddCartItem(email, shoppingCartItem, price);
             */
            throw new System.NotImplementedException();
        }
Esempio n. 13
0
        public void AddCartItem(string email, ShoppingCartItemInputModel shoppingCartItemItem)
        {
            float priceInUsd = _cryptoCurrencyService.GetCryptocurrencyPrice(shoppingCartItemItem.ProductIdentifier).Result;

            _shoppingCartRepository.AddCartItem(email, shoppingCartItemItem, priceInUsd);
        }
 public IActionResult UpdateQuantityOfItem([FromBody] ShoppingCartItemInputModel shoppingCartItem, int cartItemId)
 // Updates the quantity for an item in the cart
 {
     _shoppingCartService.UpdateCartItemQuantity(User.Identity.Name, cartItemId, (float)shoppingCartItem.Quantity);
     return(NoContent());
 }