mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-03 21:54:49 -04:00
Fix & test /movies/random
This commit is contained in:
parent
b6f996139f
commit
1cdb372079
@ -155,18 +155,18 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
|
|||||||
.get(
|
.get(
|
||||||
"random",
|
"random",
|
||||||
async ({ error, redirect }) => {
|
async ({ error, redirect }) => {
|
||||||
const [id] = await db
|
const [movie] = await db
|
||||||
.select({ id: shows.id })
|
.select({ id: shows.id })
|
||||||
.from(shows)
|
.from(shows)
|
||||||
.where(eq(shows.kind, "movie"))
|
.where(eq(shows.kind, "movie"))
|
||||||
.orderBy(sql`random()`)
|
.orderBy(sql`random()`)
|
||||||
.limit(1);
|
.limit(1);
|
||||||
if (!id)
|
if (!movie)
|
||||||
return error(404, {
|
return error(404, {
|
||||||
status: 404,
|
status: 404,
|
||||||
message: "No movies in the database",
|
message: "No movies in the database",
|
||||||
});
|
});
|
||||||
return redirect(`/movies/${id}`);
|
return redirect(`/movies/${movie.id}`);
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
detail: {
|
detail: {
|
||||||
@ -175,7 +175,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
|
|||||||
response: {
|
response: {
|
||||||
302: t.Void({
|
302: t.Void({
|
||||||
description:
|
description:
|
||||||
"Redirected to the [/movies/id](#tag/movies/GET/movies/{id}) route.",
|
"Redirected to the [/movies/{id}](#tag/movies/GET/movies/{id}) route.",
|
||||||
}),
|
}),
|
||||||
404: {
|
404: {
|
||||||
...KError,
|
...KError,
|
||||||
|
@ -6,8 +6,9 @@ import { shows } from "~/db/schema";
|
|||||||
import { bubble } from "~/models/examples";
|
import { bubble } from "~/models/examples";
|
||||||
import { dune1984 } from "~/models/examples/dune-1984";
|
import { dune1984 } from "~/models/examples/dune-1984";
|
||||||
import { dune } from "~/models/examples/dune-2021";
|
import { dune } from "~/models/examples/dune-2021";
|
||||||
import { getMovies, movieApp } from "./movies-helper";
|
import { getMovie, getMovies, movieApp } from "./movies-helper";
|
||||||
import type { Movie } from "~/models/movie";
|
import type { Movie } from "~/models/movie";
|
||||||
|
import { isUuid } from "~/models/utils";
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
await db.delete(shows);
|
await db.delete(shows);
|
||||||
@ -186,5 +187,16 @@ describe("Get all movies", () => {
|
|||||||
body2.items[0].slug,
|
body2.items[0].slug,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Get /random", async () => {
|
||||||
|
const resp = await movieApp.handle(
|
||||||
|
new Request("http://localhost/movies/random"),
|
||||||
|
);
|
||||||
|
expect(resp.status).toBe(302);
|
||||||
|
const location = resp.headers.get("location")!;
|
||||||
|
expect(location).toStartWith("/movies/");
|
||||||
|
const id = location.substring("/movies/".length);
|
||||||
|
expect(isUuid(id)).toBe(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user