aboutsummaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2025-01-19 18:40:48 +0200
committerSyndamia <kamen@syndamia.com>2025-01-19 18:40:48 +0200
commit658fd289774d658347ccc5eafa5c140b8ff48d39 (patch)
tree1f2fec9efc9a142c074d6fb902e0aec0157825a9 /models
parent1fedd90926b64f050d7a8f4284fb7a5125fcae16 (diff)
downloadnowayforward_human-658fd289774d658347ccc5eafa5c140b8ff48d39.tar
nowayforward_human-658fd289774d658347ccc5eafa5c140b8ff48d39.tar.gz
nowayforward_human-658fd289774d658347ccc5eafa5c140b8ff48d39.zip
feat(models): Separate user and webpage out of database
Diffstat (limited to 'models')
-rw-r--r--models/database.php58
-rw-r--r--models/user.php29
-rw-r--r--models/webpage.php35
3 files changed, 64 insertions, 58 deletions
diff --git a/models/database.php b/models/database.php
index c59dec6..1fe05e0 100644
--- a/models/database.php
+++ b/models/database.php
@@ -2,64 +2,6 @@
namespace Database;
use PDO;
-class User extends Table {
- public $UID;
- public $Username;
- public $Password;
- public $Role;
-
- static function create(string $Username, string $Password, string $Role) : int {
- return Table::_create(
- "Users",
- "(Username, Password, Role)",
- "(\"$Username\", \"$Password\", \"$Role\")",
- );
- }
-
- function fromDB(string $username) : User {
- return Table::_fromDB(
- "SELECT * FROM Users WHERE Username = \"$username\"",
- "Database\User"
- );
- }
-
- static function get_all() : array {
- return Table::_get_all("Users", "Database\User");
- }
-}
-
-class Webpage extends Table {
- public $WID;
- public $Path;
- public $URL;
- public $Date;
- public $Visits;
- public $RequesterUID;
-
- static function create(string $Path, string $URL, int $RequesterUID) : int {
- return Table::_create(
- 'Webpages',
- '(Path, URL, Date, Visits, RequesterUID)',
- "(\"$Path\", \"$URL\", NOW(), 0, \"$RequesterUID\")"
- );
- }
-
- static function fromDB(string $URL) : Webpage {
- return Table::_fromDB(
- "SELECT * FROM Webpages WHERE URL = \"$URL\"",
- "Database\Webpage"
- );
- }
-
- static function mostVisited(int $count) : array {
- return Table::_get_all(
- 'Webpages',
- 'Database\Webpage',
- "GROUP BY URL ORDER BY Visits DESC, Date DESC LIMIT $count"
- );
- }
-}
-
abstract class Table {
// Cannot be created, because FETCH_CLASS will assign to all attributes
// and then call the constructor
diff --git a/models/user.php b/models/user.php
new file mode 100644
index 0000000..72933f9
--- /dev/null
+++ b/models/user.php
@@ -0,0 +1,29 @@
+<?php
+namespace Database;
+use PDO;
+
+class User extends Table {
+ public $UID;
+ public $Username;
+ public $Password;
+ public $Role;
+
+ static function create(string $Username, string $Password, string $Role) : int {
+ return Table::_create(
+ "Users",
+ "(Username, Password, Role)",
+ "(\"$Username\", \"$Password\", \"$Role\")",
+ );
+ }
+
+ function fromDB(string $username) : User {
+ return Table::_fromDB(
+ "SELECT * FROM Users WHERE Username = \"$username\"",
+ "Database\User"
+ );
+ }
+
+ static function get_all() : array {
+ return Table::_get_all("Users", "Database\User");
+ }
+}
diff --git a/models/webpage.php b/models/webpage.php
new file mode 100644
index 0000000..35bb3ce
--- /dev/null
+++ b/models/webpage.php
@@ -0,0 +1,35 @@
+<?php
+namespace Database;
+use PDO;
+
+class Webpage extends Table {
+ public $WID;
+ public $Path;
+ public $URL;
+ public $Date;
+ public $Visits;
+ public $RequesterUID;
+
+ static function create(string $Path, string $URL, int $RequesterUID) : int {
+ return Table::_create(
+ 'Webpages',
+ '(Path, URL, Date, Visits, RequesterUID)',
+ "(\"$Path\", \"$URL\", NOW(), 0, \"$RequesterUID\")"
+ );
+ }
+
+ static function fromDB(string $URL) : Webpage {
+ return Table::_fromDB(
+ "SELECT * FROM Webpages WHERE URL = \"$URL\"",
+ "Database\Webpage"
+ );
+ }
+
+ static function mostVisited(int $count) : array {
+ return Table::_get_all(
+ 'Webpages',
+ 'Database\Webpage',
+ "GROUP BY URL ORDER BY Visits DESC, Date DESC LIMIT $count"
+ );
+ }
+}