diff --git a/prisma/migrations/20240301082352_change_table_name_to_user_answer/migration.sql b/prisma/migrations/20240301082352_change_table_name_to_user_answer/migration.sql new file mode 100644 index 00000000..90b9cc3e --- /dev/null +++ b/prisma/migrations/20240301082352_change_table_name_to_user_answer/migration.sql @@ -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 +); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 5cc12ec9..0af88294 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -19,7 +19,7 @@ model User { displayName String @unique userSolvedAnswers UserCompletedProblem[] - ProblemAnswerLog ProblemAnswerLog[] + ProblemAnswerLog UserAnswer[] UserProblemSession UserProblemSession[] } @@ -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 } diff --git a/src/app/(withAuth)/courses/[courseId]/programs/[programId]/CheckpointProblem.tsx b/src/app/(withAuth)/courses/[courseId]/programs/[programId]/CheckpointProblem.tsx index fe91c3af..4dc12d19 100644 --- a/src/app/(withAuth)/courses/[courseId]/programs/[programId]/CheckpointProblem.tsx +++ b/src/app/(withAuth)/courses/[courseId]/programs/[programId]/CheckpointProblem.tsx @@ -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; diff --git a/src/app/(withAuth)/courses/[courseId]/programs/[programId]/page.tsx b/src/app/(withAuth)/courses/[courseId]/programs/[programId]/page.tsx index b5bf807b..198b82a0 100644 --- a/src/app/(withAuth)/courses/[courseId]/programs/[programId]/page.tsx +++ b/src/app/(withAuth)/courses/[courseId]/programs/[programId]/page.tsx @@ -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'; @@ -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('executionResult'); const [problemProgram, setProblemProgram] = useState({ displayProgram: '', excuteProgram: '' }); @@ -44,8 +43,7 @@ const ProblemPage: NextPage<{ params: { courseId: string; programId: string } }> }; const createAnswerLog = async (isPassed: boolean): Promise => { - 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); @@ -77,7 +75,6 @@ const ProblemPage: NextPage<{ params: { courseId: string; programId: string } }> setBeforeCheckPointLine={setBeforeCheckPointLine} setCurrentCheckPointLine={setCurrentCheckPointLine} setProblemType={setProblemType} - setStartedAt={setStartedAt} /> ); } diff --git a/src/app/lib/actions.ts b/src/app/lib/actions.ts index d1774a3c..e97aa7ae 100644 --- a/src/app/lib/actions.ts +++ b/src/app/lib/actions.ts @@ -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 { try { - await prisma.problemAnswerLog.create({ + await prisma.userAnswer.create({ data: { programId, problemType, languageId, userId, - startedAt, - answeredAt, + step, isPassed, }, });