diff --git a/functions/db/deck/is-user-deck-exists.ts b/functions/db/deck/is-user-deck-exists.ts new file mode 100644 index 00000000..a0d0c24a --- /dev/null +++ b/functions/db/deck/is-user-deck-exists.ts @@ -0,0 +1,18 @@ +import { EnvType } from "../../env/env-schema.ts"; +import { getDatabase } from "../get-database.ts"; +import { DatabaseException } from "../database-exception.ts"; + +export const isUserDeckExists = async ( + envSafe: EnvType, + body: { user_id: number; deck_id: number }, +) => { + const db = getDatabase(envSafe); + + const userDeck = await db.from("user_deck").select().match(body); + + if (userDeck.error) { + throw new DatabaseException(userDeck.error); + } + + return userDeck.data.length ? userDeck.data[0] : null; +}; diff --git a/functions/remove-deck-from-mine.ts b/functions/remove-deck-from-mine.ts index cd0c30d6..10d82cb3 100644 --- a/functions/remove-deck-from-mine.ts +++ b/functions/remove-deck-from-mine.ts @@ -6,8 +6,8 @@ import { envSchema } from "./env/env-schema.ts"; import { createBadRequestResponse } from "./lib/json-response/create-bad-request-response.ts"; import { createJsonResponse } from "./lib/json-response/create-json-response.ts"; import { removeDeckFromMineDb } from "./db/deck/remove-deck-from-mine-db.ts"; -import { getDeckByIdAndAuthorId } from "./db/deck/get-deck-by-id-and-author-id.ts"; import { createForbiddenRequestResponse } from "./lib/json-response/create-forbidden-request-response.ts"; +import { isUserDeckExists } from "./db/deck/is-user-deck-exists.ts"; const requestSchema = z.object({ deckId: z.number(), @@ -26,12 +26,11 @@ export const onRequestPost = handleError(async ({ env, request }) => { const envSafe = envSchema.parse(env); - const canEdit = await getDeckByIdAndAuthorId( - envSafe, - input.data.deckId, - user.id, - ); - if (!canEdit) { + const userDeckExists = await isUserDeckExists(envSafe, { + user_id: user.id, + deck_id: input.data.deckId, + }); + if (!userDeckExists) { return createForbiddenRequestResponse(); }