From 7c5fc61104ff232421005597aa26bee72eb97f90 Mon Sep 17 00:00:00 2001 From: Gorbachev Egor <7gorbachevm@gmail.com> Date: Mon, 13 Nov 2023 08:02:13 +0700 Subject: [PATCH 1/3] Simplify deck share --- functions/lib/short-unique-id/short-unique-id.ts | 5 +++++ functions/share-deck.ts | 3 ++- functions/upsert-deck.ts | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 functions/lib/short-unique-id/short-unique-id.ts diff --git a/functions/lib/short-unique-id/short-unique-id.ts b/functions/lib/short-unique-id/short-unique-id.ts new file mode 100644 index 00000000..be1c0426 --- /dev/null +++ b/functions/lib/short-unique-id/short-unique-id.ts @@ -0,0 +1,5 @@ +import ShortUniqueId from "short-unique-id"; + +export const shortUniqueId = () => { + return new ShortUniqueId({ length: 10 }).rnd(); +}; diff --git a/functions/share-deck.ts b/functions/share-deck.ts index ba99ed0b..4af89882 100644 --- a/functions/share-deck.ts +++ b/functions/share-deck.ts @@ -8,6 +8,7 @@ import { envSchema } from "./env/env-schema.ts"; import { getDatabase } from "./db/get-database.ts"; import { DatabaseException } from "./db/database-exception.ts"; import { createJsonResponse } from "./lib/json-response/create-json-response.ts"; +import { shortUniqueId } from "./lib/short-unique-id/short-unique-id.ts"; const requestSchema = z.object({ deckId: z.number(), @@ -49,7 +50,7 @@ export const onRequestPost = handleError(async ({ env, request }) => { ); } - const shareId = new ShortUniqueId({ length: 10 }).rnd(); + const shareId = shortUniqueId(); const { error } = await db .from("deck") diff --git a/functions/upsert-deck.ts b/functions/upsert-deck.ts index c4f0b663..5608518a 100644 --- a/functions/upsert-deck.ts +++ b/functions/upsert-deck.ts @@ -11,6 +11,7 @@ import { deckSchema } from "./db/deck/decks-with-cards-schema.ts"; import { addDeckToMineDb } from "./db/deck/add-deck-to-mine-db.ts"; import { createForbiddenRequestResponse } from "./lib/json-response/create-forbidden-request-response.ts"; import { canEditDeck } from "./db/deck/can-edit-deck.ts"; +import { shortUniqueId } from "./lib/short-unique-id/short-unique-id.ts"; const requestSchema = z.object({ id: z.number().nullable().optional(), @@ -53,6 +54,7 @@ export const onRequestPost = handleError(async ({ request, env }) => { .upsert({ id: input.data.id ? input.data.id : undefined, author_id: user.id, + share_id: input.data.id ? undefined : shortUniqueId(), name: input.data.title, description: input.data.description, is_public: false, From c2367a9a2367d7f2d54208dc5d5920add0da4f73 Mon Sep 17 00:00:00 2001 From: Gorbachev Egor <7gorbachevm@gmail.com> Date: Mon, 13 Nov 2023 08:04:36 +0700 Subject: [PATCH 2/3] wip --- functions/share-deck.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/functions/share-deck.ts b/functions/share-deck.ts index 4af89882..b73ddc91 100644 --- a/functions/share-deck.ts +++ b/functions/share-deck.ts @@ -1,6 +1,5 @@ import { handleError } from "./lib/handle-error/handle-error.ts"; import { z } from "zod"; -import ShortUniqueId from "short-unique-id"; import { getUser } from "./services/get-user.ts"; import { createAuthFailedResponse } from "./lib/json-response/create-auth-failed-response.ts"; import { createBadRequestResponse } from "./lib/json-response/create-bad-request-response.ts"; From 518380ed7629b09ec444646acf717a57e125c966 Mon Sep 17 00:00:00 2001 From: Gorbachev Egor <7gorbachevm@gmail.com> Date: Mon, 13 Nov 2023 08:39:48 +0700 Subject: [PATCH 3/3] Simplify deck share --- functions/share-deck.ts | 64 ------------------- src/api/api.ts | 12 ---- src/screens/deck-review/deck-preview.tsx | 2 +- src/screens/deck-review/share-deck-button.tsx | 40 +++--------- 4 files changed, 11 insertions(+), 107 deletions(-) delete mode 100644 functions/share-deck.ts diff --git a/functions/share-deck.ts b/functions/share-deck.ts deleted file mode 100644 index b73ddc91..00000000 --- a/functions/share-deck.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { handleError } from "./lib/handle-error/handle-error.ts"; -import { z } from "zod"; -import { getUser } from "./services/get-user.ts"; -import { createAuthFailedResponse } from "./lib/json-response/create-auth-failed-response.ts"; -import { createBadRequestResponse } from "./lib/json-response/create-bad-request-response.ts"; -import { envSchema } from "./env/env-schema.ts"; -import { getDatabase } from "./db/get-database.ts"; -import { DatabaseException } from "./db/database-exception.ts"; -import { createJsonResponse } from "./lib/json-response/create-json-response.ts"; -import { shortUniqueId } from "./lib/short-unique-id/short-unique-id.ts"; - -const requestSchema = z.object({ - deckId: z.number(), -}); -const responseSchema = z.object({ - shareId: z.string(), -}); - -export type ShareDeckRequest = z.infer; -export type ShareDeckResponse = z.infer; - -export const onRequestPost = handleError(async ({ env, request }) => { - const user = await getUser(request, env); - if (!user) return createAuthFailedResponse(); - - const input = requestSchema.safeParse(await request.json()); - if (!input.success) { - return createBadRequestResponse(); - } - - const envSafe = envSchema.parse(env); - const db = getDatabase(envSafe); - - const getDeckQuery = await db - .from("deck") - .select() - .eq("id", input.data.deckId) - .not("share_id", "is", null); - - if (getDeckQuery.error) { - throw new DatabaseException(getDeckQuery.error); - } - - if (getDeckQuery.data.length > 0) { - return createJsonResponse( - responseSchema.parse({ - shareId: getDeckQuery.data[0].share_id, - }), - ); - } - - const shareId = shortUniqueId(); - - const { error } = await db - .from("deck") - .update({ share_id: shareId }) - .eq("id", input.data.deckId); - - if (error) { - throw new DatabaseException(error); - } - - return createJsonResponse(responseSchema.parse({ shareId })); -}); diff --git a/src/api/api.ts b/src/api/api.ts index 5115ab0b..8fe1db1c 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -13,10 +13,6 @@ import { UpsertDeckRequest, UpsertDeckResponse, } from "../../functions/upsert-deck.ts"; -import { - ShareDeckRequest, - ShareDeckResponse, -} from "../../functions/share-deck.ts"; import { GetSharedDeckResponse } from "../../functions/get-shared-deck.ts"; import { AddCardRequest, AddCardResponse } from "../../functions/add-card.ts"; @@ -59,11 +55,3 @@ export const upsertDeckRequest = (body: UpsertDeckRequest) => { export const addCardRequest = (body: AddCardRequest) => { return request("/add-card", "POST", body); }; - -export const shareDeckRequest = (body: ShareDeckRequest) => { - return request( - "/share-deck", - "POST", - body, - ); -}; diff --git a/src/screens/deck-review/deck-preview.tsx b/src/screens/deck-review/deck-preview.tsx index c11fda74..98260998 100644 --- a/src/screens/deck-review/deck-preview.tsx +++ b/src/screens/deck-review/deck-preview.tsx @@ -68,7 +68,7 @@ export const DeckPreview = observer(() => {
- + {deckListStore.myId && deck.author_id === deckListStore.myId ? ( ); };