[API] Add device-id GET parameter to list-reading-types action..

This commit is contained in:
Starbeamrainbowlabs 2019-02-07 19:15:38 +00:00
parent a6e8d13e1d
commit 3a2b2571f6
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
5 changed files with 42 additions and 4 deletions

View File

@ -87,10 +87,14 @@ Examples:
### list-reading-types
> Lists the different types of readings that can be specified.
_No parameters are currently supported by this action._
Parameter | Type | Meaning
--------------------|-----------|---------------------
`device-id` | int | Optional. If specified, this filters the list of measurement types to list only those reported by the device with the specified id.
```
https://example.com/path/to/api.php?action=list-reading-types
https://example.com/path/to/api.php?action=list-reading-types&device-id=22
https://example.com/path/to/api.php?action=list-reading-types&device-id=54
```

View File

@ -32,9 +32,14 @@ class ListReadingTypes implements IAction {
$start_handle = microtime(true);
// 1: Parse & validate parameters
$device_id = !empty($_GET["device-id"]) ? intval($_GET["device-id"]) : null;
// 1: Pull data from database
$data = $this->types_repo->get_all_types();
$data = null;
if(!is_int($device_id))
$data = $this->types_repo->get_all_types();
else
$data = $this->types_repo->get_types_by_device($device_id);
// 1.5: Validate data from database
if(empty($data)) {

View File

@ -42,7 +42,7 @@ class Database
}
public function query($sql, $variables = []) {
//error_log("[Database/SQL] $sql");
error_log("[Database/SQL] $sql");
// FUTURE: Optionally cache prepared statements?
$statement = $this->connection->prepare($sql);
$statement->execute($variables);

View File

@ -22,4 +22,10 @@ interface IMeasurementTypeRepository {
* @return array All the measurement types currently known.
*/
public function get_all_types();
/**
* Gets the all the measurement types ever reported by a given device id.
* @return string[] A list of device ids.
*/
public function get_types_by_device(int $device_id);
}

View File

@ -19,12 +19,16 @@ class MariaDBMeasurementTypeRepository implements IMeasurementTypeRepository {
*/
private $database;
/** Function that gets a static variable by it's name. Useful in preparing SQL queries. */
/** Functions that get a static variable by it's name. Useful in preparing SQL queries. */
private $get_static;
private $get_static_extra;
function __construct(\AirQuality\Database $in_database) {
$this->database = $in_database;
$this->get_static = function($name) { return self::$$name; };
$this->get_static_extra = function($class_name, $name) {
return $class_name::$$name;
};
}
public function is_valid_type(string $type_name) : bool {
@ -50,4 +54,23 @@ class MariaDBMeasurementTypeRepository implements IMeasurementTypeRepository {
"SELECT * FROM {$s("table_name")}"
)->fetchAll();
}
public function get_types_by_device($device_id) {
$s = $this->get_static;
$o = $this->get_static_extra;
return $this->database->query(
"SELECT
{$s("table_name")}.*,
COUNT({$s("table_name")}.{$s("column_id")}) AS count
FROM {$o(MariaDBMeasurementDataRepository::class, "table_name_values")}
JOIN {$o(MariaDBMeasurementDataRepository::class, "table_name_metadata")} ON
{$o(MariaDBMeasurementDataRepository::class, "table_name_metadata")}.{$o(MariaDBMeasurementDataRepository::class, "column_metadata_id")} = {$o(MariaDBMeasurementDataRepository::class, "table_name_values")}.{$o(MariaDBMeasurementDataRepository::class, "column_values_reading_id")}
JOIN {$s("table_name")} ON
{$s("table_name")}.{$s("column_id")} = {$o(MariaDBMeasurementDataRepository::class, "table_name_values")}.{$o(MariaDBMeasurementDataRepository::class, "column_values_reading_type")}
WHERE {$o(MariaDBMeasurementDataRepository::class, "table_name_metadata")}.{$o(MariaDBMeasurementDataRepository::class, "column_metadata_device_id")} = :device_id
GROUP BY {$s("table_name")}.{$s("column_id")};", [
"device_id" => $device_id
]
)->fetchAll();
}
}