-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
628 additions
and
49 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,27 @@ | ||
import { EnvSafe } from "../../env/env-schema.ts"; | ||
import { getDatabase } from "../get-database.ts"; | ||
import { DatabaseException } from "../database-exception.ts"; | ||
import { z } from "zod"; | ||
|
||
export const schema = z.object({ | ||
id: z.string(), | ||
name: z.string(), | ||
}); | ||
|
||
export type MyDeckFlatDb = z.infer<typeof schema>; | ||
|
||
export const getAllMyDecksFlatDb = async (env: EnvSafe, userId: number) => { | ||
const db = getDatabase(env); | ||
|
||
const { data, error } = await db | ||
.from("deck") | ||
.select("id") | ||
.eq("author_id", userId) | ||
.limit(500); | ||
|
||
if (error) { | ||
throw new DatabaseException(error); | ||
} | ||
|
||
return z.array(schema).parse(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,26 @@ | ||
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(), | ||
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
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,43 @@ | ||
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"; | ||
|
||
const requestSchema = z.object({ | ||
id: z.number().optional(), | ||
title: z.string(), | ||
}); | ||
|
||
export type AddFolderRequest = z.infer<typeof requestSchema>; | ||
export type AddFolderResponse = null; | ||
|
||
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 db = getDatabase(envSafe); | ||
const { data } = input; | ||
|
||
const upsertFolderResult = await db.from("folder").upsert({ | ||
id: data.id, | ||
title: data.title, | ||
author_id: user.id, | ||
}); | ||
|
||
if (upsertFolderResult.error) { | ||
throw new DatabaseException(upsertFolderResult.error); | ||
} | ||
|
||
return createJsonResponse<AddFolderResponse>(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
Oops, something went wrong.