mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-11-02 02:27:17 -05:00
Add progress to /videos/:id
This commit is contained in:
parent
c33ba01e54
commit
e3ae961b68
@ -54,7 +54,7 @@ export const entryProgressQ = db
|
|||||||
})
|
})
|
||||||
.from(history)
|
.from(history)
|
||||||
.leftJoin(videos, eq(history.videoPk, videos.pk))
|
.leftJoin(videos, eq(history.videoPk, videos.pk))
|
||||||
.leftJoin(profiles, eq(history.profilePk, profiles.pk))
|
.innerJoin(profiles, eq(history.profilePk, profiles.pk))
|
||||||
.where(eq(profiles.id, sql.placeholder("userId")))
|
.where(eq(profiles.id, sql.placeholder("userId")))
|
||||||
.orderBy(history.entryPk, desc(history.playedDate))
|
.orderBy(history.entryPk, desc(history.playedDate))
|
||||||
.as("progress");
|
.as("progress");
|
||||||
|
|||||||
@ -18,6 +18,7 @@ import { db, type Transaction } from "~/db";
|
|||||||
import {
|
import {
|
||||||
entries,
|
entries,
|
||||||
entryVideoJoin,
|
entryVideoJoin,
|
||||||
|
history,
|
||||||
profiles,
|
profiles,
|
||||||
shows,
|
shows,
|
||||||
showTranslations,
|
showTranslations,
|
||||||
@ -38,8 +39,9 @@ import {
|
|||||||
import { Entry } from "~/models/entry";
|
import { Entry } from "~/models/entry";
|
||||||
import { KError } from "~/models/error";
|
import { KError } from "~/models/error";
|
||||||
import { bubbleVideo } from "~/models/examples";
|
import { bubbleVideo } from "~/models/examples";
|
||||||
|
import { Progress } from "~/models/history";
|
||||||
import { Movie, type MovieStatus } from "~/models/movie";
|
import { Movie, type MovieStatus } from "~/models/movie";
|
||||||
import { Serie, type Serie } from "~/models/serie";
|
import { Serie } from "~/models/serie";
|
||||||
import {
|
import {
|
||||||
AcceptLanguage,
|
AcceptLanguage,
|
||||||
buildRelations,
|
buildRelations,
|
||||||
@ -228,6 +230,35 @@ const videoRelations = {
|
|||||||
.where(eq(entryVideoJoin.videoPk, videos.pk))
|
.where(eq(entryVideoJoin.videoPk, videos.pk))
|
||||||
.as("slugs");
|
.as("slugs");
|
||||||
},
|
},
|
||||||
|
progress: () => {
|
||||||
|
const query = db
|
||||||
|
.select({
|
||||||
|
json: jsonbBuildObject<Progress>({
|
||||||
|
percent: history.percent,
|
||||||
|
time: history.time,
|
||||||
|
playedDate: sql`to_char(${history.playedDate}, 'YYYY-MM-DD"T"HH24:MI:SS"Z"')`,
|
||||||
|
videoId: videos.id,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
.from(history)
|
||||||
|
.innerJoin(profiles, eq(history.profilePk, profiles.pk))
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(profiles.id, sql.placeholder("userId")),
|
||||||
|
eq(history.videoPk, videos.pk),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.orderBy(desc(history.playedDate))
|
||||||
|
.limit(1);
|
||||||
|
return sql`
|
||||||
|
(
|
||||||
|
select coalesce(
|
||||||
|
${query},
|
||||||
|
'{"percent": 0, "time": 0, "playedDate": null, "videoId": null}'::jsonb
|
||||||
|
)
|
||||||
|
as "progress"
|
||||||
|
)`;
|
||||||
|
},
|
||||||
entries: ({ languages }: { languages: string[] }) => {
|
entries: ({ languages }: { languages: string[] }) => {
|
||||||
const transQ = getEntryTransQ(languages);
|
const transQ = getEntryTransQ(languages);
|
||||||
|
|
||||||
@ -431,7 +462,7 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] })
|
|||||||
.select({
|
.select({
|
||||||
...getColumns(videos),
|
...getColumns(videos),
|
||||||
...buildRelations(
|
...buildRelations(
|
||||||
["slugs", "entries", ...relations],
|
["slugs", "progress", "entries", ...relations],
|
||||||
videoRelations,
|
videoRelations,
|
||||||
{
|
{
|
||||||
languages,
|
languages,
|
||||||
@ -486,6 +517,7 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] })
|
|||||||
slugs: t.Array(
|
slugs: t.Array(
|
||||||
t.String({ format: "slug", examples: ["made-in-abyss-s1e13"] }),
|
t.String({ format: "slug", examples: ["made-in-abyss-s1e13"] }),
|
||||||
),
|
),
|
||||||
|
progress: Progress,
|
||||||
entries: t.Array(Entry),
|
entries: t.Array(Entry),
|
||||||
previous: t.Optional(
|
previous: t.Optional(
|
||||||
t.Nullable(
|
t.Nullable(
|
||||||
|
|||||||
@ -107,7 +107,7 @@ export function values<K extends string>(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const coalesce = <T>(val: SQL<T> | Column, def: SQL<T> | Column) => {
|
export const coalesce = <T>(val: SQL<T> | SQLWrapper, def: SQL<T> | Column) => {
|
||||||
return sql<T>`coalesce(${val}, ${def})`;
|
return sql<T>`coalesce(${val}, ${def})`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user