add: route for getting videos
This commit is contained in:
parent
b4a0320e3e
commit
0a6ecc6314
13
packages/backend/db/latestSnapshots.ts
Normal file
13
packages/backend/db/latestSnapshots.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { sql } from "@core/db/dbNew";
|
||||||
|
import type { LatestSnapshotType } from "@core/db/schema.d.ts";
|
||||||
|
|
||||||
|
export async function getVideosInViewsRange(minViews: number, maxViews: number) {
|
||||||
|
return sql<LatestSnapshotType[]>`
|
||||||
|
SELECT *
|
||||||
|
FROM latest_video_snapshot
|
||||||
|
WHERE views >= ${minViews}
|
||||||
|
AND views <= ${maxViews}
|
||||||
|
ORDER BY views DESC
|
||||||
|
LIMIT 5000
|
||||||
|
`;
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
import { Context, Hono } from "hono";
|
import { Hono } from "hono";
|
||||||
import { Variables } from "hono/types";
|
import { Variables } from "hono/types";
|
||||||
import { bodyLimitForPing } from "./bodyLimits.ts";
|
import { bodyLimitForPing } from "./bodyLimits.ts";
|
||||||
import { pingHandler } from "routes/ping";
|
import { pingHandler } from "routes/ping";
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@cvsa/backend",
|
"name": "@cvsa/backend",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "0.5.3",
|
"version": "0.6.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"format": "prettier --write .",
|
"format": "prettier --write .",
|
||||||
"dev": "NODE_ENV=development bun run --hot src/main.ts",
|
"dev": "NODE_ENV=development bun run --hot src/main.ts",
|
||||||
|
65
packages/backend/routes/videos/GET.ts
Normal file
65
packages/backend/routes/videos/GET.ts
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
import type { Context } from "hono";
|
||||||
|
import { createHandlers } from "src/utils.ts";
|
||||||
|
import type { BlankEnv, BlankInput } from "hono/types";
|
||||||
|
import { number, object, ValidationError } from "yup";
|
||||||
|
import { ErrorResponse } from "src/schema";
|
||||||
|
import { startTime, endTime } from "hono/timing";
|
||||||
|
import { getVideosInViewsRange } from "@/db/latestSnapshots";
|
||||||
|
|
||||||
|
const SnapshotQueryParamsSchema = object({
|
||||||
|
min_views: number().integer().optional().positive(),
|
||||||
|
max_views: number().integer().optional().positive()
|
||||||
|
});
|
||||||
|
|
||||||
|
type ContextType = Context<BlankEnv, "/videos", BlankInput>;
|
||||||
|
|
||||||
|
export const getVideosHanlder = createHandlers(async (c: ContextType) => {
|
||||||
|
startTime(c, "parse", "Parse the request");
|
||||||
|
try {
|
||||||
|
const queryParams = await SnapshotQueryParamsSchema.validate(c.req.query());
|
||||||
|
const { min_views, max_views } = queryParams;
|
||||||
|
|
||||||
|
if (!min_views && !max_views) {
|
||||||
|
const response: ErrorResponse<string> = {
|
||||||
|
code: "INVALID_QUERY_PARAMS",
|
||||||
|
message: "Invalid query parameters",
|
||||||
|
errors: ["Must provide one of these query parameters: min_views, max_views"]
|
||||||
|
};
|
||||||
|
return c.json<ErrorResponse<string>>(response, 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
endTime(c, "parse");
|
||||||
|
|
||||||
|
startTime(c, "db", "Query the database");
|
||||||
|
|
||||||
|
const minViews = min_views ? min_views : 0;
|
||||||
|
const maxViews = max_views ? max_views : 2147483647;
|
||||||
|
|
||||||
|
const result = await getVideosInViewsRange(minViews, maxViews);
|
||||||
|
|
||||||
|
endTime(c, "db");
|
||||||
|
|
||||||
|
const rows = result.map((row) => ({
|
||||||
|
...row,
|
||||||
|
aid: Number(row.aid)
|
||||||
|
}));
|
||||||
|
|
||||||
|
return c.json(rows);
|
||||||
|
} catch (e: unknown) {
|
||||||
|
if (e instanceof ValidationError) {
|
||||||
|
const response: ErrorResponse<string> = {
|
||||||
|
code: "INVALID_QUERY_PARAMS",
|
||||||
|
message: "Invalid query parameters",
|
||||||
|
errors: e.errors
|
||||||
|
};
|
||||||
|
return c.json<ErrorResponse<string>>(response, 400);
|
||||||
|
} else {
|
||||||
|
const response: ErrorResponse<unknown> = {
|
||||||
|
code: "UNKNOWN_ERROR",
|
||||||
|
message: "Unhandled error",
|
||||||
|
errors: [e]
|
||||||
|
};
|
||||||
|
return c.json<ErrorResponse<unknown>>(response, 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
1
packages/backend/routes/videos/index.ts
Normal file
1
packages/backend/routes/videos/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from "./GET.ts";
|
@ -15,4 +15,4 @@ configureRoutes(app);
|
|||||||
|
|
||||||
await startServer(app);
|
await startServer(app);
|
||||||
|
|
||||||
export const VERSION = "0.5.3";
|
export const VERSION = "0.6.0";
|
||||||
|
@ -6,11 +6,14 @@ import { Hono } from "hono";
|
|||||||
import { Variables } from "hono/types";
|
import { Variables } from "hono/types";
|
||||||
import { createCaptchaSessionHandler, verifyChallengeHandler } from "routes/captcha";
|
import { createCaptchaSessionHandler, verifyChallengeHandler } from "routes/captcha";
|
||||||
import { getCaptchaDifficultyHandler } from "routes/captcha/difficulty/GET.ts";
|
import { getCaptchaDifficultyHandler } from "routes/captcha/difficulty/GET.ts";
|
||||||
|
import { getVideosHanlder } from "@/routes/videos";
|
||||||
|
|
||||||
export function configureRoutes(app: Hono<{ Variables: Variables }>) {
|
export function configureRoutes(app: Hono<{ Variables: Variables }>) {
|
||||||
app.get("/", ...rootHandler);
|
app.get("/", ...rootHandler);
|
||||||
app.all("/ping", ...pingHandler);
|
app.all("/ping", ...pingHandler);
|
||||||
|
|
||||||
|
app.get("/videos", ...getVideosHanlder);
|
||||||
|
|
||||||
app.get("/video/:id/snapshots", ...getSnapshotsHanlder);
|
app.get("/video/:id/snapshots", ...getSnapshotsHanlder);
|
||||||
app.get("/video/:id/info", ...videoInfoHandler);
|
app.get("/video/:id/info", ...videoInfoHandler);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user