From 7f596b3d2b208efdc45f7a04bf3fcb52d3ee55e7 Mon Sep 17 00:00:00 2001 From: netkas Date: Thu, 29 May 2025 21:14:16 -0400 Subject: [PATCH] Add FileAttachmentManager for managing file attachment records and operations --- .idea/sqldialects.xml | 1 + .../Managers/FileAttachmentManager.php | 123 ++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/FederationServer/Classes/Managers/FileAttachmentManager.php diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml index f7226ae..68f9b80 100644 --- a/.idea/sqldialects.xml +++ b/.idea/sqldialects.xml @@ -2,6 +2,7 @@ + diff --git a/src/FederationServer/Classes/Managers/FileAttachmentManager.php b/src/FederationServer/Classes/Managers/FileAttachmentManager.php new file mode 100644 index 0000000..76489af --- /dev/null +++ b/src/FederationServer/Classes/Managers/FileAttachmentManager.php @@ -0,0 +1,123 @@ + 255) + { + throw new InvalidArgumentException('File name exceeds maximum length of 255 characters.'); + } + + if(!is_numeric($fileSize) || $fileSize <= 0) + { + throw new InvalidArgumentException('File size must be a positive integer.'); + } + + try + { + $stmt = DatabaseConnection::getConnection()->prepare("INSERT INTO file_attachments (uuid, evidence, file_name, file_size) VALUES (:uuid, :evidence, :file_name, :file_size)"); + $stmt->bindParam(':uuid', $uuid); + $stmt->bindParam(':evidence', $evidence); + $stmt->bindParam(':file_name', $fileName); + $stmt->bindParam(':file_size', $fileSize); + + $stmt->execute(); + } + catch (PDOException $e) + { + throw new DatabaseOperationException("Failed to create file attachment record: " . $e->getMessage(), $e->getCode(), $e); + } + } + + /** + * Retrieves a file attachment record by its UUID. + * + * @param string $uuid The UUID of the file attachment record. + * @return FileAttachmentRecord|null The FileAttachmentRecord object if found, null otherwise. + * @throws DatabaseOperationException If there is an error preparing or executing the SQL statement. + */ + public static function getRecord(string $uuid): ?FileAttachmentRecord + { + try + { + $stmt = DatabaseConnection::getConnection()->prepare("SELECT * FROM file_attachments WHERE uuid = :uuid"); + $stmt->bindParam(':uuid', $uuid); + $stmt->execute(); + + $result = $stmt->fetch(\PDO::FETCH_ASSOC); + if($result === false) + { + return null; // No record found + } + + return new FileAttachmentRecord($result); + } + catch (PDOException $e) + { + throw new DatabaseOperationException("Failed to retrieve file attachment record: " . $e->getMessage(), $e->getCode(), $e); + } + } + + /** + * Retrieves all file attachment records associated with a specific evidence UUID. + * + * @param string $evidence The UUID of the evidence record. + * @return FileAttachmentRecord[] An array of FileAttachmentRecord objects. + * @throws DatabaseOperationException If there is an error preparing or executing the SQL statement. + */ + public static function getRecordsByEvidence(string $evidence): array + { + try + { + $stmt = DatabaseConnection::getConnection()->prepare("SELECT * FROM file_attachments WHERE evidence = :evidence"); + $stmt->bindParam(':evidence', $evidence); + $stmt->execute(); + + $results = $stmt->fetchAll(\PDO::FETCH_ASSOC); + return array_map(fn($data) => new FileAttachmentRecord($data), $results); + } + catch (PDOException $e) + { + throw new DatabaseOperationException("Failed to retrieve file attachment records: " . $e->getMessage(), $e->getCode(), $e); + } + } + + /** + * Deletes a file attachment record by its UUID. + * + * @param string $uuid The UUID of the file attachment record to delete. + * @throws DatabaseOperationException If there is an error preparing or executing the SQL statement. + */ + public static function deleteRecord(string $uuid): void + { + try + { + $stmt = DatabaseConnection::getConnection()->prepare("DELETE FROM file_attachments WHERE uuid = :uuid"); + $stmt->bindParam(':uuid', $uuid); + $stmt->execute(); + } + catch (PDOException $e) + { + throw new DatabaseOperationException("Failed to delete file attachment record: " . $e->getMessage(), $e->getCode(), $e); + } + } + } \ No newline at end of file