logger = $logger; $this->db_filename = $db_filename; } /** * Populates the database with the necessary structures for users. * * @return void */ public function install(): void { $db = new SQLite3($this->db_filename, SQLITE3_OPEN_READWRITE); $db->exec("CREATE TABLE trackings(user_uuid text not null, person_name text not null, is_deceased int not null default 0, PRIMARY KEY (user_uuid, person_name));"); $db->close(); } /** * Adds a tracking to the database. * * @param string $user_uuid the user to whom the tracking belongs * @param string $person_name the name of the person to track * @return Response a response with no message, and a status indicating whether the insertion succeeded */ public function add_tracking(string $user_uuid, string $person_name): Response { $db = new SQLite3($this->db_filename, SQLITE3_OPEN_READWRITE); $stmt = $db->prepare("INSERT INTO trackings (user_uuid, person_name) VALUES (:user_uuid, :person_name);"); $stmt->bindValue(":user_uuid", $user_uuid); $stmt->bindValue(":person_name", $person_name); $inserted = $stmt->execute() !== false; $db->close(); return new Response(null, $inserted); } /** * Removes a tracking from the database. * * @param string $user_uuid the user to whom the tracking belongs * @param string $person_name the name of the tracked person to remove * @return Response a response with no message, and a status indicating whether the removal succeeded */ public function remove_tracking(string $user_uuid, string $person_name): Response { $db = new SQLite3($this->db_filename, SQLITE3_OPEN_READWRITE); $stmt = $db->prepare("DELETE FROM trackings WHERE user_uuid=:user_uuid AND person_name=:person_name;"); $stmt->bindValue(":user_uuid", $user_uuid); $stmt->bindValue(":person_name", $person_name); $inserted = $stmt->execute() !== false; $db->close(); return new Response(null, $inserted); } /** * Lists all trackings of the indicated user. * * @param string $user_uuid the user to return the trackings of * @return Response a response with all trackings of the indicated user */ public function list_trackings(string $user_uuid): Response { $db = new SQLite3($this->db_filename, SQLITE3_OPEN_READONLY); $stmt = $db->prepare("SELECT * FROM trackings WHERE user_uuid=:user_uuid;"); $stmt->bindValue(":user_uuid", $user_uuid); $results = $stmt->execute(); $trackings = []; while ($row = $results->fetchArray(SQLITE3_ASSOC)) $trackings[] = $row; $db->close(); return new Response($trackings, true); } }