Exemple #1
0
        [FunctionName("Remove_Item")] //Delete
        public async Task <IActionResult> RemoveItem([HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "cart/delete")] HttpRequest req)
        {
            try
            {
                if (req.Query is null)
                {
                    throw new NullReferenceException();
                }
                else if (await IsTokenValid(req))
                {
                    int.TryParse(req.Query["id"], out int id);
                    using Cart currentCart = await ICartRepo.GetOne(id);

                    using EcoItem ecoItem = await IEcoItemRepo.GetOne(currentCart.EcoItemId);

                    ecoItem.Quantity += currentCart.Quantity;

                    ICartRepo.Remove(currentCart);
                    IEcoItemRepo.Modify(ecoItem);
                    await Context.SaveChangesAsync();

                    return(new OkObjectResult(new Response(true, "Item successfully removed from the cart.", null)));
                }
                else
                {
                    return(new UnauthorizedResult());
                }
            }
            catch (Exception)
            { throw new Exception("Oops! Something went wrong!"); }
        }
Exemple #2
0
        public async Task <IActionResult> DeleteEcoItem([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "ecoitem/delete")] HttpRequest req)
        {
            try
            {
                if (req.Body is null)
                {
                    throw new NullReferenceException();
                }
                else if (await IsTokenValid(req))
                {
                    int.TryParse(req.Query["id"], out int id);
                    using EcoItem item = await IEcoItemRepo.GetOne(id);

                    IEcoItemRepo.Remove(item);
                    await Context.SaveChangesAsync();

                    return(new OkObjectResult(new Response(true, "Item has been deleted.", null)));
                }
                else
                {
                    return(new UnauthorizedResult());
                }
            }
            catch (Exception)
            { throw new Exception("Oops! Something went wrong!"); }
        }
Exemple #3
0
        public async Task <IActionResult> UpdateEcoItem([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "ecoitem/update")] HttpRequest req)
        {
            try
            {
                if (req.Body is null)
                {
                    throw new NullReferenceException();
                }
                else if (await IsTokenValid(req))
                {
                    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                    using EcoItem ecoitem = JsonConvert.DeserializeObject <EcoItem>(requestBody);

                    if (ValidationEcoItem(ecoitem))
                    {
                        IEcoItemRepo.Modify(ecoitem);
                        await Context.SaveChangesAsync();

                        return(new OkObjectResult(new Response(true, "Item has been added.", ecoitem)));
                    }
                    return(new OkObjectResult(new Response(false, "Item failed to updated. Please check quantity.", null)));
                }
                else
                {
                    return(new UnauthorizedResult());
                }
            }
            catch (Exception)
            { throw new Exception("Oops! Something went wrong!"); }
        }
Exemple #4
0
        [FunctionName("Update_Cart")] //Update
        public async Task <IActionResult> UpdateCart([HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "cart/put")] HttpRequest req)
        {
            try
            {
                if (req.Query is null)
                {
                    throw new NullReferenceException();
                }
                else if (await IsTokenValid(req))
                {
                    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                    using Cart cart = JsonConvert.DeserializeObject <Cart>(requestBody);

                    //check if item has enough stock
                    if (await IsInStock(cart.Id, cart.Quantity))
                    {
                        using EcoItem ecoItem = await IEcoItemRepo.GetOne(cart.EcoItemId);

                        using Cart currentCart = await ICartRepo.GetOne(cart.Id);

                        ecoItem.Quantity -= currentCart.Quantity - cart.Quantity;

                        ICartRepo.Modify(cart);
                        IEcoItemRepo.Modify(ecoItem);
                        await Context.SaveChangesAsync();

                        return(new OkObjectResult(new Response(true, "Cart has been updated successfully.", cart)));
                    }
                    else
                    {
                        return(new OkObjectResult(new Response(false, "Cannot add to cart, item is out-of-stock.", null)));
                    }
                }
                else
                {
                    return(new UnauthorizedResult());
                }
            }
            catch (Exception)
            { throw new Exception("Oops! Something went wrong!"); }
        }
Exemple #5
0
        private async Task <bool> IsInStock(int id, int cartQty)
        {
            using EcoItem ecoItem = await IEcoItemRepo.GetOne(id);

            return(ecoItem.Quantity > cartQty);
        }
Exemple #6
0
 private bool ValidationEcoItem(EcoItem ecoitem) => !(ecoitem is null || ecoitem?.Quantity == 0);