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 // API
['name' => 'api#list', 'url' => '/api/list', 'verb' => 'GET'], ['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); 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 * @NoAdminRequired
* *

View File

@ -36,39 +36,51 @@ class Util {
return; return;
} }
$qb = $this->connection->getQueryBuilder(); // Get parameters
$qb->select('*') $fileId = $file->getId();
->from('betterphotos') $dateTaken = $this->getDateTaken($file);
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user))) $dayId = 0;
->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($file->getId())))
->setMaxResults(1);;
$result = $qb->executeQuery();
$row = $result->fetch();
if ($row !== false) { // Insert or update file
if ($update) { // todo: update dateTaken and dayId if needed
$qb = $this->connection->getQueryBuilder(); $sql = 'INSERT IGNORE
$qb->update('betterphotos') INTO oc_betterphotos (user_id, file_id, date_taken, day_id)
->set('date_taken', $qb->createNamedParameter($this->getDateTaken($file), IQueryBuilder::PARAM_INT)) VALUES (?, ?, ?, ?)';
->where($qb->expr()->eq('id', $qb->createNamedParameter($row['id']))); $res = $this->connection->executeStatement($sql, [
$qb->executeStatement(); $user, $fileId, $dateTaken, $dayId,
]);
// 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,
]);
} }
return;
}
$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) { public function deleteFile(File $file) {
$qb = $this->connection->getQueryBuilder(); $sql = 'DELETE
$qb->delete('betterphotos') FROM oc_betterphotos
->where($qb->expr()->eq('file_id', $qb->createNamedParameter($file->getId()))); WHERE file_id = ?
$qb->executeStatement(); 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( private static function getListQuery(
@ -93,28 +105,4 @@ class Util {
$rows = $result->fetchAll(); $rows = $result->fetchAll();
return $rows; 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, 'notnull' => true,
'length' => 20, 'length' => 20,
]); ]);
$table->addColumn('day_id', Types::INTEGER, [
'notnull' => true,
]);
$table->setPrimaryKey(['id']); $table->setPrimaryKey(['id']);
$table->addIndex(['user_id'], 'betterphotos_user_id_index'); $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; return $schema;
} }
} }