Created
July 16, 2012 20:58
-
-
Save sebastianhoitz/3125012 to your computer and use it in GitHub Desktop.
Mega REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function buildSetString($arguments) | |
{ | |
if (count($arguments) == 0) | |
{ | |
return ""; | |
} | |
$query = 'SET '; | |
$parts = array(); | |
foreach ($arguments as $key => $value) | |
{ | |
$parts[] = " $key = :$key"; | |
} | |
return "SET " . implode(",", $parts); | |
} | |
function buildWhereString($arguments) | |
{ | |
if(count($arguments) == 0) | |
{ | |
return ""; | |
} | |
$namedConditions = array(); | |
foreach($arguments as $key => $value) | |
{ | |
$namedConditions[] = "$key = :$key"; | |
$namedParamsValues[":$key"] = $value; | |
} | |
$where = "WHERE " . implode(" && ", $namedConditions); | |
return $where; | |
} | |
function fetchOneQuery($table, $where = array()) | |
{ | |
return executeQuery($table, array(), "SELECT * FROM", $where, "one"); | |
} | |
function fetchAllQuery($table, $where = array()) | |
{ | |
return executeQuery($table, array(), "SELECT * FROM", $where, "all"); | |
} | |
function executeInsertQuery($table, $arguments) | |
{ | |
return executeQuery($table, $arguments, "INSERT INTO"); | |
} | |
function executeUpdateQuery($table, $arguments, $where) | |
{ | |
return executeQuery($table, $arguments, "UPDATE", $where); | |
} | |
function executeQuery($table, $arguments, $method, $where = array(), $type = "execute") | |
{ | |
$namedParams = array(); | |
$namedParamsValues = array(); | |
foreach($arguments as $key => $value) | |
{ | |
$namedParamsValues[":$key"] = $value; | |
} | |
foreach($where as $key => $value) | |
{ | |
$namedParamsValues[":$key"] = $value; | |
} | |
$where = buildWhereString($where); | |
$set = buildSetString($arguments); | |
$sql = "$method $table $set $where"; | |
// echo $sql; | |
// var_dump($namedParamsValues); | |
return getDatabase()->{$type}($sql, $namedParamsValues); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Card | |
{ | |
public static function register() | |
{ | |
getApi()->get("/cards", array("Card", "index"), EpiApi::external); | |
getApi()->get("/cards/(\d+)", array("Card", "get"), EpiApi::external); | |
getApi()->get("/cards/(\d+)/votes", array("Card", "votes"), EpiApi::external); | |
getApi()->post("/cards/(\d+)/votes", array("Card", "addVote"), EpiApi::external); | |
getApi()->put("/cards/(\d+)", array("Card", "put"), EpiApi::external); | |
getApi()->post("/cards", array("Card", "post"), EpiApi::external); | |
} | |
public static function index($where = array()) | |
{ | |
return fetchAllQuery("card", $where); | |
} | |
public static function get($id, $where = array()) | |
{ | |
$where['id'] = $id; | |
return fetchOneQuery("card", $where); | |
} | |
public static function put($id) | |
{ | |
parse_str(file_get_contents('php://input'), $values); | |
$card = array( | |
"creatorID" => $values['creatorID'], | |
"question" => $values['question'], | |
"response" => $values['response'], | |
"isBad" => 0 | |
); | |
$result = executeUpdateQuery("card", $card, array("id" => $id)); | |
getRoute()->redirect("/card/".$id); | |
} | |
public static function post() | |
{ | |
$card = array( | |
"id" => null, | |
"creatorID" => $_POST['creatorID'], | |
"question" => $_POST['question'], | |
"response" => $_POST['response'], | |
"isBad" => 0 | |
); | |
$result = executeInsertQuery("card", $card); | |
getRoute()->redirect("/card/".$result); | |
} | |
public static function votes($id) | |
{ | |
return Vote::index(array("cardID" => $id)); | |
} | |
public static function addVote($id) | |
{ | |
$vote = array( | |
"vote" => $_POST['vote'], | |
"cardID" => $id, | |
"userID" => $_POST['userID'] | |
); | |
$result = executeInsertQuery("card_vote", $vote); | |
var_dump($id); | |
getRoute()->redirect("/cards/$id/votes"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment