Webservices

This commit is contained in:
2026-01-31 18:01:24 +01:00
parent e1c752fcf8
commit 2d2fc24d71
35 changed files with 3531 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include_once '../../config/database.php';
include_once '../objects/presence.php';
include_once '../authenticator.php';
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
http_response_code(400);
echo json_encode(array("error" => "Method not accepted."));
exit();
}
$database = new Database();
$db = $database->getConnection();
$auth = new Authenticator($db);
$userId = $auth->authenticate();
// get posted data
$data = json_decode(file_get_contents("php://input"));
$p = new Presence($db);
$p->userId = $userId;
$p->willBePresent = $data->willBePresent;
$p->notes = $data->notes;
if($p->create()){
// set response code - 201 created
http_response_code(201);
echo json_encode($p);
} else {
// unable to create
http_response_code(500);
echo json_encode(array("error" => "Unable to create Presence."));
}
?>

View File

@@ -0,0 +1,51 @@
<?php
/**
* Returns the current user response to the presence question.
* Works as a list because the id should not be needed for the query.
*/
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
// include database and object files
include_once '../../config/database.php';
include_once '../objects/presence.php';
include_once '../objects/user.php';
include_once '../authenticator.php';
if ($_SERVER['REQUEST_METHOD'] != 'GET') {
http_response_code(400);
echo json_encode(array("error" => "Method not accepted."));
exit();
}
// instantiate database and product object
$database = new Database();
$db = $database->getConnection();
$auth = new Authenticator($db);
$uid = $auth->authenticate();
// query products
$stmt = Presence::read($db, $uid);
$resp=array();
$resp["records"]=array();
$resp["page"]=1;
$resp["more"]=FALSE;
$presence = $stmt->fetch(PDO::FETCH_ASSOC);
if ($presence) {
$resp["records"][] = [
'willBePresent' => $presence['will_be_present'] ? true : false,
'notes' => $presence['notes']
];
}
// set response code - 200 OK
http_response_code(200);
// show products data in json format
echo json_encode($resp);