diff --git a/db.ts b/db.ts index 85688d7..5147138 100644 --- a/db.ts +++ b/db.ts @@ -1007,6 +1007,20 @@ export class EhDb { ); return s.length ? s[0] : undefined; } + get_tag(id: number) { + const s = this.db.queryEntries( + "SELECT * FROM tag WHERE id = ?;", + [id], + ); + return s.length ? s[0] : undefined; + } + get_tag_by_tag(tag: string) { + const s = this.db.queryEntries( + "SELECT * FROM tag WHERE tag = ?;", + [tag], + ); + return s.length ? s[0] : undefined; + } async get_task(id: number) { const s = await this.transaction(() => this.db.queryEntries("SELECT * FROM task WHERE id = ?;", [id]) diff --git a/fresh.gen.ts b/fresh.gen.ts index 85a5538..a61cb3b 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -16,16 +16,17 @@ import * as $10 from "./routes/api/files/[token].ts"; import * as $11 from "./routes/api/gallery/[gid].ts"; import * as $12 from "./routes/api/gallery/list.ts"; import * as $13 from "./routes/api/status.ts"; -import * as $14 from "./routes/api/task.ts"; -import * as $15 from "./routes/api/thumbnail/[id].ts"; -import * as $16 from "./routes/api/token.ts"; -import * as $17 from "./routes/api/user.ts"; -import * as $18 from "./routes/file/[id].ts"; -import * as $19 from "./routes/file/_middleware.ts"; -import * as $20 from "./routes/index.tsx"; -import * as $21 from "./routes/manifest.json.ts"; -import * as $22 from "./routes/thumbnail/[id].ts"; -import * as $23 from "./routes/thumbnail/_middleware.ts"; +import * as $14 from "./routes/api/tag/[id].ts"; +import * as $15 from "./routes/api/task.ts"; +import * as $16 from "./routes/api/thumbnail/[id].ts"; +import * as $17 from "./routes/api/token.ts"; +import * as $18 from "./routes/api/user.ts"; +import * as $19 from "./routes/file/[id].ts"; +import * as $20 from "./routes/file/_middleware.ts"; +import * as $21 from "./routes/index.tsx"; +import * as $22 from "./routes/manifest.json.ts"; +import * as $23 from "./routes/thumbnail/[id].ts"; +import * as $24 from "./routes/thumbnail/_middleware.ts"; import * as $$0 from "./islands/Container.tsx"; import * as $$1 from "./islands/Settings.tsx"; import * as $$2 from "./islands/TaskManager.tsx"; @@ -46,16 +47,17 @@ const manifest = { "./routes/api/gallery/[gid].ts": $11, "./routes/api/gallery/list.ts": $12, "./routes/api/status.ts": $13, - "./routes/api/task.ts": $14, - "./routes/api/thumbnail/[id].ts": $15, - "./routes/api/token.ts": $16, - "./routes/api/user.ts": $17, - "./routes/file/[id].ts": $18, - "./routes/file/_middleware.ts": $19, - "./routes/index.tsx": $20, - "./routes/manifest.json.ts": $21, - "./routes/thumbnail/[id].ts": $22, - "./routes/thumbnail/_middleware.ts": $23, + "./routes/api/tag/[id].ts": $14, + "./routes/api/task.ts": $15, + "./routes/api/thumbnail/[id].ts": $16, + "./routes/api/token.ts": $17, + "./routes/api/user.ts": $18, + "./routes/file/[id].ts": $19, + "./routes/file/_middleware.ts": $20, + "./routes/index.tsx": $21, + "./routes/manifest.json.ts": $22, + "./routes/thumbnail/[id].ts": $23, + "./routes/thumbnail/_middleware.ts": $24, }, islands: { "./islands/Container.tsx": $$0, diff --git a/routes/api/tag/[id].ts b/routes/api/tag/[id].ts new file mode 100644 index 0000000..9a68a7a --- /dev/null +++ b/routes/api/tag/[id].ts @@ -0,0 +1,39 @@ +import { Handlers } from "$fresh/server.ts"; +import { get_task_manager } from "../../../server.ts"; +import { Tag } from "../../../db.ts"; +import { + gen_data, + gen_error, + JSONResult, + return_data, +} from "../../../server/utils.ts"; + +export const handler: Handlers = { + GET(_req, ctx) { + const ids = ctx.params.id.split(","); + const r: Record> = {}; + for (const _id of ids) { + const id = parseInt(_id); + let key: string | undefined; + let tag: string | undefined; + if (isNaN(id)) { + tag = _id; + } + const m = get_task_manager(); + let t: Tag | undefined; + if (tag) { + t = m.db.get_tag_by_tag(tag); + key = tag; + } else { + t = m.db.get_tag(id); + key = id.toString(); + } + if (t) { + r[key] = gen_data(t); + } else { + r[key] = gen_error(404, "tag not found."); + } + } + return return_data(r); + }, +}; diff --git a/server/utils.ts b/server/utils.ts index b5b8444..40357e8 100644 --- a/server/utils.ts +++ b/server/utils.ts @@ -41,6 +41,17 @@ export function return_data( return gen_response({ ok: true, status: 0, data }, status, headers); } +export function gen_data(data: T): JSONResult { + return { ok: true, status: 0, data }; +} + +export function gen_error( + status: Exclude, + error: string, +): JSONResult { + return { ok: false, status, error }; +} + export function return_json(data: T, status = 200) { return new Response(JSON.stringify(data), { status,