diff --git a/lib/CompoundTypes.ts b/lib/CompoundTypes.ts index 78d7ad4..4f57590 100644 --- a/lib/CompoundTypes.ts +++ b/lib/CompoundTypes.ts @@ -18,4 +18,11 @@ export type UserReturnType = { export type Message = { message: string; - }; \ No newline at end of file + }; + +export type Quizzes = { + id : number; + questions : string; + passed : boolean; + score : number; +} \ No newline at end of file diff --git a/lib/persistentQuizzesInstance.ts b/lib/persistentQuizzesInstance.ts new file mode 100644 index 0000000..0b0ae9b --- /dev/null +++ b/lib/persistentQuizzesInstance.ts @@ -0,0 +1,7 @@ +import Quizzes from "@/models/quizzes"; +import prisma from "./client"; + + +const persistentQuizzesInstance = new Quizzes(prisma.subModuleQuiz); + +export default persistentQuizzesInstance; \ No newline at end of file diff --git a/lib/persistentUserInstance.ts b/lib/persistentUserInstance.ts index 6bd17a8..b8d22bb 100644 --- a/lib/persistentUserInstance.ts +++ b/lib/persistentUserInstance.ts @@ -1,6 +1,7 @@ import Users from "@/models/users"; import prisma from "./client"; + const persistentUserInstance = new Users(prisma.user); export default persistentUserInstance; diff --git a/package-lock.json b/package-lock.json index 721854a..b63f162 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@types/react-dom": "18.2.7", "autoprefixer": "10.4.15", "crypto-js": "^4.1.1", + "i": "^0.3.7", "isemail": "^3.2.0", "jsonwebtoken": "^9.0.2", "next": "13.4.19", @@ -855,6 +856,14 @@ "node": ">= 0.4.0" } }, + "node_modules/i": { + "version": "0.3.7", + "resolved": "https://registry.npmjs.org/i/-/i-0.3.7.tgz", + "integrity": "sha512-FYz4wlXgkQwIPqhzC5TdNMLSE5+GS1IIDJZY/1ZiEPCT2S3COUVZeT5OW4BmW4r5LHLQuOosSwsvnroG9GR59Q==", + "engines": { + "node": ">=0.4" + } + }, "node_modules/inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", @@ -2540,6 +2549,11 @@ "function-bind": "^1.1.1" } }, + "i": { + "version": "0.3.7", + "resolved": "https://registry.npmjs.org/i/-/i-0.3.7.tgz", + "integrity": "sha512-FYz4wlXgkQwIPqhzC5TdNMLSE5+GS1IIDJZY/1ZiEPCT2S3COUVZeT5OW4BmW4r5LHLQuOosSwsvnroG9GR59Q==" + }, "inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", diff --git a/package.json b/package.json index 9538b12..5b4c6fd 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "@types/react-dom": "18.2.7", "autoprefixer": "10.4.15", "crypto-js": "^4.1.1", + "i": "^0.3.7", "isemail": "^3.2.0", "jsonwebtoken": "^9.0.2", "next": "13.4.19", diff --git a/src/models/insert_data.sql b/src/models/insert_data.sql new file mode 100644 index 0000000..5b5e58e --- /dev/null +++ b/src/models/insert_data.sql @@ -0,0 +1,8 @@ +INSERT INTO "Curriculum" ("id", "title") +VALUES (3, 'Curriculum Title'); +INSERT INTO "Module" ("id", "title", "curriculumId") +VALUES (3, 'Module Title', 1); +INSERT INTO "SubModule" ("id", "title", "moduleId") +VALUES (1, 'SubModule Title', 7); +INSERT INTO "SubModuleQuiz" ("id", "subModuleId", "questions", "passed", "score","updatedAt") +VALUES (1, 1, ARRAY['Question 1?', 'Question 2?'], FALSE, 100, NOW()) \ No newline at end of file diff --git a/src/models/quizzes.ts b/src/models/quizzes.ts new file mode 100644 index 0000000..aeb5754 --- /dev/null +++ b/src/models/quizzes.ts @@ -0,0 +1,44 @@ +import { PrismaClient, User } from "@prisma/client"; + +import isEmail from "isemail"; +import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library"; +import { UserData, UserReturnType } from "../../lib/CompoundTypes"; +import { data } from "autoprefixer"; + +// Uer Information that is Insensitive +export interface QuizInfo { + id: number; + questions?: string[]; + passed?: boolean; + score?: number; +} +const UNIQUE_CONSTRAINT_ERROR_CODE = "P2002"; + +export default class SubModuleQuizzes { + constructor(private readonly quizDB: PrismaClient["subModuleQuiz"]) {} + + + // Get Insensitive User Information By ID + public async getQuizById(id: number) { + try { + // Select Insensitive User Information from Database based on ID + const QuizzesInfo: QuizInfo | null = + await this.quizDB.findUnique({ + where: { + id, + }, + select: { + id: true, + questions: true, + passed: true, + score: true, + }, + }); + + return QuizzesInfo; + } catch (Error) { + throw "Error: getQuizId is not an Number"; + } + } + +} \ No newline at end of file diff --git a/src/models/users.ts b/src/models/users.ts index 61bb10d..2468212 100644 --- a/src/models/users.ts +++ b/src/models/users.ts @@ -130,4 +130,4 @@ export default class Users { return userExists instanceof Users } -} +} \ No newline at end of file diff --git a/src/pages/api/subModules/[id].ts b/src/pages/api/subModules/[id].ts index e69de29..deacddc 100644 --- a/src/pages/api/subModules/[id].ts +++ b/src/pages/api/subModules/[id].ts @@ -0,0 +1,52 @@ +// GET, PUT, DELETE operations for a specific user by ID +import type { NextApiRequest, NextApiResponse } from "next"; +import persistentUserInstance from "../../../../lib/persistentUserInstance"; +import { getServerSession } from "next-auth"; +import { authOptions } from "../auth/[...nextauth]"; +import Users from "@/models/users"; +import { error } from "console"; +import { PrismaClient, User } from "@prisma/client"; +const prisma = new PrismaClient(); +import persistentQuizzesInstance from "../../../../lib/persistentQuizzesInstance"; +import { QuizInfo } from "@/models/quizzes"; + +type Message = { + message?: string; +}; + + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + const session = await getServerSession(req, res, authOptions); + console.log("hello"); + const supportedRequestMethods: { [key: string]: Function } = { + GET: getQuiz, + }; + console.log(req.body); + + if (req.method) { + return supportedRequestMethods[req.method](req, res, session); + } + return res.status(405).send({ message: "request method not supported" }); +} + + +async function getQuiz( + req: NextApiRequest, + res: NextApiResponse +) { + let id = Number(req.query.id); + + try { + const quizInfo = await persistentQuizzesInstance.getQuizById(id); + if (!quizInfo || Object.keys(quizInfo).length === 0) { + return res.status(404).send({ message: "Submodule Quiz Not Found" }); + } + return res.status(200).send(quizInfo); + } catch (error) { + return res.status(403).send({ message: String(error) }); + } + +}