Add 200/201 handling on post /movies + doc

This commit is contained in:
Zoe Roux 2024-12-02 22:55:58 +01:00
parent 5d24dcafd5
commit 24035c15bf
No known key found for this signature in database
3 changed files with 25 additions and 13 deletions

View File

@ -11,12 +11,24 @@ export const seed = new Elysia()
}) })
.post( .post(
"/movies", "/movies",
async ({ body }) => { async ({ body, error }) => {
return await seedMovie(body); const { status, ...ret } = await seedMovie(body);
return error(status === "created" ? 201 : 200, ret);
}, },
{ {
body: "seed-movie", body: "seed-movie",
response: { 200: "seed-movie-response", 400: "error" }, response: {
tags: ["movies"], 200: {
...SeedMovieResponse,
description: "Existing movie edited/updated.",
},
201: { ...SeedMovieResponse, description: "Created a new movie." },
400: "error",
},
detail: {
tags: ["movies"],
description:
"Create a movie & all related metadata. Can also link videos.",
},
}, },
); );

View File

@ -11,7 +11,6 @@ import {
} from "~/db/schema"; } from "~/db/schema";
import { conflictUpdateAllExcept } from "~/db/schema/utils"; import { conflictUpdateAllExcept } from "~/db/schema/utils";
import type { SeedMovie } from "~/models/movie"; import type { SeedMovie } from "~/models/movie";
import { Resource } from "~/models/utils";
import { processOptImage } from "./images"; import { processOptImage } from "./images";
import { guessNextRefresh } from "./refresh"; import { guessNextRefresh } from "./refresh";
@ -19,17 +18,18 @@ type Show = typeof shows.$inferInsert;
type ShowTrans = typeof showTranslations.$inferInsert; type ShowTrans = typeof showTranslations.$inferInsert;
type Entry = typeof entries.$inferInsert; type Entry = typeof entries.$inferInsert;
export const SeedMovieResponse = t.Intersect([ export const SeedMovieResponse = t.Object({
Resource, id: t.String({ format: "uuid" }),
t.Object({ slug: t.String({ format: "slug", examples: ["bubble"] }),
videos: t.Array(t.Object({ slug: t.String({ format: "slug" }) })), videos: t.Array(
}), t.Object({ slug: t.String({ format: "slug", examples: ["bubble-v2"] }) }),
]); ),
});
export type SeedMovieResponse = typeof SeedMovieResponse.static; export type SeedMovieResponse = typeof SeedMovieResponse.static;
export const seedMovie = async ( export const seedMovie = async (
seed: SeedMovie, seed: SeedMovie,
): Promise<SeedMovieResponse> => { ): Promise<SeedMovieResponse & { status: "created" | "updated" }> => {
const { translations, videos: vids, ...bMovie } = seed; const { translations, videos: vids, ...bMovie } = seed;
const ret = await db.transaction(async (tx) => { const ret = await db.transaction(async (tx) => {
@ -139,6 +139,7 @@ export const seedMovie = async (
} }
return { return {
status: ret.updated ? "updated" : "created",
id: ret.id, id: ret.id,
slug: ret.slug, slug: ret.slug,
videos: retVideos, videos: retVideos,

View File

@ -73,7 +73,6 @@ const app = new Elysia()
}, },
}), }),
) )
.get("/", () => "Hello Elysia")
.model({ image: Image }) .model({ image: Image })
.use(movies) .use(movies)
.use(series) .use(series)