Add new parameter to /api/gallery/list

This commit is contained in:
2023-09-07 18:35:38 +08:00
parent 7e78caab3a
commit 406366772a
2 changed files with 25 additions and 6 deletions

21
db.ts
View File

@@ -909,17 +909,30 @@ export class EhDb {
[limit, offset],
).map((n) => n[0]);
}
get_gmetas(offset = 0, limit = 20, fields = "*") {
get_gmetas(
offset = 0,
limit = 20,
fields = "*",
sort_by_gid: boolean | null = null,
) {
const sort_sql = sort_by_gid !== null
? ` ORDER BY gid ${sort_by_gid ? "ASC" : "DESC"}`
: "";
return this.convert_gmeta(
this.db.queryEntries<GMetaRaw>(
`SELECT ${fields} FROM gmeta LIMIT ? OFFSET ?;`,
`SELECT ${fields} FROM gmeta${sort_sql} LIMIT ? OFFSET ?;`,
[limit, offset],
),
);
}
get_gmetas_all(fields = "*") {
get_gmetas_all(fields = "*", sort_by_gid: boolean | null = null) {
const sort_sql = sort_by_gid !== null
? ` ORDER BY gid ${sort_by_gid ? "ASC" : "DESC"}`
: "";
return this.convert_gmeta(
this.db.queryEntries<GMetaRaw>(`SELECT ${fields} FROM gmeta;`),
this.db.queryEntries<GMetaRaw>(
`SELECT ${fields} FROM gmeta${sort_sql};`,
),
);
}
get_gmeta_by_gid(gid: number) {

View File

@@ -29,6 +29,10 @@ export const handler: Handlers = {
const offset = await parse_int(u.searchParams.get("offset"), 0);
const limit = await parse_int(u.searchParams.get("limit"), 20);
const fields = u.searchParams.get("fields") || "*";
const sort_by_gid = await parse_bool(
u.searchParams.get("sort_by_gid"),
null,
);
if (fields !== "*") {
const fs = fields.split(",");
const ok = fs.every((d) => {
@@ -38,9 +42,11 @@ export const handler: Handlers = {
if (!ok) return return_error(1, "Some fields not allowed.");
}
if (all) {
return return_data(t.db.get_gmetas_all(fields));
return return_data(t.db.get_gmetas_all(fields, sort_by_gid));
} else {
return return_data(t.db.get_gmetas(offset, limit, fields));
return return_data(
t.db.get_gmetas(offset, limit, fields, sort_by_gid),
);
}
},
};