Skip to content

Commit aee00db

Browse files
committed
refactor: Use DTO (#2627)
1 parent e63c209 commit aee00db

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

docs/dev-notes/2025-09-23/contest-task-pair-mapping/plan.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,32 @@ export async function getContestTaskPairs(): Promise<ContestTaskPairs> {
8787
* ContestTaskPair の新規レコードを作成
8888
*/
8989
export async function createContestTaskPair(
90-
contestId: string,
91-
taskTableIndex: string,
92-
taskId: string,
90+
params: ContestTaskPairCreate,
9391
): Promise<ContestTaskPair> {
92+
const { contestId, taskTableIndex, taskId } = params;
93+
9494
try {
95-
return await db.contestTaskPair.create({
95+
const contestTaskPair = await db.contestTaskPair.create({
9696
data: {
9797
contestId,
9898
taskTableIndex,
9999
taskId,
100100
},
101101
});
102+
103+
console.log('Created ContestTaskPair:', contestTaskPair);
104+
105+
return contestTaskPair;
102106
} catch (error) {
103107
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') {
104-
const errorMessage = `ContestTaskPair already exists: contestId=${contestId}, taskId=${taskId}`;
105-
console.error(errorMessage);
106-
throw new Error(errorMessage);
108+
console.log(`ContestTaskPair already exists: contestId=${contestId}, taskId=${taskId}`);
109+
const existingPair = await getContestTaskPair(contestId, taskId);
110+
111+
if (!existingPair) {
112+
throw new Error('Unexpected: record exists but cannot be fetched');
113+
}
114+
115+
return existingPair;
107116
}
108117

109118
console.error('Failed to create ContestTaskPair:', error);
@@ -115,12 +124,12 @@ export async function createContestTaskPair(
115124
* ContestTaskPair のレコードを更新
116125
*/
117126
export async function updateContestTaskPair(
118-
contestId: string,
119-
taskTableIndex: string,
120-
taskId: string,
127+
params: ContestTaskPairUpdate,
121128
): Promise<ContestTaskPair> {
129+
const { contestId, taskTableIndex, taskId } = params;
130+
122131
try {
123-
return await db.contestTaskPair.update({
132+
const updatedContestTaskPair = await db.contestTaskPair.update({
124133
where: {
125134
contestId_taskId: {
126135
contestId,
@@ -131,6 +140,10 @@ export async function updateContestTaskPair(
131140
taskTableIndex,
132141
},
133142
});
143+
144+
console.log('Updated ContestTaskPair:', updatedContestTaskPair);
145+
146+
return updatedContestTaskPair;
134147
} catch (error) {
135148
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2025') {
136149
const errorMessage = `Not found ContestTaskPair: contestId=${contestId}, taskId=${taskId}`;

src/lib/services/contest_task_pairs.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { Prisma } from '@prisma/client';
22

33
import { default as db } from '$lib/server/database';
44

5-
import type { ContestTaskPair, ContestTaskPairs } from '$lib/types/contest_task_pair';
5+
import type {
6+
ContestTaskPair,
7+
ContestTaskPairs,
8+
ContestTaskPairCreate,
9+
ContestTaskPairUpdate,
10+
} from '$lib/types/contest_task_pair';
611

712
/**
813
* Retrieves all ContestTaskPair records from the database.
@@ -38,19 +43,17 @@ export async function getContestTaskPair(
3843
/**
3944
* Creates a new ContestTaskPair record in the database.
4045
*
41-
* @param contestId - The ID of the contest.
42-
* @param taskTableIndex - The table index of the task.
43-
* @param taskId - The ID of the task.
46+
* @param params - The parameters for creating a ContestTaskPair.
4447
*
4548
* @returns The created ContestTaskPair object or the existing one if it already exists.
4649
*
4750
* @throws Will throw an error if the creation fails.
4851
*/
4952
export async function createContestTaskPair(
50-
contestId: string,
51-
taskTableIndex: string,
52-
taskId: string,
53+
params: ContestTaskPairCreate,
5354
): Promise<ContestTaskPair> {
55+
const { contestId, taskTableIndex, taskId } = params;
56+
5457
try {
5558
const contestTaskPair = await db.contestTaskPair.create({
5659
data: {
@@ -83,19 +86,17 @@ export async function createContestTaskPair(
8386
/**
8487
* Updates an existing ContestTaskPair record in the database.
8588
*
86-
* @param contestId: The ID of the contest.
87-
* @param taskTableIndex: The table index of the task.
88-
* @param taskId: The ID of the task.
89+
* @param params - The parameters for updating a ContestTaskPair.
8990
*
9091
* @returns The updated ContestTaskPair object.
9192
*
9293
* @throws Will throw an error if the update fails or if the record does not exist.
9394
*/
9495
export async function updateContestTaskPair(
95-
contestId: string,
96-
taskTableIndex: string,
97-
taskId: string,
96+
params: ContestTaskPairUpdate,
9897
): Promise<ContestTaskPair> {
98+
const { contestId, taskTableIndex, taskId } = params;
99+
99100
try {
100101
const updatedContestTaskPair = await db.contestTaskPair.update({
101102
where: {

src/lib/types/contest_task_pair.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type ContestTaskPairCreate = {
1515
taskId: string;
1616
};
1717

18-
export type ContestTaskPairUpdate = Partial<ContestTaskPairCreate>;
18+
export type ContestTaskPairUpdate = ContestTaskPairCreate;
1919

2020
// For mapping and identification
2121
export type ContestTaskPairKey = `${string}:${string}`; // "contest_id:task_id"

0 commit comments

Comments
 (0)