From 828e7ad9994c2b3b13fceaa8631eef2d209c9398 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Mon, 18 Feb 2019 20:39:35 +0000
Subject: [PATCH] Bugfix: Update a significant portion of the backend to the
 new database structure

---
 logic/Actions/FetchData.php                   |  4 ++-
 .../IMeasurementTypeRepository.php            | 15 +++++++---
 .../MariaDBMeasurementTypeRepository.php      | 29 +++++++++++++++----
 3 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/logic/Actions/FetchData.php b/logic/Actions/FetchData.php
index 9f89a08..241a70d 100644
--- a/logic/Actions/FetchData.php
+++ b/logic/Actions/FetchData.php
@@ -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) ]
diff --git a/logic/Repositories/IMeasurementTypeRepository.php b/logic/Repositories/IMeasurementTypeRepository.php
index 8be3abe..264c945 100644
--- a/logic/Repositories/IMeasurementTypeRepository.php
+++ b/logic/Repositories/IMeasurementTypeRepository.php
@@ -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.
diff --git a/logic/Repositories/MariaDBMeasurementTypeRepository.php b/logic/Repositories/MariaDBMeasurementTypeRepository.php
index 66162b2..eead285 100644
--- a/logic/Repositories/MariaDBMeasurementTypeRepository.php
+++ b/logic/Repositories/MariaDBMeasurementTypeRepository.php
@@ -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();
 	}
