Skip to content

Commit 7c69d3a

Browse files
committed
♻️ Refactoring
(#2020)
1 parent 9a7a294 commit 7c69d3a

File tree

2 files changed

+2
-46
lines changed

2 files changed

+2
-46
lines changed

src/lib/utils/workbook.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,45 +40,6 @@ export async function getWorkbookWithAuthor(
4040
return { workBook: workBook, isExistingAuthor: isExistingAuthor };
4141
}
4242

43-
/**
44-
* Finds a workbook ID from a given slug string.
45-
*
46-
* This function first attempts to parse the slug as a direct workbook ID.
47-
* If that fails, it tries to parse it as a workbook URL slug and looks up
48-
* the corresponding workbook in the database.
49-
*
50-
* @param slug - The slug string to search for, can be either a numeric ID or URL slug
51-
* @returns A Promise that resolves to the workbook ID if found, or null if not found
52-
*
53-
* @example
54-
* ```typescript
55-
* // Using numeric ID
56-
* const id1 = await findWorkBookIdFrom("123");
57-
*
58-
* // Using URL slug
59-
* const id2 = await findWorkBookIdFrom("union-find");
60-
* ```
61-
*/
62-
export async function findWorkBookIdFrom(slug: string): Promise<number | null> {
63-
const workBookId = parseWorkBookId(slug);
64-
65-
if (workBookId !== null) {
66-
return workBookId;
67-
}
68-
69-
const workBookUrlSlug = parseWorkBookUrlSlug(slug);
70-
71-
if (workBookUrlSlug !== null) {
72-
const workBook = await workBookCrud.getWorkBookByUrlSlug(slug);
73-
74-
if (workBook !== null && workBook.id !== null) {
75-
return workBook.id;
76-
}
77-
}
78-
79-
return null;
80-
}
81-
8243
export function parseWorkBookId(slug: string): number | null {
8344
const isOnlyDigits = (id: string) => /^\d+$/.test(id);
8445
const id = Number(slug);

src/routes/workbooks/[slug]/+page.server.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@ import * as taskResultsCrud from '$lib/services/task_results';
77
import * as action from '$lib/actions/update_task_result';
88

99
import { getLoggedInUser, isAdmin, canRead } from '$lib/utils/authorship';
10-
import { getWorkbookWithAuthor, findWorkBookIdFrom } from '$lib/utils/workbook';
11-
import { BAD_REQUEST, FORBIDDEN } from '$lib/constants/http-response-status-codes';
10+
import { getWorkbookWithAuthor } from '$lib/utils/workbook';
11+
import { FORBIDDEN } from '$lib/constants/http-response-status-codes';
1212

1313
export async function load({ locals, params }) {
1414
const loggedInUser = await getLoggedInUser(locals);
1515
const loggedInAsAdmin = isAdmin(loggedInUser?.role as Roles);
1616
const slug = params.slug.toLowerCase();
17-
const workBookId = await findWorkBookIdFrom(slug);
18-
19-
if (workBookId === null) {
20-
error(BAD_REQUEST, '不正な問題集idです。');
21-
}
2217

2318
const workbookWithAuthor = await getWorkbookWithAuthor(slug);
2419
const workBook = workbookWithAuthor.workBook;

0 commit comments

Comments
 (0)