function addComment($data) { $results = []; //checks if requests needed are present and not empty $dataNeeded = array("comment", "goalID", "me"); if (checkData($data, $dataNeeded)) { $goal = getGoal($data["goalID"]); if ($goal["count"] > 0) { //check if user provided exists $user = getAUser($data["me"]); if ($user["count"] > 0) { $db = new pdodb(); $query = "INSERT INTO Comment (Comment, GoalID, Username, Upload) VALUES (:comment, :goalID, :me, :upload);"; $bindings = array(":comment" => $data["comment"], ":goalID" => $data["goalID"], ":me" => $data["me"], ":upload" => date("y/m/d")); $comment = $db->query($query, $bindings); //if add was ok if ($comment["count"] > 0) { $commentID = $db->lastInsertId(); $results = getComment($commentID); $results["meta"]["ok"] = true; $results["meta"]["status"] = 201; $results["meta"]["message"] = "Created"; } else { //check if database provided any meta data if so problem with executing query if (isset($comment["meta"])) { $results = $comment; } else { $results["meta"]["ok"] = false; } } } else { //check if database provided any meta data if so problem with executing query if (isset($user["meta"])) { $results = $user; } else { $results["meta"] = noUserFound($data["me"]); } } } else { //check if database provided any meta data if so problem with executing query if (isset($goal["meta"])) { $results = $goal; } else { $results["meta"] = noGoalFound($data["goalID"]); } } } else { $results["meta"] = dataNotProvided($dataNeeded); } return $results; }
function addProject($data) { //checks if requests needed are present and not empty $dataNeeded = array("username", "password", "projectName", "skills", "description", "github", "date"); if (checkData($data, $dataNeeded) && preg_match("/\\b[\\d]{4}-[\\d]{2}-[\\d]{2}\\b/im", $data["date"])) { //checks if user provided exists $results = login($data); if ($results["meta"]["ok"] === true) { $db = new pdodb(); $query = "INSERT INTO PortfolioProject (Name, Skills, Description, Link, GitHub, Download, Date) VALUES (:projectName, :skills, :description, :link, :github, :download, :date);"; $bindings = array(":projectName" => $data["projectName"], ":skills" => $data["skills"], ":description" => $data["description"], ":link" => $data["link"], ":github" => $data["github"], ":download" => $data["download"], ":date" => $data["date"]); $results = $db->query($query, $bindings); //if add was ok if ($results["count"] > 0) { $projectID = $db->lastInsertId(); $results = getProject($projectID); $results["meta"]["ok"] = true; $results["meta"]["status"] = 201; $results["meta"]["message"] = "Created"; } else { //check if database provided any meta data if so problem with executing query if (!isset($results["meta"])) { $results["meta"]["ok"] = false; } } } } else { $results["meta"] = dataNotProvided($dataNeeded); } return $results; }