Bugfix: Update a significant portion of the backend to the new database structure

This commit is contained in:
Starbeamrainbowlabs 2019-02-18 20:39:35 +00:00
parent 413c70ce2f
commit 828e7ad999
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
3 changed files with 38 additions and 10 deletions

View File

@ -49,7 +49,9 @@ class FetchData implements IAction {
$this->validator->is_max_length("reading_type", 256);
$this->validator->run();
if(!$this->type_repo->is_valid_type($_GET["reading_type"])) {
$measurement_type_id = $this->type_repo->get_id($_GET["reading_type"]);
if($measurement_type_id == null) {
$this->sender->send_error_plain(
400, "Error: That reading type is invalid.", [
[ "x-time-taken", PerfFormatter::format_perf_data($start_time, $start_handle, null) ]

View File

@ -5,17 +5,24 @@ namespace AirQuality\Repositories;
interface IMeasurementTypeRepository {
/**
* Returns whether the specified type is valid or not.
* @param string $type_name The name of the type to validate.
* @param string $id The id of the type to validate.
* @return bool Whether the specified type name is valid or not.
*/
public function is_valid_type(string $type_name) : bool;
public function is_valid_type(int $id) : bool;
/**
* Get the id associated with the given short description.
* @param string $short_descr The short description to convert.
* @return int The id associated with the specified short description.
*/
public function get_id(string $short_descr) : int;
/**
* Gets the friendly name for the specified type name.
* @param string $type_name The type name to get the friendly name for.
* @param int $id The measurement type id to get the friendly name for.
* @return string The friendly name for the specified type name.
*/
public function get_friendly_name(string $type_name) : string;
public function get_friendly_name(int $id) : string;
/**
* Returns all the currently known meeasurement types.

View File

@ -9,6 +9,7 @@ class MariaDBMeasurementTypeRepository implements IMeasurementTypeRepository {
public static $table_name = "reading_value_types";
public static $column_id = "id";
public static $column_short_description = "short_descr";
public static $column_friendly_text = "friendly_text";
// ------------------------------------------------------------------------
@ -31,19 +32,37 @@ class MariaDBMeasurementTypeRepository implements IMeasurementTypeRepository {
};
}
public function is_valid_type(string $type_name) : bool {
public function is_valid_type(int $id) : bool {
$s = $this->get_static;
return !empty($this->database->query(
"SELECT {$s("column_id")} FROM {$s("table_name")};"
"SELECT {$s("column_id")} FROM {$s("table_name")} WHERE {$s("column_id")} = :id;", [
"id" => $id
]
)->fetchColumn());
}
public function get_friendly_name(string $type_name) : string {
public function get_id(string $short_descr) : int {
$s = $this->get_static;
$result = $this->database->query(
"SELECT {$s("column_id")} FROM {$s("table_name")} WHERE {$s("column_short_description")} = :short_descr", [
"short_descr" => $short_descr
]
)->fetchColumn();
if($result == false)
return null;
return $result;
}
public function get_friendly_name(int $id) : string {
// TODO: Cache results here? Maybe https://packagist.org/packages/thumbtack/querycache will be of some use
$s = $this->get_static;
return $this->database->query(
"SELECT {$s("column_friendly_text")} FROM {$s("table_name")} WHERE $this->column_id = :type_name;", [
"type_name" => $type_name
"SELECT {$s("column_friendly_text")} FROM {$s("table_name")} WHERE $this->column_id = :id;", [
"id" => $id
]
)->fetchColumn();
}