mirror of
https://github.com/lifegpc/eh-downloader.git
synced 2026-07-08 01:31:00 +08:00
Add new API /api/tag
This commit is contained in:
14
db.ts
14
db.ts
@@ -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])
|
||||
|
||||
42
fresh.gen.ts
42
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,
|
||||
|
||||
39
routes/api/tag/[id].ts
Normal file
39
routes/api/tag/[id].ts
Normal file
@@ -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);
|
||||
},
|
||||
};
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user