Skip to content

Commit

Permalink
Add new API /api/tag
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed Aug 27, 2023
1 parent 8b31fe1 commit 4ee6cc3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 20 deletions.
14 changes: 14 additions & 0 deletions db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,20 @@ export class EhDb {
);
return s.length ? s[0] : undefined;
}
get_tag(id: number) {
const s = this.db.queryEntries<Tag>(
"SELECT * FROM tag WHERE id = ?;",
[id],
);
return s.length ? s[0] : undefined;
}
get_tag_by_tag(tag: string) {
const s = this.db.queryEntries<Tag>(
"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<Task>("SELECT * FROM task WHERE id = ?;", [id])
Expand Down
42 changes: 22 additions & 20 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions routes/api/tag/[id].ts
Original file line number Diff line number Diff line change
@@ -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<string, JSONResult<Tag>> = {};
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);
},
};
11 changes: 11 additions & 0 deletions server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ export function return_data<T = unknown>(
return gen_response<T>({ ok: true, status: 0, data }, status, headers);
}

export function gen_data<T = unknown>(data: T): JSONResult<T> {
return { ok: true, status: 0, data };
}

export function gen_error<T = unknown>(
status: Exclude<number, 0>,
error: string,
): JSONResult<T> {
return { ok: false, status, error };
}

export function return_json<T = unknown>(data: T, status = 200) {
return new Response(JSON.stringify(data), {
status,
Expand Down

0 comments on commit 4ee6cc3

Please sign in to comment.