mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -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(
|
||||
"random",
|
||||
async ({ error, redirect }) => {
|
||||
const [id] = await db
|
||||
const [movie] = await db
|
||||
.select({ id: shows.id })
|
||||
.from(shows)
|
||||
.where(eq(shows.kind, "movie"))
|
||||
.orderBy(sql`random()`)
|
||||
.limit(1);
|
||||
if (!id)
|
||||
if (!movie)
|
||||
return error(404, {
|
||||
status: 404,
|
||||
message: "No movies in the database",
|
||||
});
|
||||
return redirect(`/movies/${id}`);
|
||||
return redirect(`/movies/${movie.id}`);
|
||||
},
|
||||
{
|
||||
detail: {
|
||||
@ -175,7 +175,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] })
|
||||
response: {
|
||||
302: t.Void({
|
||||
description:
|
||||
"Redirected to the [/movies/id](#tag/movies/GET/movies/{id}) route.",
|
||||
"Redirected to the [/movies/{id}](#tag/movies/GET/movies/{id}) route.",
|
||||
}),
|
||||
404: {
|
||||
...KError,
|
||||
|
@ -6,8 +6,9 @@ import { shows } from "~/db/schema";
|
||||
import { bubble } from "~/models/examples";
|
||||
import { dune1984 } from "~/models/examples/dune-1984";
|
||||
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 { isUuid } from "~/models/utils";
|
||||
|
||||
beforeAll(async () => {
|
||||
await db.delete(shows);
|
||||
@ -186,5 +187,16 @@ describe("Get all movies", () => {
|
||||
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