mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-02 05:04:15 -04:00
wip
This commit is contained in:
parent
eaec881594
commit
1c0fdf6f89
@ -26,6 +26,8 @@ import {
|
|||||||
isUuid,
|
isUuid,
|
||||||
keysetPaginate,
|
keysetPaginate,
|
||||||
processLanguages,
|
processLanguages,
|
||||||
|
createPage,
|
||||||
|
sortToSql,
|
||||||
} from "~/models/utils";
|
} from "~/models/utils";
|
||||||
import { comment } from "~/utils";
|
import { comment } from "~/utils";
|
||||||
import { db } from "../db";
|
import { db } from "../db";
|
||||||
@ -282,6 +284,8 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
|
|||||||
)
|
)
|
||||||
.as("video");
|
.as("video");
|
||||||
|
|
||||||
|
console.log(sort.isDefault)
|
||||||
|
|
||||||
const items = await db
|
const items = await db
|
||||||
.select({
|
.select({
|
||||||
...moviesCol,
|
...moviesCol,
|
||||||
@ -316,15 +320,9 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
.orderBy(
|
.orderBy(
|
||||||
...(query
|
...(query && sort.isDefault
|
||||||
? [sql`word_similarity(${query}::text, ${showTranslations.name})`]
|
? [sql`word_similarity(${query}::text, ${showTranslations.name})`]
|
||||||
: []),
|
: sortToSql(sort, shows)),
|
||||||
...(sort.random
|
|
||||||
? [sql`md5(${sort.random.seed} || ${shows.pk})`]
|
|
||||||
: []),
|
|
||||||
...sort.sort.map((x) =>
|
|
||||||
x.desc ? sql`${shows[x.key]} desc nulls last` : shows[x.key],
|
|
||||||
),
|
|
||||||
shows.pk,
|
shows.pk,
|
||||||
)
|
)
|
||||||
.limit(limit);
|
.limit(limit);
|
||||||
@ -337,7 +335,6 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
|
|||||||
sort: Sort(["slug", "rating", "airDate", "createdAt", "nextRefresh"], {
|
sort: Sort(["slug", "rating", "airDate", "createdAt", "nextRefresh"], {
|
||||||
remap: { airDate: "startAir" },
|
remap: { airDate: "startAir" },
|
||||||
default: ["slug"],
|
default: ["slug"],
|
||||||
description: "How to sort the query",
|
|
||||||
}),
|
}),
|
||||||
filter: t.Optional(Filter({ def: movieFilters })),
|
filter: t.Optional(Filter({ def: movieFilters })),
|
||||||
query: t.Optional(
|
query: t.Optional(
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import { sql } from "drizzle-orm";
|
||||||
|
import type { PgColumn } from "drizzle-orm/pg-core";
|
||||||
import { t } from "elysia";
|
import { t } from "elysia";
|
||||||
|
|
||||||
export type Sort<
|
export type Sort<
|
||||||
@ -10,6 +12,7 @@ export type Sort<
|
|||||||
desc: boolean;
|
desc: boolean;
|
||||||
}[];
|
}[];
|
||||||
random?: { seed: number };
|
random?: { seed: number };
|
||||||
|
isDefault?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type NonEmptyArray<T> = [T, ...T[]];
|
export type NonEmptyArray<T> = [T, ...T[]];
|
||||||
@ -25,7 +28,7 @@ export const Sort = <
|
|||||||
remap,
|
remap,
|
||||||
}: {
|
}: {
|
||||||
default?: T[number][];
|
default?: T[number][];
|
||||||
description: string;
|
description?: string;
|
||||||
remap: Remap;
|
remap: Remap;
|
||||||
},
|
},
|
||||||
) =>
|
) =>
|
||||||
@ -49,6 +52,7 @@ export const Sort = <
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
.Decode((sort): Sort<T, Remap> => {
|
.Decode((sort): Sort<T, Remap> => {
|
||||||
|
console.log(sort);
|
||||||
const random = sort.find((x) => x.startsWith("random"));
|
const random = sort.find((x) => x.startsWith("random"));
|
||||||
if (random) {
|
if (random) {
|
||||||
const seed = random.includes(":")
|
const seed = random.includes(":")
|
||||||
@ -63,8 +67,26 @@ export const Sort = <
|
|||||||
if (key in remap) return { key: remap[key]!, remmapedKey: key, desc };
|
if (key in remap) return { key: remap[key]!, remmapedKey: key, desc };
|
||||||
return { key: key as Exclude<typeof key, keyof Remap>, desc };
|
return { key: key as Exclude<typeof key, keyof Remap>, desc };
|
||||||
}),
|
}),
|
||||||
|
isDefault: "isDefault" in sort,
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.Encode(() => {
|
.Encode(() => {
|
||||||
throw new Error("Encode not supported for sort");
|
throw new Error("Encode not supported for sort");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
type Table<Name extends string> = Record<Name, PgColumn>;
|
||||||
|
|
||||||
|
export const sortToSql = <
|
||||||
|
T extends string[],
|
||||||
|
Remap extends Partial<Record<T[number], string>>,
|
||||||
|
>(
|
||||||
|
sort: Sort<T, Remap>,
|
||||||
|
table: Table<Sort<T, Remap>["sort"][number]["key"] | "pk">,
|
||||||
|
) => {
|
||||||
|
if (sort.random) {
|
||||||
|
return [sql`md5(${sort.random.seed} || ${table.pk})`];
|
||||||
|
}
|
||||||
|
return sort.sort.map((x) =>
|
||||||
|
x.desc ? sql`${table[x.key]} desc nulls last` : table[x.key],
|
||||||
|
);
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user