-
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.
Merge branch 'main' of github.com:kubk/memo-card
- Loading branch information
Showing
52 changed files
with
1,528 additions
and
277 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { EnvSafe } from "../../env/env-schema.ts"; | ||
import { getDatabase } from "../get-database.ts"; | ||
import { DatabaseException } from "../database-exception.ts"; | ||
|
||
export const deleteFolderById = async ( | ||
env: EnvSafe, | ||
folderId: number, | ||
): Promise<void> => { | ||
const db = getDatabase(env); | ||
const deleteFolderResult = await db | ||
.from("folder") | ||
.delete() | ||
.eq("id", folderId) | ||
.single(); | ||
|
||
if (deleteFolderResult.error) { | ||
throw new DatabaseException(deleteFolderResult.error); | ||
} | ||
}; |
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,23 @@ | ||
import { EnvSafe } from "../../env/env-schema.ts"; | ||
import { getDatabase } from "../get-database.ts"; | ||
import { DatabaseException } from "../database-exception.ts"; | ||
|
||
export const getFolderByIdAndAuthorId = async ( | ||
envSafe: EnvSafe, | ||
folderId: number, | ||
user: { id: number; is_admin: boolean }, | ||
) => { | ||
const db = getDatabase(envSafe); | ||
|
||
let query = db.from("folder").select().eq("id", folderId); | ||
if (!user.is_admin) { | ||
query = query.eq("author_id", user.id); | ||
} | ||
|
||
const canEditResult = await query.single(); | ||
if (canEditResult.error) { | ||
throw new DatabaseException(canEditResult.error); | ||
} | ||
|
||
return canEditResult.data ?? 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { EnvSafe } from "../../env/env-schema.ts"; | ||
import { getDatabase } from "../get-database.ts"; | ||
import { DatabaseException } from "../database-exception.ts"; | ||
import { z } from "zod"; | ||
|
||
const userFoldersSchema = z.object({ | ||
folder_id: z.number(), | ||
folder_title: z.string(), | ||
folder_description: z.string().nullable(), | ||
folder_author_id: z.number(), | ||
deck_id: z.number().nullable(), | ||
}); | ||
|
||
export type UserFoldersDbType = z.infer<typeof userFoldersSchema>; | ||
|
||
export const getFoldersWithDecksDb = async (env: EnvSafe, userId: number) => { | ||
const db = getDatabase(env); | ||
|
||
const result = await db.rpc("get_folder_with_decks", { | ||
usr_id: userId, | ||
}); | ||
|
||
if (result.error) { | ||
throw new DatabaseException(result.error); | ||
} | ||
|
||
return z.array(userFoldersSchema).parse(result.data); | ||
}; |
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,21 @@ | ||
import { handleError } from "./lib/handle-error/handle-error.ts"; | ||
import { getUser } from "./services/get-user.ts"; | ||
import { createAuthFailedResponse } from "./lib/json-response/create-auth-failed-response.ts"; | ||
import { envSchema } from "./env/env-schema.ts"; | ||
import { getDecksCreatedByMe } from "./db/deck/get-decks-created-by-me.ts"; | ||
import { DeckWithoutCardsDbType } from "./db/deck/decks-with-cards-schema.ts"; | ||
import { createJsonResponse } from "./lib/json-response/create-json-response.ts"; | ||
|
||
export type DecksMineResponse = { | ||
decks: DeckWithoutCardsDbType[]; | ||
}; | ||
|
||
export const onRequest = handleError(async ({ request, env }) => { | ||
const user = await getUser(request, env); | ||
if (!user) return createAuthFailedResponse(); | ||
const envSafe = envSchema.parse(env); | ||
|
||
const decks = await getDecksCreatedByMe(envSafe, user.id); | ||
|
||
return createJsonResponse<DecksMineResponse>({ decks: decks }); | ||
}); |
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,35 @@ | ||
import { handleError } from "./lib/handle-error/handle-error.ts"; | ||
import { getUser } from "./services/get-user.ts"; | ||
import { createAuthFailedResponse } from "./lib/json-response/create-auth-failed-response.ts"; | ||
import { envSchema } from "./env/env-schema.ts"; | ||
import { createBadRequestResponse } from "./lib/json-response/create-bad-request-response.ts"; | ||
import { getFolderByIdAndAuthorId } from "./db/folder/get-folder-by-id-and-author-id.ts"; | ||
import { createJsonResponse } from "./lib/json-response/create-json-response.ts"; | ||
import { deleteFolderById } from "./db/folder/delete-folder-by-id.ts"; | ||
|
||
export const onRequestPost = handleError(async ({ request, env }) => { | ||
const user = await getUser(request, env); | ||
if (!user) { | ||
return createAuthFailedResponse(); | ||
} | ||
const envSafe = envSchema.parse(env); | ||
|
||
const url = new URL(request.url); | ||
const folderId = url.searchParams.get("folder_id"); | ||
if (!folderId) { | ||
return createBadRequestResponse(); | ||
} | ||
|
||
const canEdit = await getFolderByIdAndAuthorId( | ||
envSafe, | ||
parseInt(folderId), | ||
user, | ||
); | ||
if (!canEdit) { | ||
return createBadRequestResponse(); | ||
} | ||
|
||
await deleteFolderById(envSafe, parseInt(folderId)); | ||
|
||
return createJsonResponse(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { handleError } from "./lib/handle-error/handle-error.ts"; | ||
import { getUser } from "./services/get-user.ts"; | ||
import { createAuthFailedResponse } from "./lib/json-response/create-auth-failed-response.ts"; | ||
import { z } from "zod"; | ||
import { createBadRequestResponse } from "./lib/json-response/create-bad-request-response.ts"; | ||
import { getDatabase } from "./db/get-database.ts"; | ||
import { envSchema } from "./env/env-schema.ts"; | ||
import { DatabaseException } from "./db/database-exception.ts"; | ||
import { createJsonResponse } from "./lib/json-response/create-json-response.ts"; | ||
import { | ||
getFoldersWithDecksDb, | ||
UserFoldersDbType, | ||
} from "./db/folder/get-folders-with-decks-db.tsx"; | ||
import { getFolderByIdAndAuthorId } from "./db/folder/get-folder-by-id-and-author-id.ts"; | ||
|
||
const requestSchema = z.object({ | ||
id: z.number().optional(), | ||
title: z.string(), | ||
description: z.string().nullable(), | ||
deckIds: z.array(z.number()), | ||
}); | ||
|
||
export type AddFolderRequest = z.infer<typeof requestSchema>; | ||
export type AddFolderResponse = { | ||
folder: { | ||
id: number; | ||
} | ||
folders: UserFoldersDbType[]; | ||
}; | ||
|
||
export const onRequestPost = handleError(async ({ request, env }) => { | ||
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 { data } = input; | ||
if (data.id) { | ||
const canEdit = await getFolderByIdAndAuthorId(envSafe, data.id, user); | ||
if (!canEdit) { | ||
return createBadRequestResponse(); | ||
} | ||
} | ||
|
||
const db = getDatabase(envSafe); | ||
|
||
const upsertFolderResult = await db | ||
.from("folder") | ||
.upsert({ | ||
id: data.id, | ||
title: data.title, | ||
description: data.description, | ||
author_id: user.id, | ||
}) | ||
.select() | ||
.single(); | ||
|
||
if (upsertFolderResult.error) { | ||
throw new DatabaseException(upsertFolderResult.error); | ||
} | ||
|
||
const folderId = upsertFolderResult.data.id; | ||
|
||
const oldDeckFolderResult = await db.from("deck_folder").delete().match({ | ||
folder_id: folderId, | ||
}); | ||
|
||
if (oldDeckFolderResult.error) { | ||
throw new DatabaseException(oldDeckFolderResult.error); | ||
} | ||
|
||
const upsertDeckFolderResult = await db.from("deck_folder").upsert( | ||
data.deckIds.map((deckId) => ({ | ||
deck_id: deckId, | ||
folder_id: folderId, | ||
})), | ||
); | ||
|
||
if (upsertDeckFolderResult.error) { | ||
throw new DatabaseException(upsertDeckFolderResult.error); | ||
} | ||
|
||
return createJsonResponse<AddFolderResponse>({ | ||
folder: upsertFolderResult.data, | ||
folders: await getFoldersWithDecksDb(envSafe, user.id), | ||
}); | ||
}); |
Oops, something went wrong.