Skip to content

Commit 690f35c

Browse files
authored
Merge pull request #82 from codegasms/fix/problem-solving-page
Fix code editor page so it runs all test cases and checks validity of the output
2 parents 5f35194 + 2e55a2f commit 690f35c

File tree

7 files changed

+256
-53
lines changed

7 files changed

+256
-53
lines changed

app/api/orgs/[orgId]/contests/[contestId]/problems/[problemId]/service.ts

+37-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import { db } from "@/db/drizzle";
2-
import { problems, contestProblems } from "@/db/schema";
2+
import { problems, contestProblems, testCases } from "@/db/schema";
33
import { eq, and } from "drizzle-orm";
44
import { getProblemIdFromCode } from "../../../../problems/service";
55

66
export async function getContestProblem(
77
contestId: number,
88
problemCode: string,
99
) {
10-
const problem = await db
10+
// First, find the problem ID from the contest problems
11+
const contestProblemResult = await db
1112
.select({
12-
id: problems.id,
13-
code: problems.code,
14-
title: problems.title,
15-
description: problems.description,
16-
allowedLanguages: problems.allowedLanguages,
13+
problemId: contestProblems.problemId,
1714
order: contestProblems.order,
1815
})
1916
.from(contestProblems)
@@ -26,11 +23,42 @@ export async function getContestProblem(
2623
)
2724
.limit(1);
2825

29-
if (!problem[0]) {
26+
if (!contestProblemResult[0]) {
3027
throw new Error("Problem not found");
3128
}
3229

33-
return problem[0];
30+
const { problemId, order } = contestProblemResult[0];
31+
32+
// Then, get the problem with its example test cases
33+
const problem = await db.query.problems.findFirst({
34+
where: eq(problems.id, problemId),
35+
columns: {
36+
id: true,
37+
code: true,
38+
title: true,
39+
description: true,
40+
allowedLanguages: true,
41+
},
42+
with: {
43+
testCases: {
44+
where: eq(testCases.kind, "example"),
45+
columns: {
46+
input: true,
47+
output: true,
48+
},
49+
},
50+
},
51+
});
52+
53+
if (!problem) {
54+
throw new Error("Problem not found");
55+
}
56+
57+
// Add the order from contest problems
58+
return {
59+
...problem,
60+
order,
61+
};
3462
}
3563

3664
export async function updateContestProblem(

app/api/orgs/[orgId]/contests/[contestId]/problems/service.ts

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { db } from "@/db/drizzle";
22
import { contestProblems, problems } from "@/db/schema";
3-
import { eq } from "drizzle-orm";
3+
import { eq, and } from "drizzle-orm";
44

55
export async function addProblemToContest(
66
contestId: number,
@@ -46,3 +46,37 @@ export async function getContestProblems(contestId: number) {
4646
.where(eq(contestProblems.contestId, contestId))
4747
.orderBy(contestProblems.order);
4848
}
49+
50+
export async function removeProblemFromContest(
51+
orgId: number,
52+
contestId: number,
53+
problemCode: string,
54+
) {
55+
return await db.transaction(async (tx) => {
56+
// First, find the problem ID from the problem code
57+
const problem = await tx.query.problems.findFirst({
58+
where: and(eq(problems.orgId, orgId), eq(problems.code, problemCode)),
59+
});
60+
61+
if (!problem) {
62+
throw new Error("Problem not found");
63+
}
64+
65+
// Delete the contest problem entry
66+
const deleted = await tx
67+
.delete(contestProblems)
68+
.where(
69+
and(
70+
eq(contestProblems.contestId, contestId),
71+
eq(contestProblems.problemId, problem.id),
72+
),
73+
)
74+
.returning();
75+
76+
if (deleted.length === 0) {
77+
throw new Error("Problem not found in contest");
78+
}
79+
80+
return deleted[0];
81+
});
82+
}

app/api/orgs/[orgId]/problems/[problemId]/service.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import { db } from "@/db/drizzle";
2-
import { problems, contestProblems } from "@/db/schema";
2+
import { problems, contestProblems, testCases } from "@/db/schema";
33
import { eq, and } from "drizzle-orm";
44
import { z } from "zod";
55
import { updateProblemSchema } from "@/lib/validations";
66

77
export async function getProblem(orgId: number, code: string) {
88
const problem = await db.query.problems.findFirst({
99
where: and(eq(problems.orgId, orgId), eq(problems.code, code)),
10+
with: {
11+
testCases: {
12+
where: eq(testCases.kind, "example"),
13+
columns: {
14+
input: true,
15+
output: true,
16+
},
17+
},
18+
},
1019
});
1120

1221
if (!problem) {

0 commit comments

Comments
 (0)