Add days api

pull/37/head
Varun Patil 2022-08-14 23:31:47 +00:00
parent 61ebfb3f83
commit aa008dbe91
3 changed files with 41 additions and 17 deletions

View File

@ -4,6 +4,7 @@ return [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
// API
['name' => 'api#list', 'url' => '/api/list', 'verb' => 'GET'],
['name' => 'api#days', 'url' => '/api/days', 'verb' => 'GET'],
['name' => 'api#day', 'url' => '/api/days/{id}', 'verb' => 'GET'],
]
];

View File

@ -54,19 +54,35 @@ class ApiController extends Controller {
$this->connection = $connection;
}
/**
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse
*/
public function list(): JSONResponse {
public function days(): JSONResponse {
$user = $this->userSession->getUser();
if (is_null($user)) {
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
}
$list = \OCA\BetterPhotos\Db\Util::getList($this->connection, $user->getUID());
$list = \OCA\BetterPhotos\Db\Util::getDays($this->connection, $user->getUID());
return new JSONResponse($list, Http::STATUS_OK);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse
*/
public function day(string $id): JSONResponse {
$user = $this->userSession->getUser();
if (is_null($user) || !is_numeric($id)) {
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
}
$list = \OCA\BetterPhotos\Db\Util::getDay($this->connection, $user->getUID(), intval($id));
return new JSONResponse($list, Http::STATUS_OK);
}

View File

@ -39,7 +39,7 @@ class Util {
// Get parameters
$fileId = $file->getId();
$dateTaken = $this->getDateTaken($file);
$dayId = 0;
$dayId = floor($dateTaken / 86400);
// Insert or update file
// todo: update dateTaken and dayId if needed
@ -83,24 +83,31 @@ class Util {
}
}
private static function getListQuery(
public static function getDays(
IDBConnection $connection,
string $user,
) {
): array {
$qb = $connection->getQueryBuilder();
$qb->select('day_id', 'count')
->from('betterphotos_day')
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user)))
->orderBy('day_id', 'DESC');
$result = $qb->executeQuery();
$rows = $result->fetchAll();
return $rows;
}
public static function getDay(
IDBConnection $connection,
string $user,
int $dayId,
): array {
$qb = $connection->getQueryBuilder();
$qb->select('file_id', 'date_taken')
->from('betterphotos')
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user)))
->orderBy('date_taken', 'DESC')
->setMaxResults(100);
return $qb;
}
public static function getList(
IDBConnection $connection,
string $user,
): array {
$qb = self::getListQuery($connection, $user);
->andWhere($qb->expr()->eq('day_id', $qb->createNamedParameter($dayId)))
->orderBy('date_taken', 'DESC');
$result = $qb->executeQuery();
$rows = $result->fetchAll();
return $rows;