From 69e1a2a776103b0139006a39b9d032f7235eea77 Mon Sep 17 00:00:00 2001 From: NaysKutzu Date: Sat, 20 Jul 2024 11:30:23 +0200 Subject: [PATCH] PUSH -> Finish! --- .vscode/vscode.code-snippets | 82 ++- app/Handlers/AnnouncementHandler.php | 477 +++++++++++++++--- .../AnnouncementNotFoundException.php | 11 + .../interfaces/AnnouncementSocial.php | 9 + migrate/database/2.sql | 1 + settings.json | 4 +- 6 files changed, 524 insertions(+), 60 deletions(-) create mode 100644 app/Handlers/exception/AnnouncementNotFoundException.php create mode 100644 app/Handlers/interfaces/AnnouncementSocial.php create mode 100644 migrate/database/2.sql diff --git a/.vscode/vscode.code-snippets b/.vscode/vscode.code-snippets index b786d1a..6b0370a 100755 --- a/.vscode/vscode.code-snippets +++ b/.vscode/vscode.code-snippets @@ -11,10 +11,90 @@ "* LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER", "* TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER", "*/", - "Logger::log(Logger::$1,Logger::$2,\"$3\");" + "Logger::log(LoggerLevels::$1,LoggerTypes::$2,\"$3\");" ], "description": "Some error logging!" }, + "S_LOG_INFO": { + "prefix": "S_LOG_INFO", + "scope": "php", + "body": [ + "/**", + "* Logger", + "* ", + "* Logs something: LEVEL, TYPE, MESSAGE", + "* ", + "* LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER", + "* TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER", + "*/", + "Logger::log(LoggerLevels::INFO,LoggerTypes::OTHER,\"$1\");" + ], + "description": "Some info logging!" + }, + "S_LOG_WARNING": { + "prefix": "S_LOG_WARNING", + "scope": "php", + "body": [ + "/**", + "* Logger", + "* ", + "* Logs something: LEVEL, TYPE, MESSAGE", + "* ", + "* LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER", + "* TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER", + "*/", + "Logger::log(LoggerLevels::WARNING,LoggerTypes::OTHER,\"$1\");" + ], + "description": "Some warning logging!" + }, + "S_LOG_ERROR": { + "prefix": "S_LOG_ERROR", + "scope": "php", + "body": [ + "/**", + "* Logger", + "* ", + "* Logs something: LEVEL, TYPE, MESSAGE", + "* ", + "* LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER", + "* TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER", + "*/", + "Logger::log(LoggerLevels::ERROR,LoggerTypes::OTHER,\"$1\");" + ], + "description": "Some error logging!" + }, + "S_LOG_CRITICAL": { + "prefix": "S_LOG_CRITICAL", + "scope": "php", + "body": [ + "/**", + "* Logger", + "* ", + "* Logs something: LEVEL, TYPE, MESSAGE", + "* ", + "* LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER", + "* TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER", + "*/", + "Logger::log(LoggerLevels::CRITICAL,LoggerTypes::OTHER,\"$1\");" + ], + "description": "Some critical logging!" + }, + "S_LOG_OTHER": { + "prefix": "S_LOG_OTHER", + "scope": "php", + "body": [ + "/**", + "* Logger", + "* ", + "* Logs something: LEVEL, TYPE, MESSAGE", + "* ", + "* LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER", + "* TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER", + "*/", + "Logger::log(LoggerLevels::OTHER,LoggerTypes::OTHER,\"$1\");" + ], + "description": "Some other logging!" + }, "Get translation": { "prefix": "S_GetLang", "scope": "php", diff --git a/app/Handlers/AnnouncementHandler.php b/app/Handlers/AnnouncementHandler.php index 24f5605..89970c0 100755 --- a/app/Handlers/AnnouncementHandler.php +++ b/app/Handlers/AnnouncementHandler.php @@ -3,16 +3,49 @@ namespace MythicalSystemsFramework\Handlers; use MythicalSystemsFramework\Database\MySQL; +use Exception; +use MythicalSystemsFramework\Handlers\exception\AnnouncementNotFoundException; +use MythicalSystemsFramework\Handlers\interfaces\AnnouncementSocial; +use MythicalSystemsFramework\Kernel\Logger; +use MythicalSystemsFramework\Kernel\LoggerTypes; +use MythicalSystemsFramework\Kernel\LoggerLevels; -class AnnouncementHandler +class AnnouncementHandler implements AnnouncementSocial { - //TODO: - // Add a function to get announcements read - // Add a function to mark announcements as read - // Add a function to like announcements - // Add a function to dislike announcements - // Add a function to get likes from announcements - + /** + * Does an announcement exist? + * + * @param int $id + * + * @return bool + */ + public static function exists(int $id): bool + { + try { + $mysql = new MySQL(); + $conn = $mysql->connectMYSQLI(); + + $stmt = $conn->prepare("SELECT * FROM framework_announcements WHERE id = ?"); + $stmt->bind_param("i", $id); + $stmt->execute(); + + $stmt->close(); + return $stmt->affected_rows > 0; + + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while checking if an announcement exists: " . $e->getMessage()); + return false; + } + } + /** * Create a new announcement. * @@ -23,15 +56,28 @@ class AnnouncementHandler */ public static function create(string $title, string $text): int { - $mysqli = new MySQL(); - $conn = $mysqli->connectMYSQLI(); - - $stmt = $conn->prepare("INSERT INTO framework_announcements (title, text, date) VALUES (?, ?, NOW())"); - $stmt->bind_param("ss", $title, $text); - $stmt->execute(); - $announcementID = $stmt->insert_id; - $stmt->close(); - return $announcementID; + try { + $mysqli = new MySQL(); + $conn = $mysqli->connectMYSQLI(); + + $stmt = $conn->prepare("INSERT INTO framework_announcements (title, text, date) VALUES (?, ?, NOW())"); + $stmt->bind_param("ss", $title, $text); + $stmt->execute(); + $announcementID = $stmt->insert_id; + $stmt->close(); + return $announcementID; + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while creating an announcement: " . $e->getMessage()); + return -1; + } } /** @@ -45,13 +91,38 @@ public static function create(string $title, string $text): int */ public static function edit(int $id, string $title, string $text): void { - $mysqli = new MySQL(); - $conn = $mysqli->connectMYSQLI(); + try { + if (!self::exists($id)) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::WARNING, LoggerTypes::OTHER, "An error occurred while editing an announcement: Announcement not found."); + throw new AnnouncementNotFoundException(); + } + $mysqli = new MySQL(); + $conn = $mysqli->connectMYSQLI(); - $stmt = $conn->prepare("UPDATE framework_announcements SET title = ?, text = ? WHERE id = ?"); - $stmt->bind_param("ssi", $title, $text, $id); - $stmt->execute(); - $stmt->close(); + $stmt = $conn->prepare("UPDATE framework_announcements SET title = ?, text = ? WHERE id = ?"); + $stmt->bind_param("ssi", $title, $text, $id); + $stmt->execute(); + $stmt->close(); + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while editing an announcement: " . $e->getMessage()); + throw new Exception("", $e->getCode(), $e); + } } /** @@ -62,13 +133,38 @@ public static function edit(int $id, string $title, string $text): void */ public static function delete(int $id): void { - $mysqli = new MySQL(); - $conn = $mysqli->connectMYSQLI(); + try { + if (!self::exists($id)) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::WARNING, LoggerTypes::OTHER, "An error occurred while deleting an announcement: Announcement not found."); + throw new AnnouncementNotFoundException(); + } + $mysqli = new MySQL(); + $conn = $mysqli->connectMYSQLI(); - $stmt = $conn->prepare("DELETE FROM framework_announcements WHERE id = ?"); - $stmt->bind_param("i", $id); - $stmt->execute(); - $stmt->close(); + $stmt = $conn->prepare("DELETE FROM framework_announcements WHERE id = ?"); + $stmt->bind_param("i", $id); + $stmt->execute(); + $stmt->close(); + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while deleting an announcement: " . $e->getMessage()); + throw new Exception("", $e->getCode(), $e); + } } /** @@ -78,10 +174,23 @@ public static function delete(int $id): void */ public static function deleteAll(): void { - $mysqli = new MySQL(); - $conn = $mysqli->connectMYSQLI(); + try { + $mysqli = new MySQL(); + $conn = $mysqli->connectMYSQLI(); - $conn->query("TRUNCATE TABLE framework_announcements"); + $conn->query("TRUNCATE TABLE framework_announcements"); + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while deleting all announcements: " . $e->getMessage()); + throw new Exception("", $e->getCode(), $e); + } } /** @@ -92,16 +201,41 @@ public static function deleteAll(): void */ public static function getOne(int $id): ?array { - $mysqli = new MySQL(); - $conn = $mysqli->connectMYSQLI(); - - $stmt = $conn->prepare("SELECT * FROM framework_announcements WHERE id = ?"); - $stmt->bind_param("i", $id); - $stmt->execute(); - $result = $stmt->get_result(); - $announcement = $result->fetch_assoc(); - $stmt->close(); - return $announcement ? $announcement : null; + try { + if (!self::exists($id)) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::WARNING, LoggerTypes::OTHER, "An error occurred while getting an announcement: Announcement not found."); + return []; + } + $mysqli = new MySQL(); + $conn = $mysqli->connectMYSQLI(); + + $stmt = $conn->prepare("SELECT * FROM framework_announcements WHERE id = ?"); + $stmt->bind_param("i", $id); + $stmt->execute(); + $result = $stmt->get_result(); + $announcement = $result->fetch_assoc(); + $stmt->close(); + return $announcement ? $announcement : null; + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while getting an announcement: " . $e->getMessage()); + return []; + } } /** @@ -111,12 +245,26 @@ public static function getOne(int $id): ?array */ public static function getAll(): array { - $mysqli = new MySQL(); - $conn = $mysqli->connectMYSQLI(); + try { + + $mysqli = new MySQL(); + $conn = $mysqli->connectMYSQLI(); - $result = $conn->query("SELECT * FROM framework_announcements"); - $framework_announcements = $result->fetch_all(MYSQLI_ASSOC); - return $framework_announcements; + $result = $conn->query("SELECT * FROM framework_announcements"); + $framework_announcements = $result->fetch_all(MYSQLI_ASSOC); + return $framework_announcements; + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while getting all announcements: " . $e->getMessage()); + return []; + } } /** @@ -126,12 +274,25 @@ public static function getAll(): array */ public static function getAllSortedById(): array { - $mysqli = new MySQL(); - $conn = $mysqli->connectMYSQLI(); + try { + $mysqli = new MySQL(); + $conn = $mysqli->connectMYSQLI(); - $result = $conn->query("SELECT * FROM framework_announcements ORDER BY id DESC"); - $framework_announcements = $result->fetch_all(MYSQLI_ASSOC); - return $framework_announcements; + $result = $conn->query("SELECT * FROM framework_announcements ORDER BY id DESC"); + $framework_announcements = $result->fetch_all(MYSQLI_ASSOC); + return $framework_announcements; + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while getting all announcements sorted by ID: " . $e->getMessage()); + return []; + } } /** @@ -141,11 +302,213 @@ public static function getAllSortedById(): array */ public static function getAllSortedByDate(): array { - $mysqli = new MySQL(); - $conn = $mysqli->connectMYSQLI(); + try { + $mysqli = new MySQL(); + $conn = $mysqli->connectMYSQLI(); + + $result = $conn->query("SELECT * FROM framework_announcements ORDER BY date DESC"); + $framework_announcements = $result->fetch_all(MYSQLI_ASSOC); + return $framework_announcements; + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while getting all announcements sorted by date: " . $e->getMessage()); + return []; + } + } + /** + * Summary of addSocialInteraction + * + * @param string $announcement_id + * @param string $user_uuid + * @param \MythicalSystemsFramework\Handlers\interfaces\AnnouncementSocial $type + * + * @throws \Exception + * @return void + */ + public static function addSocialInteraction(string $announcement_id, string $user_uuid, AnnouncementSocial $type): void + { + try { + if (!self::exists($announcement_id)) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::WARNING, LoggerTypes::OTHER, "An error occurred while adding a social interaction to an announcement: Announcement not found."); + throw new AnnouncementNotFoundException(); + } + $mysqli = new MySQL(); + $conn = $mysqli->connectMYSQLI(); + $stmt = $conn->prepare("INSERT INTO framework_announcements_social (announcement_id, user_uuid, type) VALUES (?, ?, ?)"); + $stmt->bind_param("iss", $announcement_id, $user_uuid, $type); + $stmt->execute(); + $stmt->close(); + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while adding a social interaction to an announcement: " . $e->getMessage()); + throw new Exception('' . $e->getMessage()); + } + } + /** + * Summary of removeSocialInteraction + * + * @param string $announcement_id + * @param string $user_uuid + * @param \MythicalSystemsFramework\Handlers\interfaces\AnnouncementSocial $type + * + * @throws \Exception + * @return void + */ + public static function removeSocialInteraction(string $announcement_id, string $user_uuid, AnnouncementSocial $type): void + { + + try { + if (!self::exists($announcement_id)) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::WARNING, LoggerTypes::OTHER, "An error occurred while removing a social interaction from an announcement: Announcement not found."); + throw new AnnouncementNotFoundException(); + } + $mysqli = new MySQL(); + $conn = $mysqli->connectMYSQLI(); + $stmt = $conn->prepare("DELETE FROM framework_announcements_social WHERE announcement_id = ? AND user_uuid = ? AND type = ?"); + $stmt->bind_param("iss", $announcement_id, $user_uuid, $type); + $stmt->execute(); + $stmt->close(); + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while removing a social interaction from an announcement: " . $e->getMessage()); + throw new Exception('' . $e->getMessage()); + } + } + /** + * Summary of getSocialInteraction + * + * @param string $announcement_id + * @param string $user_uuid + * @param string $type + * + * @return bool + */ + public static function getSocialInteraction(string $announcement_id, string $user_uuid, string $type): bool + { + try { + if (!self::exists($announcement_id)) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::WARNING, LoggerTypes::OTHER, "An error occurred while getting a social interaction from an announcement: Announcement not found."); + return false; + } + $mysqli = new MySQL(); + $conn = $mysqli->connectMYSQLI(); + + $stmt = $conn->prepare('SELECT * FROM framework_announcements_social WHERE announcement_id = ? AND user_uuid = ? AND type = ?'); + $stmt->bind_param('iss', $announcement_id, $user_uuid, $type); + $stmt->execute(); + $stmt->close(); + + if ($stmt->num_rows > 0) { + return true; + } else { + return false; + } - $result = $conn->query("SELECT * FROM framework_announcements ORDER BY date DESC"); - $framework_announcements = $result->fetch_all(MYSQLI_ASSOC); - return $framework_announcements; + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while getting a social interaction from an announcement: " . $e->getMessage()); + return false; + } } + /** + * Summary of getTotalSocialInteractions + * + * @param string $announcement_id + * @param string $type + * + * @return int + */ + public static function getTotalSocialInteractions(string $announcement_id, string $type): int + { + try { + if (!self::exists($announcement_id)) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::WARNING, LoggerTypes::OTHER, "An error occurred while getting the total social interactions from an announcement: Announcement not found."); + return 0; + } + $mysqli = new MySQL(); + $conn = $mysqli->connectMYSQLI(); + + $stmt = $conn->prepare('SELECT + FROM framework_announcements_social WHERE announcement_id = ? AND type = ?'); + $stmt->bind_param('is', $announcement_id, $type); + $stmt->execute(); + $stmt->close(); + if ($stmt->num_rows > 0) { + return $stmt->num_rows; + } else { + return 0; + } + } catch (Exception $e) { + /** + * Logger + * + * Logs something: LEVEL, TYPE, MESSAGE + * + * LEVELS: INFO, WARNING, ERROR, CRITICAL, OTHER + * TYPE: OTHER, CORE, DATABASE, PLUGIN, LOG, OTHER + */ + Logger::log(LoggerLevels::ERROR, LoggerTypes::OTHER, "An error occurred while getting the total social interactions from an announcement: " . $e->getMessage()); + return 0; + } + } + } diff --git a/app/Handlers/exception/AnnouncementNotFoundException.php b/app/Handlers/exception/AnnouncementNotFoundException.php new file mode 100644 index 0000000..ac11ab0 --- /dev/null +++ b/app/Handlers/exception/AnnouncementNotFoundException.php @@ -0,0 +1,11 @@ +