Finalized API

This commit is contained in:
Maximilian Giller 2021-01-02 18:32:05 +01:00
parent 045eb7ecc9
commit f372401e19
4 changed files with 86 additions and 5 deletions

View file

@ -0,0 +1,30 @@
<?php
session_start();
require_once(__DIR__ . "/services/apiBranch.inc.php");
require_once(__DIR__ . "/services/jsonBuilder.inc.php");
require_once(__DIR__ . "/services/responses.inc.php");
require_once(__DIR__ . "/services/jugglDbApi.inc.php");
class AddProjectBranch extends ApiBranch
{
function get(ParamCleaner $params)
{
respondStatus(405);
}
function post(ParamCleaner $params)
{
$user_id = $params->get("user_id");
if ($params->exists(["name", "start_date"]) == false) {
respondStatus(400, "Missing parameter");
}
addProject($user_id, $params);
respondStatus(200);
}
}
$branch = new AddProjectBranch();
$branch->execute();

View file

@ -0,0 +1,29 @@
<?php
session_start();
require_once(__DIR__ . "/services/apiBranch.inc.php");
require_once(__DIR__ . "/services/responses.inc.php");
require_once(__DIR__ . "/services/jugglDbApi.inc.php");
class RemoveRecordBranch extends ApiBranch
{
function get(ParamCleaner $params)
{
respondStatus(405);
}
function post(ParamCleaner $params)
{
$user_id = $params->get("user_id");
if ($params->exists(["record_id"]) == false) {
respondStatus(400, "Missing parameter");
}
removeRecord($user_id, $params);
respondStatus(200);
}
}
$branch = new RemoveRecordBranch();
$branch->execute();

View file

@ -15,6 +15,19 @@ function addStartRecord($user_id, $params, $project_id = null, $start_device_id
$db->execute(); $db->execute();
} }
function addProject($user_id, $params)
{
$data = [
"user_id" => $user_id,
"name" => $params->get("name"),
"start_date" => $params->get("start_date")
];
$db = new DbOperations();
$db->insert("projects", $data);
$db->execute();
}
function addTimeRecord($user_id, $params, $project_id = null, $start_device_id = null) function addTimeRecord($user_id, $params, $project_id = null, $start_device_id = null)
{ {
$data = [ $data = [
@ -168,8 +181,6 @@ function updateEndRecord($user_id, $params)
// Get start instance to calculate duration // Get start instance to calculate duration
$start_time = getTimeRecord($user_id, $record_id)["start_time"]; $start_time = getTimeRecord($user_id, $record_id)["start_time"];
var_dump($start_time);
var_dump($params->get("end_time"));
$data = [ $data = [
"end_time" => $params->get("end_time"), "end_time" => $params->get("end_time"),
@ -183,6 +194,17 @@ function updateEndRecord($user_id, $params)
$db->execute(); $db->execute();
} }
function removeRecord($user_id, $params)
{
$record_id = $params->get("record_id");
$db = new DbOperations();
$db->delete("time_records");
$db->where("user_id", Comparison::EQUAL, $user_id);
$db->where("record_id", Comparison::EQUAL, $record_id);
$db->execute();
}
function updateTimeRecord($user_id, $params) function updateTimeRecord($user_id, $params)
{ {
$data = []; $data = [];

View file

@ -10,20 +10,20 @@ function setDefaultHeader()
} }
function respondJson(JsonBuilder $builder) { function respondJson(JsonBuilder $builder) {
defaultHeader(); setDefaultHeader();
header('Content-type: application/json'); header('Content-type: application/json');
echo($builder->getJson()); echo($builder->getJson());
} }
function respondHtml(string $html) function respondHtml(string $html)
{ {
defaultHeader(); setDefaultHeader();
print($html); print($html);
} }
function respondStatus(int $statusCode, string $message = "") function respondStatus(int $statusCode, string $message = "")
{ {
defaultHeader(); setDefaultHeader();
http_response_code($statusCode); http_response_code($statusCode);
die($message); die($message);
} }