You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.5 KiB
84 lines
2.5 KiB
<?php
|
|
namespace Gateway;
|
|
use Config\DatabaseCon;
|
|
use Config\Connection;
|
|
use PDOException;
|
|
use PDO;
|
|
|
|
class FileGateway {
|
|
private Connection $con;
|
|
|
|
public function __construct() {
|
|
try {
|
|
$this->con = (new DatabaseCon)->connect();
|
|
} catch(PDOException $e) {
|
|
throw new PDOException($e->getMessage(), $e->getCode(), $e);
|
|
}
|
|
}
|
|
|
|
public function createFile(string $filename, string $user_uuid) {
|
|
$query = "INSERT INTO file VALUES(UUID(), :user_uuid, :filename, CURDATE());";
|
|
try {
|
|
$this->con->executeQuery($query, array(
|
|
':filename' => array($filename, PDO::PARAM_STR),
|
|
':user_uuid' => array($user_uuid, PDO::PARAM_STR)
|
|
));
|
|
} catch (PDOException $e) {
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Delete User: (1:OK, 2:Unauthorize, 3:No User)
|
|
public function deleteFile(string $file_uuid) : int {
|
|
$query = "DELETE FROM file WHERE id=:file_uuid;";
|
|
try {
|
|
$this->con->executeQuery($query, array(
|
|
':file_uuid' => array($file_uuid, PDO::PARAM_STR)
|
|
));
|
|
} catch (PDOException $e) {
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public function getFilename(string $file_uuid, string $user_uuid) {
|
|
$query = "SELECT filename FROM file WHERE user_id=:user_uuid and id=:file_uuid;";
|
|
try {
|
|
$this->con->executeQuery($query, array(
|
|
':user_uuid' => array($user_uuid, PDO::PARAM_STR),
|
|
':file_uuid' => array($file_uuid, PDO::PARAM_STR)
|
|
));
|
|
$results = $this->con->getResults();
|
|
} catch (PDOException) {
|
|
return -1;
|
|
}
|
|
if(count($results) === 0) return -2;
|
|
|
|
return $results[0]['filename'];
|
|
}
|
|
|
|
public function listFiles(string $user_uuid) {
|
|
$query = "SELECT f.id, f.filename FROM file f, user u WHERE f.user_id=u.id and u.id=:user_uuid;";
|
|
try {
|
|
$this->con->executeQuery($query, array(
|
|
':user_uuid' => array($user_uuid, PDO::PARAM_STR)
|
|
));
|
|
$results = $this->con->getResults();
|
|
} catch (PDOException $e) {
|
|
return -1;
|
|
}
|
|
|
|
$rows = [];
|
|
foreach ($results as $row) {
|
|
$rows[] = [
|
|
'uuid' => $row['id'],
|
|
'filename' => $row['filename'],
|
|
];
|
|
}
|
|
|
|
return $rows;
|
|
}
|
|
} |