Allow filter & sort by watchStatus

This commit is contained in:
Zoe Roux 2025-04-06 23:25:53 +02:00
parent b4e8fbdd26
commit aa9476680c
No known key found for this signature in database
2 changed files with 23 additions and 34 deletions

View File

@ -1,20 +1,9 @@
import { import { type SQL, and, eq, exists, ne, sql } from "drizzle-orm";
type SQL,
type Subquery,
and,
desc,
eq,
exists,
ne,
sql,
} from "drizzle-orm";
import type { PgSelect } from "drizzle-orm/pg-core";
import { db } from "~/db"; import { db } from "~/db";
import { import {
entries, entries,
entryTranslations, entryTranslations,
entryVideoJoin, entryVideoJoin,
history,
profiles, profiles,
showStudioJoin, showStudioJoin,
showTranslations, showTranslations,
@ -46,8 +35,19 @@ import {
sortToSql, sortToSql,
} from "~/models/utils"; } from "~/models/utils";
import type { EmbeddedVideo } from "~/models/video"; import type { EmbeddedVideo } from "~/models/video";
import { WatchlistStatus } from "~/models/watchlist";
import { entryVideosQ, getEntryProgressQ, mapProgress } from "../entries"; import { entryVideosQ, getEntryProgressQ, mapProgress } from "../entries";
export const watchStatusQ = db
.select({
...getColumns(watchlist),
percent: sql`${watchlist.seenCount}`.as("percent"),
})
.from(watchlist)
.leftJoin(profiles, eq(watchlist.profilePk, profiles.pk))
.where(eq(profiles.id, sql.placeholder("userId")))
.as("watchstatus");
export const showFilters: FilterDef = { export const showFilters: FilterDef = {
genres: { genres: {
column: shows.genres, column: shows.genres,
@ -70,6 +70,11 @@ export const showFilters: FilterDef = {
type: "string", type: "string",
isArray: true, isArray: true,
}, },
watchStatus: {
column: watchStatusQ.status,
type: "enum",
values: WatchlistStatus.enum,
},
}; };
export const showSort = Sort( export const showSort = Sort(
{ {
@ -80,6 +85,7 @@ export const showSort = Sort(
endAir: shows.endAir, endAir: shows.endAir,
createdAt: shows.createdAt, createdAt: shows.createdAt,
nextRefresh: shows.nextRefresh, nextRefresh: shows.nextRefresh,
watchStatus: watchStatusQ.status,
}, },
{ {
default: ["slug"], default: ["slug"],
@ -196,11 +202,9 @@ const showRelations = {
nextEntry: ({ nextEntry: ({
languages, languages,
userId, userId,
watchStatusQ,
}: { }: {
languages: string[]; languages: string[];
userId: string; userId: string;
watchStatusQ: Subquery;
}) => { }) => {
const transQ = db const transQ = db
.selectDistinctOn([entryTranslations.pk]) .selectDistinctOn([entryTranslations.pk])
@ -228,9 +232,7 @@ const showRelations = {
.innerJoin(transQ, eq(entries.pk, transQ.pk)) .innerJoin(transQ, eq(entries.pk, transQ.pk))
.leftJoin(progressQ, eq(entries.pk, progressQ.entryPk)) .leftJoin(progressQ, eq(entries.pk, progressQ.entryPk))
.leftJoinLateral(entryVideosQ, sql`true`) .leftJoinLateral(entryVideosQ, sql`true`)
.where( .where(eq(watchStatusQ.nextEntry, entries.pk))
eq((watchStatusQ as unknown as typeof watchlist).nextEntry, entries.pk),
)
.as("nextEntry"); .as("nextEntry");
}, },
}; };
@ -272,16 +274,6 @@ export async function getShows({
) )
.as("t"); .as("t");
const watchStatusQ = db
.select({
...getColumns(watchlist),
percent: sql`${watchlist.seenCount}`.as("percent"),
})
.from(watchlist)
.leftJoin(profiles, eq(watchlist.profilePk, profiles.pk))
.where(eq(profiles.id, userId))
.as("watchstatus");
return await db return await db
.select({ .select({
...getColumns(shows), ...getColumns(shows),
@ -302,11 +294,7 @@ export async function getShows({
watchStatus: getColumns(watchStatusQ), watchStatus: getColumns(watchStatusQ),
...buildRelations(relations, showRelations, { ...buildRelations(relations, showRelations, { languages, userId }),
languages,
userId,
watchStatusQ,
}),
}) })
.from(shows) .from(shows)
.leftJoin(watchStatusQ, eq(shows.pk, watchStatusQ.showPk)) .leftJoin(watchStatusQ, eq(shows.pk, watchStatusQ.showPk))
@ -327,5 +315,6 @@ export async function getShows({
: sortToSql(sort)), : sortToSql(sort)),
shows.pk, shows.pk,
) )
.limit(limit); .limit(limit)
.execute({ userId });
} }

View File

@ -6,9 +6,9 @@ import { shows } from "./shows";
import { schema } from "./utils"; import { schema } from "./utils";
export const watchlistStatus = schema.enum("watchlist_status", [ export const watchlistStatus = schema.enum("watchlist_status", [
"completed",
"watching", "watching",
"rewatching", "rewatching",
"completed",
"dropped", "dropped",
"planned", "planned",
]); ]);