Removes the `Env` parameter from several functions to simplify their signatures and rely on the global `env` for configuration. This change reduces the number of arguments passed around, making the code cleaner and easier to maintain.
104 lines
2.5 KiB
TypeScript
104 lines
2.5 KiB
TypeScript
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|
import { env } from "hono/adapter";
|
|
|
|
import { updateWatchStatus } from "~/controllers/watch-status";
|
|
import {
|
|
AniListIdQuerySchema,
|
|
EpisodeNumberSchema,
|
|
ErrorResponse,
|
|
ErrorResponseSchema,
|
|
SuccessResponseSchema,
|
|
} from "~/types/schema";
|
|
import { User } from "~/types/user";
|
|
|
|
import { markEpisodeAsWatched } from "./anilist";
|
|
|
|
const MarkEpisodeAsWatchedRequest = z.object({
|
|
episodeNumber: EpisodeNumberSchema,
|
|
isComplete: z.boolean(),
|
|
});
|
|
|
|
const route = createRoute({
|
|
tags: ["aniplay", "episodes"],
|
|
summary: "Mark episode as watched",
|
|
operationId: "markEpisodeAsWatched",
|
|
method: "post",
|
|
path: "/{aniListId}/watched",
|
|
request: {
|
|
params: z.object({ aniListId: AniListIdQuerySchema }),
|
|
body: {
|
|
content: {
|
|
"application/json": {
|
|
schema: MarkEpisodeAsWatchedRequest,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
responses: {
|
|
200: {
|
|
content: {
|
|
"application/json": {
|
|
schema: SuccessResponseSchema(User),
|
|
},
|
|
},
|
|
description: "Returns whether the episode was marked as watched",
|
|
},
|
|
401: {
|
|
content: {
|
|
"application/json": {
|
|
schema: ErrorResponseSchema,
|
|
},
|
|
},
|
|
description: "Unauthorized to mark the episode as watched",
|
|
},
|
|
500: {
|
|
content: {
|
|
"application/json": {
|
|
schema: ErrorResponseSchema,
|
|
},
|
|
},
|
|
description: "Error marking episode as watched",
|
|
},
|
|
},
|
|
});
|
|
|
|
const app = new OpenAPIHono<Cloudflare.Env>();
|
|
|
|
app.openapi(route, async (c) => {
|
|
const aniListToken = c.req.header("X-AniList-Token");
|
|
|
|
if (!aniListToken) {
|
|
return c.json(ErrorResponse, { status: 401 });
|
|
}
|
|
|
|
const deviceId = c.req.header("X-Aniplay-Device-Id")!;
|
|
const aniListId = Number(c.req.param("aniListId"));
|
|
const { episodeNumber, isComplete } =
|
|
await c.req.json<typeof MarkEpisodeAsWatchedRequest._type>();
|
|
|
|
try {
|
|
const user = await markEpisodeAsWatched(
|
|
aniListToken,
|
|
aniListId,
|
|
episodeNumber,
|
|
isComplete,
|
|
);
|
|
if (isComplete) {
|
|
await updateWatchStatus(c.req, deviceId, aniListId, "COMPLETED");
|
|
}
|
|
|
|
if (!user) {
|
|
console.error("Failed to mark episode as watched - user not found?");
|
|
return c.json(ErrorResponse, { status: 500 });
|
|
}
|
|
|
|
return c.json({ success: true, result: user }, 200);
|
|
} catch (error) {
|
|
console.error("Failed to mark episode as watched");
|
|
console.error(error);
|
|
return c.json(ErrorResponse, { status: 500 });
|
|
}
|
|
});
|
|
|
|
export default app;
|