Skip to content

Commit 9619d97

Browse files
committed
Paginate category images
1 parent 346ec3f commit 9619d97

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/db/database.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import postgres from "postgres";
22

3-
import { Category, Image, LocalImageInfo, TagEntry, TagMap } from "../types/drawref.js";
3+
import { Category, CategoryImagePage, Image, LocalImageInfo, TagEntry, TagMap } from "../types/drawref.js";
44
import urlJoin from "url-join";
55
import { allowLocalImages, myURL, uploadUrlPrefix } from "../config/env.js";
66

7+
const defaultImagesPerPage = 20;
8+
79
function tagMapToDbTags(tags: TagMap): string[] {
810
if (tags === undefined) {
911
return [];
@@ -389,16 +391,32 @@ class Database {
389391
}
390392
}
391393

392-
async getCategoryImages(category: string, page: number): Promise<Image[]> {
394+
async getCategoryImages(category: string, page: number): Promise<CategoryImagePage> {
393395
var images: Image[] = [];
394396

395-
const rows = await this.sql`
397+
// get all entries
398+
let rows = await this.sql`
399+
select count(*) as count
400+
from images
401+
inner join image_tags
402+
on images.id = image_tags.image_id
403+
where image_tags.category_id = ${category}
404+
`;
405+
406+
const totalImages = rows[0].count;
407+
// const startAtImage = Math.max(page, 0) * defaultImagesPerPage;
408+
// const endAtImage = startAtImage + Math.min(defaultImagesPerPage, totalImages - startAtImage) - 1;
409+
410+
// get this page
411+
rows = await this.sql`
396412
select image_id, path, external_url, author, author_url, is_local, image_tags.tags
397413
from images
398414
inner join image_tags
399415
on images.id = image_tags.image_id
400416
where image_tags.category_id = ${category}
401417
order by image_tags.created_at desc
418+
limit ${defaultImagesPerPage}
419+
offset ${defaultImagesPerPage * page}
402420
`;
403421
for (const row of rows) {
404422
var img: Image = {
@@ -413,7 +431,12 @@ class Database {
413431
images.push(img);
414432
}
415433

416-
return images;
434+
return {
435+
images,
436+
total_images: totalImages,
437+
page,
438+
total_pages: Math.ceil(totalImages / defaultImagesPerPage),
439+
};
417440
}
418441

419442
async getSessionImages(category: string, tags: TagMap): Promise<Image[]> {

src/routes/categories.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ router.delete("/:category/images/:image", needAdmin, async (req: Request, res: R
4949

5050
router.get("/:category/images", async (req: Request, res: Response) => {
5151
const category = req.params.category || "";
52+
const page = parseInt(String(req.query.page) || "0");
5253

5354
if (!category) {
5455
res.status(400);
@@ -60,8 +61,7 @@ router.get("/:category/images", async (req: Request, res: Response) => {
6061

6162
const db = useDatabase();
6263

63-
//TODO: pull page number out from the query parameters
64-
const images = await db.getCategoryImages(category, 0);
64+
const images = await db.getCategoryImages(category, page);
6565

6666
res.send(images);
6767
});

src/types/drawref.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export type Image = {
2626
tags?: TagMap;
2727
};
2828

29+
export type CategoryImagePage = {
30+
images: Image[];
31+
total_images: number;
32+
page: number;
33+
total_pages: number;
34+
};
35+
2936
export type SampleProviderEntry = {
3037
author: string;
3138
author_url: string;

0 commit comments

Comments
 (0)