diff --git a/api/README.md b/api/README.md index 6faa62eb..734c44d9 100644 --- a/api/README.md +++ b/api/README.md @@ -116,13 +116,14 @@ erDiagram history { int id PK guid entry_id FK - guid user_id FK - uint time "in seconds, null of finished" - uint progress "NN, from 0 to 100" + guid profile_id FK + guid video_id FK + jsonb progress "{ percent, time }" datetime played_date } entries ||--|{ history : part_of users ||--|{ history : has + videos o|--o{ history : has roles { guid show_id PK, FK @@ -143,6 +144,7 @@ erDiagram jsonb external_id } staff ||--|{ roles : has + shows ||--|{ roles : has studios { guid id PK diff --git a/api/src/db/schema/history.ts b/api/src/db/schema/history.ts new file mode 100644 index 00000000..63fcddde --- /dev/null +++ b/api/src/db/schema/history.ts @@ -0,0 +1,25 @@ +import { index, integer, jsonb, timestamp } from "drizzle-orm/pg-core"; +import type { Progress } from "~/models/watchlist"; +import { entries } from "./entries"; +import { profiles } from "./profiles"; +import { schema } from "./utils"; +import { videos } from "./videos"; + +export const history = schema.table( + "history", + { + pk: integer().primaryKey().generatedAlwaysAsIdentity(), + profilePk: integer() + .notNull() + .references(() => profiles.pk, { onDelete: "cascade" }), + entryPk: integer() + .notNull() + .references(() => entries.pk, { onDelete: "cascade" }), + videoPk: integer() + .notNull() + .references(() => videos.pk, { onDelete: "set null" }), + progress: jsonb().$type(), + playedDate: timestamp({ mode: "string" }).notNull().defaultNow(), + }, + (t) => [index("history_play_date").on(t.playedDate.desc())], +); diff --git a/api/src/db/schema/index.ts b/api/src/db/schema/index.ts index 67f4e990..f1e91a59 100644 --- a/api/src/db/schema/index.ts +++ b/api/src/db/schema/index.ts @@ -4,4 +4,6 @@ export * from "./shows"; export * from "./studios"; export * from "./staff"; export * from "./videos"; +export * from "./profiles"; +export * from "./history"; export * from "./mqueue"; diff --git a/api/src/db/schema/profiles.ts b/api/src/db/schema/profiles.ts new file mode 100644 index 00000000..2296010a --- /dev/null +++ b/api/src/db/schema/profiles.ts @@ -0,0 +1,6 @@ +import { integer } from "drizzle-orm/pg-core"; +import { schema } from "./utils"; + +export const profiles = schema.table("profiles", { + pk: integer().primaryKey().generatedAlwaysAsIdentity(), +}); diff --git a/api/src/models/watchlist.ts b/api/src/models/watchlist.ts new file mode 100644 index 00000000..5cc8529a --- /dev/null +++ b/api/src/models/watchlist.ts @@ -0,0 +1,10 @@ +import { t } from "elysia"; + +export const Progress = t.Object({ + percent: t.Integer({ minimum: 0, maximum: 100 }), + time: t.Number({ + minimum: 0, + description: "When this episode was stopped (in seconds since the start", + }), +}); +export type Progress = typeof Progress.static;