} upon successful update * - { "error" : } if an error occurs (see below) * * Invalid Request (400) Errors (in order of precedence): * - Missing required name or category POST parameter * - A price or cost is passed as a negative value * * Service Unavailable (503) Error when a DB connection occurs */ include "common.php"; if (isset($_POST["name"]) && isset($_POST["category"])) { $name = $_POST["name"]; $category = $_POST["category"]; $subcat = $category; $price = 0; $cost = 0; if (isset($_POST["subcategory"])) { $subcat = $_POST["subcategory"]; } if (isset($_POST["price"])) { $price = round(floatval($_POST["price"]), 2); check_negative($price); } if (isset($_POST["cost"])) { $cost = round(floatval($_POST["cost"]), 2); check_negative($cost); } $db = get_PDO(); if (find_item($db, $name)) { # find_item is used by other PHP programs, so we factor in common.php # error if trying to add an item already on the menu handle_request_error("We already have {$name} on the menu!"); } else { add_item($db, $name, $category, $subcat, $price, $cost); } } else { handle_request_error("Missing required name and/or category parameters"); } /** * Adds new item information to menu, re-formatting any string parameters in Title Case. * Outputs a JSON response with a success message upon successful update, otherwise a * descriptive error message (see web service documentation for possible errors). * * @param {PDO} $db - PDO object connected to cafedb * @param $name {string} - name for new item * @param $category {string} - category for new item * @param $subcat {string} - subcategory for new item * @param $price {Number} - price for new item * @param $cost {Number} - cost for new item */ function add_item($db, $name, $category, $subcat, $price, $cost) { $name = ucwords($name); $category = ucwords($category); $subcat = ucwords($subcat); $qry = "INSERT INTO menu (name, category, subcategory, price, cost) " . "VALUES(:name, :category, :subcat, :price, :cost);"; try { $stmt = $db->prepare($qry); $params = array("name" => $name, "category" => $category, "subcat" => $subcat, "price" => $price, "cost" => $cost); $stmt->execute($params); } catch (PDOException $ex) { handle_db_error(); } output_success("{$name} added to the menu!"); } ?>