-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Duplicate deck * Deck category
- Loading branch information
Showing
18 changed files
with
270 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { EnvType } from "../../env/env-schema.ts"; | ||
import { getDatabase } from "../get-database.ts"; | ||
import { DatabaseException } from "../database-exception.ts"; | ||
import { z } from "zod"; | ||
|
||
const categorySchema = z.object({ | ||
id: z.string(), | ||
name: z.string(), | ||
logo: z.string().nullable(), | ||
}); | ||
|
||
export const getAllCategoriesDb = async (env: EnvType) => { | ||
const db = getDatabase(env); | ||
|
||
const { data: categories, error: categoriesError } = await db | ||
.from("deck_category") | ||
.select("*") | ||
.limit(100); | ||
|
||
if (categoriesError) { | ||
throw new DatabaseException(categoriesError); | ||
} | ||
|
||
return z.array(categorySchema).parse(categories); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { createJsonResponse } from "./lib/json-response/create-json-response.ts"; | ||
import { getUser } from "./services/get-user.ts"; | ||
import { createAuthFailedResponse } from "./lib/json-response/create-auth-failed-response.ts"; | ||
import { handleError } from "./lib/handle-error/handle-error.ts"; | ||
import { envSchema } from "./env/env-schema.ts"; | ||
import { getDeckWithCardsById } from "./db/deck/get-deck-with-cards-by-id-db.ts"; | ||
import { createBadRequestResponse } from "./lib/json-response/create-bad-request-response.ts"; | ||
import { shortUniqueId } from "./lib/short-unique-id/short-unique-id.ts"; | ||
import { getDatabase } from "./db/get-database.ts"; | ||
import { DatabaseException } from "./db/database-exception.ts"; | ||
import { addDeckToMineDb } from "./db/deck/add-deck-to-mine-db.ts"; | ||
|
||
export type CopyDeckResponse = null; | ||
|
||
export const onRequestPost = handleError(async ({ request, env }) => { | ||
const user = await getUser(request, env); | ||
if (!user || !user.is_admin) { | ||
return createAuthFailedResponse(); | ||
} | ||
const envSafe = envSchema.parse(env); | ||
|
||
const url = new URL(request.url); | ||
const deckId = url.searchParams.get("deck_id"); | ||
if (!deckId) { | ||
return createBadRequestResponse(); | ||
} | ||
|
||
const db = getDatabase(envSafe); | ||
const deck = await getDeckWithCardsById(envSafe, parseInt(deckId)); | ||
|
||
// prettier-ignore | ||
const insertData = { | ||
author_id: user.id, | ||
name: `${deck.name} (copy)`, | ||
description: deck.description, | ||
share_id: shortUniqueId(), | ||
is_public: false, | ||
speak_field: deck.speak_field, | ||
speak_locale: deck.speak_locale, | ||
}; | ||
|
||
const insertDeckResult = await db | ||
.from("deck") | ||
.insert(insertData) | ||
.select() | ||
.single(); | ||
|
||
if (insertDeckResult.error) { | ||
throw new DatabaseException(insertDeckResult.error); | ||
} | ||
|
||
const createCardsResult = await db.from("deck_card").insert( | ||
deck.deck_card.map((card) => ({ | ||
deck_id: insertDeckResult.data.id, | ||
example: card.example, | ||
front: card.front, | ||
back: card.back, | ||
})), | ||
); | ||
|
||
await addDeckToMineDb(envSafe, { | ||
user_id: user.id, | ||
deck_id: insertDeckResult.data.id, | ||
}); | ||
|
||
if (createCardsResult.error) { | ||
throw new DatabaseException(createCardsResult.error); | ||
} | ||
|
||
return createJsonResponse<CopyDeckResponse>(null); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.