Skip to content

Instantly share code, notes, and snippets.

@piiskop
Last active March 14, 2018 08:56
Show Gist options
  • Save piiskop/df579dd5e3a08e0521b84363c0ef10d3 to your computer and use it in GitHub Desktop.
Save piiskop/df579dd5e3a08e0521b84363c0ef10d3 to your computer and use it in GitHub Desktop.
<?php
namespace pupils;
class StudyGroupController {
public function __construct($arguments) {
require_once dirname(__FILE__) . '/StudyGroup.php';
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET' :
{
$data = StudyGroup::findAll();
if (isset($_GET['format'])) {
switch ($_GET['format']) {
case 'json' :
{
if (sizeof($data) > 0) {
new \o\Response(array (
'data' => $data,
'message' => 'number of study groups found: ' . $_SESSION['dbEngine']->getNumberOfRecords(),
'status' => 200
));
} else {
new \o\Response(array (
'message' => 'no study groups found',
'status' => 204
));
}
}
default :
{
new \o\Response(array (
// @formatter:off
'message' => sprintf(
'Vormindus %1$s on mulle tundmatu.',
$_GET['format'] // 1
),
// @formatter:on
'status' => 415
));
}
}
} else {
new \o\Response(array (
'message' => 'This application doesn\'t talk to humans yet. Please use format=json in your querystring.',
'status' => 501
));
}
}
case 'POST' :
{
if (isset($_GET['format'])) {
switch ($_GET['format']) {
case 'json' :
{
$data = json_decode(
file_get_contents("php://input", true));
$studyGroup = new StudyGroup(array (
'name' => $data->name
));
$studyGroup->insert();
new \o\Response(array (
'message' => 'The study group was inserted.',
'status' => 201
));
}
default :
{
new \o\Response(array (
// @formatter:off
'message' => sprintf(
'Vormindus %1$s on mulle tundmatu.',
$_GET['format'] // 1
),
// @formatter:on
'status' => 415
));
}
}
} else {
new \o\Response(array (
'message' => 'This application doesn\'t talk to humans yet. Please use format=json in your querystring.',
'status' => 501
));
}
}
default :
{
new \o\Response(array (
'message' => 'unknown request method: ',
$_SERVER['REQUEST_METHOD'],
'status' => 405
));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment