Add API
This commit is contained in:
parent
0016096c2e
commit
a00d6c78d2
151
api/index.php
Normal file
151
api/index.php
Normal file
@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
ob_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
require_once __DIR__ . "/../required.php";
|
||||
require_once __DIR__ . "/../machine.php";
|
||||
|
||||
$VARS;
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$VARS = $_POST;
|
||||
} else {
|
||||
$VARS = $_GET;
|
||||
}
|
||||
|
||||
if (!$database->has('apikeys', ['key' => $VARS['key']])) {
|
||||
http_response_code(403);
|
||||
die('{"status": "ERROR", "message": "Invalid API key. Access denied."}');
|
||||
}
|
||||
|
||||
function sendError($type, $msg = "An error occurred.") {
|
||||
$code = 404;
|
||||
switch ($type) {
|
||||
case "nomachineid":
|
||||
$code = 400;
|
||||
$msg = "No machine ID sent.";
|
||||
break;
|
||||
case "dberror":
|
||||
$code = 500;
|
||||
$msg = "The database encountered an error: $msg";
|
||||
}
|
||||
http_response_code($code);
|
||||
die(json_encode([
|
||||
"status" => "ERROR",
|
||||
"message" => $msg
|
||||
]));
|
||||
}
|
||||
|
||||
switch ($VARS['action']) {
|
||||
/* Get info */
|
||||
case "getmachineinfo":
|
||||
if (empty($VARS['id'])) {
|
||||
sendError("nomachineid");
|
||||
}
|
||||
|
||||
try {
|
||||
$machine = new Machine($VARS['id']);
|
||||
echo json_encode($machine->getMachineInfo());
|
||||
} catch (Exception $e) {
|
||||
sendError("", $e->getMessage());
|
||||
}
|
||||
break;
|
||||
case "getmachinehistory":
|
||||
if (empty($VARS['id'])) {
|
||||
sendError("nomachineid");
|
||||
}
|
||||
|
||||
try {
|
||||
$machine = new Machine($VARS['id']);
|
||||
echo json_encode($machine->getHistory());
|
||||
} catch (Exception $e) {
|
||||
sendError("", $e->getMessage());
|
||||
}
|
||||
break;
|
||||
case "getmachinecomponents":
|
||||
if (empty($VARS['id'])) {
|
||||
sendError("nomachineid");
|
||||
}
|
||||
|
||||
try {
|
||||
$machine = new Machine($VARS['id']);
|
||||
echo json_encode($machine->getComponents());
|
||||
} catch (Exception $e) {
|
||||
sendError("", $e->getMessage());
|
||||
}
|
||||
break;
|
||||
case "geteventtypes":
|
||||
echo json_encode($database->select('event_types', ['eventid (id)', 'eventname (name)']));
|
||||
break;
|
||||
case "getcomponenttypes":
|
||||
echo json_encode($database->select('component_types', ['typeid (id)', 'typename (name)']));
|
||||
break;
|
||||
|
||||
|
||||
/* Save info */
|
||||
case "addmachine":
|
||||
if (empty($VARS['id'])) {
|
||||
sendError("nomachineid");
|
||||
}
|
||||
if ($database->has('machines', ['machineid' => $VARS['id']])) {
|
||||
sendError("", "A machine with that ID already exists.");
|
||||
}
|
||||
$data = [];
|
||||
$data['machineid'] = $VARS['id'];
|
||||
if (empty($VARS['notes'])) {
|
||||
$data['notes'] = "";
|
||||
} else {
|
||||
$data['notes'] = $VARS['notes'];
|
||||
}
|
||||
if (!empty($VARS['model'])) {
|
||||
$data['model'] = $VARS['model'];
|
||||
}
|
||||
if (!empty($VARS['condition'])) {
|
||||
if (is_numeric($VARS['condition']) && $VARS['condition'] > 0 && $VARS['condition'] < 10) {
|
||||
$data['condition'] = $VARS['condition'] * 1.0;
|
||||
} else {
|
||||
sendError("", "Machine condition must be a number and 0 < condition < 10.");
|
||||
}
|
||||
}
|
||||
if (!empty($VARS['price'])) {
|
||||
if (is_numeric($VARS['price']) && $VARS['price'] > 0 && $VARS['price'] < 10000.0) {
|
||||
$data['price'] = $VARS['price'] * 1.0;
|
||||
} else {
|
||||
sendError("", "Machine price must be a number and 0 < price < 10000.");
|
||||
}
|
||||
}
|
||||
|
||||
$database->insert('machines', $data);
|
||||
if ($database->error()[1] != 0) {
|
||||
sendError("dberror", $database->error()[2]);
|
||||
}
|
||||
exit(json_encode(["status" => "OK"]));
|
||||
break;
|
||||
case "addhistory":
|
||||
if (empty($VARS['id'])) {
|
||||
sendError("nomachineid");
|
||||
}
|
||||
try {
|
||||
$machine = new Machine($VARS['id']);
|
||||
$machine->addHistory($VARS['date'], $VARS['event'], $VARS['notes']);
|
||||
exit(json_encode(["status" => "OK"]));
|
||||
} catch (Exception $e) {
|
||||
sendError("", $e->getMessage());
|
||||
}
|
||||
break;
|
||||
case "addcomponent":
|
||||
if (empty($VARS['id'])) {
|
||||
sendError("nomachineid");
|
||||
}
|
||||
try {
|
||||
$machine = new Machine($VARS['id']);
|
||||
$machine->addComponent($VARS['serial'], $VARS['type'], $VARS['tested'], $VARS['notes'], $VARS['capacity'], $VARS['model']);
|
||||
exit(json_encode(["status" => "OK"]));
|
||||
} catch (Exception $e) {
|
||||
sendError("", $e->getMessage());
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
sendError("", "Invalid action or no action sent.");
|
||||
}
|
BIN
database.mwb
BIN
database.mwb
Binary file not shown.
46
machine.php
46
machine.php
@ -59,4 +59,50 @@ class Machine {
|
||||
}
|
||||
return $info;
|
||||
}
|
||||
|
||||
public function addHistory($date, $event, $notes = "") {
|
||||
global $database;
|
||||
if (strtotime($date) === false) {
|
||||
throw new Exception("Invalid date.");
|
||||
}
|
||||
$date = date("Y-m-d H:i:s", strtotime($date));
|
||||
if (!$database->has('event_types', ['eventid' => $event])) {
|
||||
throw new Exception("Invalid event type.");
|
||||
}
|
||||
$event = (int) $event;
|
||||
if (empty($notes)) {
|
||||
$notes = "";
|
||||
}
|
||||
$database->insert('history', ['date' => $date, 'eventid' => $event, 'notes' => $notes, 'machineid' => $this->machineid]);
|
||||
}
|
||||
|
||||
public function addComponent($serial, $type, $tested = null, $notes = "", $capacity = null, $model = null) {
|
||||
global $database;
|
||||
if (empty($serial)) {
|
||||
throw new Exception("Invalid serial number.");
|
||||
}
|
||||
if (!$database->has('component_types', ['typeid' => $type])) {
|
||||
throw new Exception("Invalid component type.");
|
||||
}
|
||||
$type = (int) $type;
|
||||
|
||||
if (!is_null($tested)) {
|
||||
if (strtotime($tested) === false) {
|
||||
throw new Exception("Invalid tested date.");
|
||||
}
|
||||
$tested = date("Y-m-d H:i:s", strtotime($tested));
|
||||
}
|
||||
|
||||
if (empty($notes)) {
|
||||
$notes = "";
|
||||
}
|
||||
if (empty($capacity)) {
|
||||
$capacity = null;
|
||||
}
|
||||
if (empty($model)) {
|
||||
$model = null;
|
||||
}
|
||||
|
||||
$database->insert('components', ['serial' => $serial, 'typeid' => $type, 'tested' => $tested, 'notes' => $notes, 'capacity' => $capacity, 'model' => $model, 'machineid' => $this->machineid]);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user