Skip to content

Commit b0f71da

Browse files
committed
♻️ Refactoring (#1124)
1 parent 08d8232 commit b0f71da

File tree

1 file changed

+21
-66
lines changed

1 file changed

+21
-66
lines changed

prisma/seed.ts

Lines changed: 21 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,9 @@ async function addTasks() {
127127
task_id: task.id,
128128
},
129129
});
130-
const registeredTaskTag = await prisma.taskTag.findMany({
131-
where: {
132-
task_id: task.id,
133-
},
134-
});
135130

136131
if (!registeredTask) {
137-
await addTask(task, taskFactory, registeredTaskTag.length !== 0);
132+
await addTask(task, taskFactory);
138133
console.log('task id:', task.id, 'was registered.');
139134
}
140135
} catch (e) {
@@ -147,35 +142,16 @@ async function addTasks() {
147142
console.log('Finished adding tasks.');
148143
}
149144

150-
async function addTask(task, taskFactory, isHavingTaskTag) {
151-
if (isHavingTaskTag) {
152-
await taskFactory.create({
153-
contest_type: classifyContest(task.contest_id),
154-
contest_id: task.contest_id,
155-
task_table_index: task.problem_index,
156-
task_id: task.id,
157-
title: task.title,
158-
grade: task.grade,
159-
tags: {
160-
create: [
161-
{
162-
tag: {
163-
connect: { task_id: task.id },
164-
},
165-
},
166-
],
167-
},
168-
});
169-
} else {
170-
await taskFactory.create({
171-
contest_type: classifyContest(task.contest_id),
172-
contest_id: task.contest_id,
173-
task_table_index: task.problem_index,
174-
task_id: task.id,
175-
title: task.title,
176-
grade: task.grade,
177-
});
178-
}
145+
async function addTask(task, taskFactory) {
146+
// Note: Task-Tag relationships are handled separately via TaskTag table
147+
await taskFactory.create({
148+
contest_type: classifyContest(task.contest_id),
149+
contest_id: task.contest_id,
150+
task_table_index: task.problem_index,
151+
task_id: task.id,
152+
title: task.title,
153+
grade: task.grade,
154+
});
179155
}
180156

181157
async function addWorkBooks() {
@@ -295,15 +271,10 @@ async function addTags() {
295271
id: tag.id,
296272
},
297273
});
298-
const registeredTaskTag = await prisma.taskTag.findMany({
299-
where: {
300-
tag_id: tag.id,
301-
},
302-
});
303274

304275
if (registeredTag.length === 0) {
305276
console.log('tag id:', tag.id, 'was registered.');
306-
await addTag(tag, tagFactory, registeredTaskTag.length !== 0);
277+
await addTag(tag, tagFactory);
307278
}
308279
} catch (e) {
309280
console.error('Failed to add tag', tag.id, e);
@@ -315,31 +286,15 @@ async function addTags() {
315286
console.log('Finished adding tags.');
316287
}
317288

318-
async function addTag(tag, tagFactory, isHavingTaskTag) {
319-
if (isHavingTaskTag) {
320-
await tagFactory.create({
321-
id: tag.id,
322-
name: tag.name,
323-
is_official: tag.is_official,
324-
is_published: tag.is_published,
325-
tasks: {
326-
create: [
327-
{
328-
task: {
329-
connect: { tag_id: tag.id },
330-
},
331-
},
332-
],
333-
},
334-
});
335-
} else {
336-
await tagFactory.create({
337-
id: tag.id,
338-
name: tag.name,
339-
is_official: tag.is_official,
340-
is_published: tag.is_published,
341-
});
342-
}
289+
async function addTag(tag, tagFactory) {
290+
// Note: Tags and Tasks are connected via the TaskTag relationship table
291+
// which is handled separately in addTaskTags()
292+
await tagFactory.create({
293+
id: tag.id,
294+
name: tag.name,
295+
is_official: tag.is_official,
296+
is_published: tag.is_published,
297+
});
343298
}
344299

345300
async function addTaskTags() {

0 commit comments

Comments
 (0)