Skip to content

Commit

Permalink
Simplify deck share
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed Nov 13, 2023
1 parent 8c61829 commit 7c5fc61
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions functions/lib/short-unique-id/short-unique-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ShortUniqueId from "short-unique-id";

export const shortUniqueId = () => {
return new ShortUniqueId({ length: 10 }).rnd();
};
3 changes: 2 additions & 1 deletion functions/share-deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions functions/upsert-deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7c5fc61

Please sign in to comment.