-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from GenerateNU/EDU-10
created a proper get request and added queries
- Loading branch information
Showing
9 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Quizzes from "@/models/quizzes"; | ||
import prisma from "./client"; | ||
|
||
|
||
const persistentQuizzesInstance = new Quizzes(prisma.subModuleQuiz); | ||
|
||
export default persistentQuizzesInstance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import Users from "@/models/users"; | ||
import prisma from "./client"; | ||
|
||
|
||
const persistentUserInstance = new Users(prisma.user); | ||
|
||
export default persistentUserInstance; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,4 +130,4 @@ export default class Users { | |
|
||
return userExists instanceof Users | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Message> | ||
) { | ||
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<Message | QuizInfo> | ||
) { | ||
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) }); | ||
} | ||
|
||
} |