Skip to content

Commit

Permalink
Merge pull request #31 from GenerateNU/EDU-10
Browse files Browse the repository at this point in the history
created a proper get request and added queries
  • Loading branch information
TylerSchaefer4 authored Oct 3, 2023
2 parents e201738 + f09c7aa commit 12781e5
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/CompoundTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ export type UserReturnType = {

export type Message = {
message: string;
};
};

export type Quizzes = {
id : number;
questions : string;
passed : boolean;
score : number;
}
7 changes: 7 additions & 0 deletions lib/persistentQuizzesInstance.ts
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;
1 change: 1 addition & 0 deletions lib/persistentUserInstance.ts
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;
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions src/models/insert_data.sql
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())
44 changes: 44 additions & 0 deletions src/models/quizzes.ts
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";
}
}

}
2 changes: 1 addition & 1 deletion src/models/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,4 @@ export default class Users {

return userExists instanceof Users
}
}
}
52 changes: 52 additions & 0 deletions src/pages/api/subModules/[id].ts
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) });
}

}

0 comments on commit 12781e5

Please sign in to comment.