Skip to content

Commit 052d0f3

Browse files
committed
feat: Create crud for pairs of contest and task (#2627)
1 parent c3aa2d1 commit 052d0f3

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { default as db } from '$lib/server/database';
2+
3+
import type { ContestTaskPair, ContestTaskPairs } from '$lib/types/contest_task_pair';
4+
5+
/**
6+
* Retrieves all ContestTaskPair records from the database.
7+
*
8+
* @returns An array of ContestTaskPair objects.
9+
*/
10+
export async function getContestTaskPairs(): Promise<ContestTaskPairs> {
11+
return await db.contestTaskPair.findMany();
12+
}
13+
14+
/**
15+
* Retrieves a ContestTaskPair record by contestId and taskId.
16+
*
17+
* @param contestId: The ID of the contest.
18+
* @param taskId: The ID of the task.
19+
*
20+
* @returns The ContestTaskPair if found, otherwise null.
21+
*/
22+
export async function getContestTaskPair(
23+
contestId: string,
24+
taskId: string,
25+
): Promise<ContestTaskPair | null> {
26+
const contestTaskPair = await db.contestTaskPair.findUnique({
27+
where: {
28+
contestId_taskId: {
29+
contestId,
30+
taskId,
31+
},
32+
},
33+
});
34+
35+
return contestTaskPair;
36+
}
37+
38+
/**
39+
* Creates a new ContestTaskPair record in the database.
40+
*
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.
44+
*
45+
* @throws Will throw an error if the creation fails.
46+
*/
47+
export async function createContestTaskPair(
48+
contestId: string,
49+
taskTableIndex: string,
50+
taskId: string,
51+
): Promise<void> {
52+
try {
53+
const existingRecord = await getContestTaskPair(contestId, taskId);
54+
55+
if (existingRecord) {
56+
console.log(`ContestTaskPair already exists: contestId=${contestId}, taskId=${taskId}`);
57+
return;
58+
}
59+
60+
let contestTaskPair: ContestTaskPair | undefined;
61+
62+
contestTaskPair = await db.contestTaskPair.create({
63+
data: {
64+
contestId,
65+
taskTableIndex,
66+
taskId,
67+
},
68+
});
69+
70+
console.log('Created ContestTaskPair:', contestTaskPair);
71+
} catch (error) {
72+
if (error && typeof error === 'object' && 'code' in error && (error as any).code === 'P2002') {
73+
console.log(`Found ContestTaskPair (race): contestId=${contestId}, taskId=${taskId}`);
74+
return;
75+
}
76+
77+
console.error('Failed to create ContestTaskPair:', error);
78+
throw error;
79+
}
80+
}
81+
82+
/**
83+
* Updates an existing ContestTaskPair record in the database.
84+
*
85+
* @param contestId: The ID of the contest.
86+
* @param taskTableIndex: The table index of the task.
87+
* @param taskId: The ID of the task.
88+
*
89+
* @throws Will throw an error if the update fails or if the record does not exist.
90+
*/
91+
export async function updateContestTaskPair(
92+
contestId: string,
93+
taskTableIndex: string,
94+
taskId: string,
95+
): Promise<void> {
96+
try {
97+
const existingRecord = await getContestTaskPair(contestId, taskId);
98+
99+
if (!existingRecord) {
100+
const errorMessage = `Not found ContestTaskPair: contestId=${contestId}, taskId=${taskId}`;
101+
console.log(errorMessage);
102+
throw new Error(errorMessage);
103+
}
104+
105+
const updatedContestTaskPair = await db.contestTaskPair.update({
106+
where: {
107+
contestId_taskId: {
108+
contestId,
109+
taskId,
110+
},
111+
},
112+
data: {
113+
taskTableIndex,
114+
},
115+
});
116+
117+
console.log('Updated ContestTaskPair:', updatedContestTaskPair);
118+
} catch (error) {
119+
console.error('Failed to update ContestTaskPair:', error);
120+
throw error;
121+
}
122+
}

0 commit comments

Comments
 (0)