Skip to content

Commit

Permalink
Change table name to UserAnswer
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatehito committed Mar 1, 2024
1 parent 9402c1f commit 6cebd7c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Warnings:
- You are about to drop the `ProblemAnswerLog` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropTable
PRAGMA foreign_keys=off;
DROP TABLE "ProblemAnswerLog";
PRAGMA foreign_keys=on;

-- CreateTable
CREATE TABLE "UserAnswer" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"userId" TEXT NOT NULL,
"programId" TEXT NOT NULL,
"languageId" TEXT NOT NULL,
"problemType" TEXT NOT NULL,
"step" INTEGER NOT NULL,
"isPassed" BOOLEAN NOT NULL,
CONSTRAINT "UserAnswer_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
15 changes: 8 additions & 7 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ model User {
displayName String @unique
userSolvedAnswers UserCompletedProblem[]
ProblemAnswerLog ProblemAnswerLog[]
ProblemAnswerLog UserAnswer[]
UserProblemSession UserProblemSession[]
}

Expand Down Expand Up @@ -67,17 +67,18 @@ model UserCompletedProblem {
languageId String
}

model ProblemAnswerLog {
model UserAnswer {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
userId String
user User @relation(fields: [userId], references: [id])
userId String
programId String
languageId String
problemType String
startedAt DateTime
answeredAt DateTime
isPassed Boolean
step Int
isPassed Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { solveProblem } from '../../../../../lib/solveProblem';
import { Variables } from './Variables';

interface CheckpointProblemProps {
setStartedAt: (date: Date) => void;
setProblemType: (step: ProblemType) => void;
problemProgram: GeneratedProgram;
beforeCheckPointLine: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useSessionContext } from 'supertokens-auth-react/recipe/session';
import { generateProgram, getExplanation, programIdToName } from '../../../../../../problems/problemData';
import type { GeneratedProgram, ProblemType } from '../../../../../../types';
import { getLanguageIdFromSessionStorage } from '../../../../../lib/SessionStorage';
import { createProblemAnswerLog, createUserCompletedProblem } from '../../../../../lib/actions';
import { createUserAnswer, createUserCompletedProblem } from '../../../../../lib/actions';

import { CheckpointProblem } from './CheckpointProblem';
import { ExecutionResultProblem } from './ExecutionResultProblem';
Expand All @@ -22,7 +22,6 @@ const ProblemPage: NextPage<{ params: { courseId: string; programId: string } }>
// TODO: チェックポイントを取得する処理が実装できたら置き換える
const checkPointLines = [2, 6, 8, 12];

const [startedAt, setStartedAt] = useState(new Date());
const [selectedLanguageId, setSelectedLanguageId] = useState('');
const [problemType, setProblemType] = useState<ProblemType>('executionResult');
const [problemProgram, setProblemProgram] = useState<GeneratedProgram>({ displayProgram: '', excuteProgram: '' });
Expand All @@ -44,8 +43,7 @@ const ProblemPage: NextPage<{ params: { courseId: string; programId: string } }>
};

const createAnswerLog = async (isPassed: boolean): Promise<void> => {
await createProblemAnswerLog(programId, problemType, selectedLanguageId, userId, startedAt, new Date(), isPassed);
setStartedAt(new Date());
await createUserAnswer(programId, problemType, selectedLanguageId, userId, currentCheckPointLine, isPassed);
};

const explanation = getExplanation(programId, selectedLanguageId);
Expand Down Expand Up @@ -77,7 +75,6 @@ const ProblemPage: NextPage<{ params: { courseId: string; programId: string } }>
setBeforeCheckPointLine={setBeforeCheckPointLine}
setCurrentCheckPointLine={setCurrentCheckPointLine}
setProblemType={setProblemType}
setStartedAt={setStartedAt}
/>
);
}
Expand Down
10 changes: 4 additions & 6 deletions src/app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,22 @@ export async function fetchUserCompletedProblems(
}
}

export async function createProblemAnswerLog(
export async function createUserAnswer(
programId: string,
problemType: string,
languageId: string,
userId: string,
startedAt: Date,
answeredAt: Date,
step: number,
isPassed: boolean
): Promise<void> {
try {
await prisma.problemAnswerLog.create({
await prisma.userAnswer.create({
data: {
programId,
problemType,
languageId,
userId,
startedAt,
answeredAt,
step,
isPassed,
},
});
Expand Down

0 comments on commit 6cebd7c

Please sign in to comment.