mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-02 05:04:15 -04:00
Add watchstatus create/edit apis
This commit is contained in:
parent
3f5af4b7fa
commit
83d8462003
@ -1,11 +1,16 @@
|
|||||||
import { and, isNotNull, isNull } from "drizzle-orm";
|
import { type SQL, and, eq, isNotNull, isNull, sql } from "drizzle-orm";
|
||||||
import Elysia, { t } from "elysia";
|
import Elysia, { t } from "elysia";
|
||||||
import { auth, getUserInfo } from "~/auth";
|
import { auth, getUserInfo } from "~/auth";
|
||||||
import { shows } from "~/db/schema";
|
import { db } from "~/db";
|
||||||
|
import { profiles, shows } from "~/db/schema";
|
||||||
|
import { watchlist } from "~/db/schema/watchlist";
|
||||||
|
import { conflictUpdateAllExcept } from "~/db/utils";
|
||||||
import { KError } from "~/models/error";
|
import { KError } from "~/models/error";
|
||||||
|
import { bubble, madeInAbyss } from "~/models/examples";
|
||||||
import { Show } from "~/models/show";
|
import { Show } from "~/models/show";
|
||||||
import {
|
import {
|
||||||
AcceptLanguage,
|
AcceptLanguage,
|
||||||
|
DbMetadata,
|
||||||
Filter,
|
Filter,
|
||||||
Page,
|
Page,
|
||||||
createPage,
|
createPage,
|
||||||
@ -13,11 +18,53 @@ import {
|
|||||||
processLanguages,
|
processLanguages,
|
||||||
} from "~/models/utils";
|
} from "~/models/utils";
|
||||||
import { desc } from "~/models/utils/descriptions";
|
import { desc } from "~/models/utils/descriptions";
|
||||||
|
import { WatchStatus } from "~/models/watchlist";
|
||||||
import { getShows, showFilters, showSort, watchStatusQ } from "./shows/logic";
|
import { getShows, showFilters, showSort, watchStatusQ } from "./shows/logic";
|
||||||
|
|
||||||
|
async function setWatchStatus({
|
||||||
|
showFilter,
|
||||||
|
status,
|
||||||
|
userId,
|
||||||
|
}: {
|
||||||
|
showFilter?: SQL;
|
||||||
|
status: Omit<WatchStatus, "percent">;
|
||||||
|
userId: string;
|
||||||
|
}) {
|
||||||
|
const profileQ = db
|
||||||
|
.select({ pk: profiles.pk })
|
||||||
|
.from(profiles)
|
||||||
|
.where(eq(profiles.id, userId))
|
||||||
|
.as("profileQ");
|
||||||
|
const showQ = db
|
||||||
|
.select({ pk: shows.pk })
|
||||||
|
.from(shows)
|
||||||
|
.where(showFilter)
|
||||||
|
.as("showQ");
|
||||||
|
|
||||||
|
return await db
|
||||||
|
.insert(watchlist)
|
||||||
|
.values({
|
||||||
|
...status,
|
||||||
|
profilePk: sql`${profileQ}`,
|
||||||
|
showPk: sql`${showQ}`,
|
||||||
|
})
|
||||||
|
.onConflictDoUpdate({
|
||||||
|
target: [watchlist.profilePk, watchlist.showPk],
|
||||||
|
set: {
|
||||||
|
...conflictUpdateAllExcept(watchlist, [
|
||||||
|
"profilePk",
|
||||||
|
"showPk",
|
||||||
|
"createdAt",
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.returning();
|
||||||
|
}
|
||||||
|
|
||||||
export const watchlistH = new Elysia({ tags: ["profiles"] })
|
export const watchlistH = new Elysia({ tags: ["profiles"] })
|
||||||
.use(auth)
|
.use(auth)
|
||||||
.guard({
|
.guard(
|
||||||
|
{
|
||||||
query: t.Object({
|
query: t.Object({
|
||||||
sort: showSort,
|
sort: showSort,
|
||||||
filter: t.Optional(Filter({ def: showFilters })),
|
filter: t.Optional(Filter({ def: showFilters })),
|
||||||
@ -39,7 +86,9 @@ export const watchlistH = new Elysia({ tags: ["profiles"] })
|
|||||||
200: Page(Show),
|
200: Page(Show),
|
||||||
422: KError,
|
422: KError,
|
||||||
},
|
},
|
||||||
})
|
},
|
||||||
|
(app) =>
|
||||||
|
app
|
||||||
.get(
|
.get(
|
||||||
"/profiles/me/watchlist",
|
"/profiles/me/watchlist",
|
||||||
async ({
|
async ({
|
||||||
@ -106,7 +155,9 @@ export const watchlistH = new Elysia({ tags: ["profiles"] })
|
|||||||
return createPage(items, { url, sort, limit });
|
return createPage(items, { url, sort, limit });
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
detail: { description: "Get all movies/series in someone's watchlist" },
|
detail: {
|
||||||
|
description: "Get all movies/series in someone's watchlist",
|
||||||
|
},
|
||||||
params: t.Object({
|
params: t.Object({
|
||||||
id: t.String({
|
id: t.String({
|
||||||
description:
|
description:
|
||||||
@ -120,4 +171,63 @@ export const watchlistH = new Elysia({ tags: ["profiles"] })
|
|||||||
}),
|
}),
|
||||||
permissions: ["users.read"],
|
permissions: ["users.read"],
|
||||||
},
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.post(
|
||||||
|
"/series/:id/watchstatus",
|
||||||
|
async ({ params: { id }, body, jwt: { sub } }) => {
|
||||||
|
return await setWatchStatus({
|
||||||
|
showFilter: and(
|
||||||
|
eq(shows.kind, "serie"),
|
||||||
|
isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id),
|
||||||
|
),
|
||||||
|
userId: sub,
|
||||||
|
status: body,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{
|
||||||
|
detail: { description: "Set watchstatus of a series." },
|
||||||
|
params: t.Object({
|
||||||
|
id: t.String({
|
||||||
|
description: "The id or slug of the serie.",
|
||||||
|
example: madeInAbyss.slug,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
body: t.Omit(WatchStatus, ["percent"]),
|
||||||
|
response: {
|
||||||
|
201: t.Union([t.Omit(WatchStatus, ["percent"]), DbMetadata]),
|
||||||
|
},
|
||||||
|
permissions: ["core.read"],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.post(
|
||||||
|
"/movies/:id/watchstatus",
|
||||||
|
async ({ params: { id }, body, jwt: { sub } }) => {
|
||||||
|
return await setWatchStatus({
|
||||||
|
showFilter: and(
|
||||||
|
eq(shows.kind, "movie"),
|
||||||
|
isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id),
|
||||||
|
),
|
||||||
|
userId: sub,
|
||||||
|
// for movies, watch-percent is stored in `seenCount`.
|
||||||
|
status: { ...body, seenCount: body.status === "completed" ? 100 : 0 },
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{
|
||||||
|
detail: { description: "Set watchstatus of a movie." },
|
||||||
|
params: t.Object({
|
||||||
|
id: t.String({
|
||||||
|
description: "The id or slug of the movie.",
|
||||||
|
example: bubble.slug,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
body: t.Omit(WatchStatus, ["seenCount", "percent"]),
|
||||||
|
response: {
|
||||||
|
201: t.Union([
|
||||||
|
t.Omit(WatchStatus, ["seenCount", "percent"]),
|
||||||
|
DbMetadata,
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
permissions: ["core.read"],
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user