Exemplo n.º 1
0
        public ActionResult <Product> ModifyQuantity(string id, string op, string amount)
        {
            ActionResult <Product> response;

            op = op ?? "".Trim().ToLower();
            string[] validOps = { "add", "subtract" };
            if (!validOps.Contains(op))
            {
                response = UnprocessableEntity(new { error = "Invalid PATCH operation specified, choices are 'add' and 'subtract'." });
            }
            else
            {
                Product modified;
                try
                {
                    switch (op)
                    {
                    case "add":
                        modified = new AdminBLLController().AddQuantityToProductByID(id, amount);
                        response = Ok(modified);
                        break;

                    case "subtract":
                        modified = new AdminBLLController().SubtractQuantityFromProductByID(id, amount);
                        response = Ok(modified);
                        break;

                    default:
                        response = StatusCode(500);
                        break;
                    }
                }
                catch (InvalidOperationException)
                {
                    response = StatusCode(403, new { error = $"No product was found with the ID of {id}." });
                }
                catch (Exception e)
                {
                    response = StatusCode(403, new { error = e.Message });;
                }
            }


            // Return the response.
            return(response);
        }
Exemplo n.º 2
0
        public ActionResult <Product> SubtractQuantityProduct(string id, string amount)
        {
            ActionResult <Product> response;
            Product modified;

            try
            {
                // We aren't concerned with validation here. Only in BLL.
                modified = new AdminBLLController().SubtractQuantityFromProductByID(id, amount);
                // Encode our created object as JSON and bounce it back with the request.
                response = Ok(modified);
            }
            catch (InvalidOperationException)
            {
                response = StatusCode(403, new { error = $"No product was found with the ID of {id}." });
            }
            catch (Exception e)
            {
                response = StatusCode(403, new { error = e.Message });;
            }

            // Return the response.
            return(response);
        }
Exemplo n.º 3
0
        public ActionResult <Product> AddProduct(string name, string quantity, string isDiscontinued)
        {
            ActionResult <Product> response;
            Product created;

            try
            {
                // We aren't concerned with validation here. Only in BLL.
                created = new AdminBLLController().CreateProduct(name, quantity, isDiscontinued);
                // Encode our created object as JSON and bounce it back with the request.
                response = Ok(created);
            }
            catch (InvalidOperationException)
            {
                response = StatusCode(403, new { error = $"Please provide a product name to add." });
            }
            catch (Exception e)
            {
                response = StatusCode(403, new { error = e.Message });;
            }

            // Return the response.
            return(response);
        }