Add day table

pull/37/head
Varun Patil 2022-08-14 23:19:13 +00:00
parent bac7071f52
commit 61ebfb3f83
4 changed files with 68 additions and 92 deletions

View File

@ -5,7 +5,5 @@ return [
// API
['name' => 'api#list', 'url' => '/api/list', 'verb' => 'GET'],
['name' => 'api#listafter', 'url' => '/api/list/after/{time}', 'verb' => 'GET'],
['name' => 'api#listbefore', 'url' => '/api/list/before/{time}', 'verb' => 'GET'],
]
];

View File

@ -70,45 +70,6 @@ class ApiController extends Controller {
return new JSONResponse($list, Http::STATUS_OK);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse
*/
public function listafter(string $time): JSONResponse {
$user = $this->userSession->getUser();
if (is_null($user)) {
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
}
if (!is_numeric($time)) {
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
}
$list = \OCA\BetterPhotos\Db\Util::getAfter($this->connection, $user->getUID(), intval($time));
return new JSONResponse($list, Http::STATUS_OK);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse
*/
public function listbefore(string $time): JSONResponse {
$user = $this->userSession->getUser();
if (is_null($user)) {
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
}
if (!is_numeric($time)) {
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
}
$list = \OCA\BetterPhotos\Db\Util::getBefore($this->connection, $user->getUID(), intval($time));
return new JSONResponse($list, Http::STATUS_OK);
}
/**
* @NoAdminRequired
*

View File

@ -36,39 +36,51 @@ class Util {
return;
}
$qb = $this->connection->getQueryBuilder();
$qb->select('*')
->from('betterphotos')
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user)))
->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($file->getId())))
->setMaxResults(1);;
$result = $qb->executeQuery();
$row = $result->fetch();
// Get parameters
$fileId = $file->getId();
$dateTaken = $this->getDateTaken($file);
$dayId = 0;
if ($row !== false) {
if ($update) {
$qb = $this->connection->getQueryBuilder();
$qb->update('betterphotos')
->set('date_taken', $qb->createNamedParameter($this->getDateTaken($file), IQueryBuilder::PARAM_INT))
->where($qb->expr()->eq('id', $qb->createNamedParameter($row['id'])));
$qb->executeStatement();
}
// Insert or update file
// todo: update dateTaken and dayId if needed
$sql = 'INSERT IGNORE
INTO oc_betterphotos (user_id, file_id, date_taken, day_id)
VALUES (?, ?, ?, ?)';
$res = $this->connection->executeStatement($sql, [
$user, $fileId, $dateTaken, $dayId,
]);
return;
// Update day table
if ($res === 1) {
$sql = 'INSERT
INTO oc_betterphotos_day (user_id, day_id, count)
VALUES (?, ?, 1)
ON DUPLICATE KEY
UPDATE count = count + 1';
$this->connection->executeStatement($sql, [
$user, $dayId,
]);
}
$qb->insert('betterphotos')
->setValue('user_id', $qb->createNamedParameter($user))
->setValue('file_id', $qb->createNamedParameter($file->getId()))
->setValue('date_taken', $qb->createNamedParameter($this->getDateTaken($file), IQueryBuilder::PARAM_INT));
$qb->executeStatement();
}
public function deleteFile(File $file) {
$qb = $this->connection->getQueryBuilder();
$qb->delete('betterphotos')
->where($qb->expr()->eq('file_id', $qb->createNamedParameter($file->getId())));
$qb->executeStatement();
$sql = 'DELETE
FROM oc_betterphotos
WHERE file_id = ?
RETURNING *';
$res = $this->connection->executeQuery($sql, [$file->getId()], [\PDO::PARAM_INT]);
$rows = $res->fetchAll();
foreach ($rows as $row) {
$dayId = $row['day_id'];
$userId = $row['user_id'];
$sql = 'UPDATE oc_betterphotos_day
SET count = count - 1
WHERE user_id = ? AND day_id = ?';
$this->connection->executeStatement($sql, [$userId, $dayId], [
\PDO::PARAM_STR, \PDO::PARAM_INT,
]);
}
}
private static function getListQuery(
@ -93,28 +105,4 @@ class Util {
$rows = $result->fetchAll();
return $rows;
}
public static function getAfter(
IDBConnection $connection,
string $user,
int $time,
): array {
$qb = self::getListQuery($connection, $user);
$qb->andWhere($qb->expr()->gte('date_taken', $qb->createNamedParameter($time, IQueryBuilder::PARAM_INT)));
$result = $qb->executeQuery();
$rows = $result->fetchAll();
return $rows;
}
public static function getBefore(
IDBConnection $connection,
string $user,
int $time,
): array {
$qb = self::getListQuery($connection, $user);
$qb->andWhere($qb->expr()->lte('date_taken', $qb->createNamedParameter($time, IQueryBuilder::PARAM_INT)));
$result = $qb->executeQuery();
$rows = $result->fetchAll();
return $rows;
}
}

View File

@ -37,10 +37,39 @@
'notnull' => true,
'length' => 20,
]);
$table->addColumn('day_id', Types::INTEGER, [
'notnull' => true,
]);
$table->setPrimaryKey(['id']);
$table->addIndex(['user_id'], 'betterphotos_user_id_index');
$table->addIndex(['day_id'], 'betterphotos_day_id_index');
$table->addUniqueIndex(['user_id', 'file_id'], 'betterphotos_day_uf_ui');
}
if (!$schema->hasTable('betterphotos_day')) {
$table = $schema->createTable('betterphotos_day');
$table->addColumn('id', 'integer', [
'autoincrement' => true,
'notnull' => true,
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 200,
]);
$table->addColumn('count', Types::INTEGER, [
'notnull' => true,
'default' => 0,
]);
$table->addColumn('day_id', Types::INTEGER, [
'notnull' => true,
]);
$table->setPrimaryKey(['id']);
$table->addIndex(['user_id'], 'betterphotos_day_user_id_index');
$table->addUniqueIndex(['user_id', 'day_id'], 'betterphotos_day_ud_ui');
}
return $schema;
}
}