Skip to content

Commit

Permalink
Merge pull request #3 from Siddharth9890/feat/dsa-sheets
Browse files Browse the repository at this point in the history
Feat/dsa sheets
  • Loading branch information
Siddharth9890 authored Feb 15, 2024
2 parents 0092077 + fc366c1 commit 1bb945e
Show file tree
Hide file tree
Showing 22 changed files with 293 additions and 339 deletions.
8 changes: 4 additions & 4 deletions db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const makeConnectionWithDB = async () => {
try {
await sequelize.authenticate();
// if (process.env.NODE_ENV === "development") {
await sequelize.sync({
force: true,
logging: (sql) => console.log(sql),
});
// await sequelize.sync({
// force: true,
// logging: (sql) => console.log(sql),
// });
// }
console.log("Connection has been established successfully.");
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function makeDatabase() {
await makeConnectionWithDB();
}

app.use(express.json({ limit: "1kb" }));
app.use(express.json({ limit: "5kb" }));

routes(app);

Expand Down
6 changes: 3 additions & 3 deletions routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ function routes(app: Express): void {

app.use(verifyJWT);

// app.use("/api/v2/subject", subjectRouter);
app.use("/api/v2/subject", subjectRouter);

// app.use("/api/v2/topic", topicRouter);
app.use("/api/v2/topic", topicRouter);

// app.use("/api/v2/question", questionRouter);
app.use("/api/v2/question", questionRouter);

app.use(verifyPermission);

Expand Down
34 changes: 15 additions & 19 deletions src/DataAccessLayer/question.dal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,43 @@ export const createQuestionDal = async (
};

export const updateQuestionDal = async (
questionName: string,
questionId: number,
payload: Partial<QuestionInput>
): Promise<QuestionOutput> => {
const question = await Question.findOne({
where: { question_name: questionName },
});
const question = await Question.findByPk(questionId);
if (!question) throw "Question not found";

const updatedQuestion = question.set({
question_description: payload.question_description,
description: payload.description,
difficulty: payload.difficulty,
leet_code_problem_link: payload.leet_code_problem_link,
question_name: payload.question_name,
youtube_video_link: payload.youtube_video_link,
under_which_topic: payload.under_which_topic,
problemLink: payload.problemLink,
name: payload.name,
videoLink: payload.videoLink,
underWhichTopic: payload.underWhichTopic,
});
await updatedQuestion.save();
return updatedQuestion;
};

export const getQuestionsUnderTopicDal = async (
topicName: string
topicId: number
): Promise<QuestionOutput[]> => {
const questions = await Question.findAll({
where: { under_which_topic: topicName },
where: { underWhichTopic: topicId },
limit: 20,
});
if (questions.length === 0) throw `questions not found for ${topicName}`;
if (questions.length === 0) throw `questions not found for ${topicId}`;
return questions;
};

export const getQuestionByNameDal = async (
questionName: string
export const getQuestionByIdDal = async (
questionId: number
): Promise<QuestionOutput> => {
const question = await Question.findOne({
where: { question_name: questionName },
});
const question = await Question.findByPk(questionId);
if (!question) throw "Question not found";
return question;
};

export const deleteQuestionByNameDal = async (questionName: string) => {
return await Question.destroy({ where: { question_name: questionName } });
export const deleteQuestionByIdDal = async (questionId: number) => {
return await Question.destroy({ where: { id: questionId } });
};
24 changes: 11 additions & 13 deletions src/DataAccessLayer/subject.dal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ export const createSubjectDal = async (
};

export const updateSubjectDal = async (
subjectName: string,
subjectId: string,
payload: Partial<SubjectInput>
): Promise<SubjectOutput> => {
const subject = await Subject.findOne({
where: { subject_name: subjectName },
where: { id: subjectId },
});
if (!subject) throw "Subject not found";

const updatedSubject = subject.set({
subject_description: payload.subject_description,
image_url: payload.image_url,
subject_name: payload.subject_name,
topic_count: payload.topic_count,
description: payload.description,
imageUrl: payload.imageUrl,
name: payload.name,
topicCount: payload.topicCount,
});
await updatedSubject.save();
return updatedSubject;
Expand All @@ -29,16 +29,14 @@ export const getAllSubjectsDal = async (): Promise<SubjectOutput[]> => {
return await Subject.findAll({ limit: 20 });
};

export const getSubjectByNameDal = async (
subjectName: string
export const getSubjectByIdDal = async (
subjectId: number
): Promise<SubjectOutput> => {
const subject = await Subject.findOne({
where: { subject_name: subjectName },
});
const subject = await Subject.findByPk(subjectId);
if (!subject) throw "Subject not found";
return subject;
};

export const deleteSubjectByNameDal = async (name: string) => {
return await Subject.destroy({ where: { subject_name: name } });
export const deleteSubjectByIdDal = async (subjectId: string) => {
return await Subject.destroy({ where: { id: subjectId } });
};
26 changes: 13 additions & 13 deletions src/DataAccessLayer/topicUnderSubject.dal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,45 @@ export const createTopicUnderSubjectDal = async (
};

export const updateTopicUnderSubjectDal = async (
topicName: string,
topicId: number,
payload: Partial<TopicUnderSubjectInput>
): Promise<TopicUnderSubjectOutput> => {
const topic = await TopicUnderSubject.findOne({
where: { topic_name: topicName },
where: { id: topicId },
});
if (!topic) throw "Topic not found";

const updatedTopic = topic.set({
topic_description: payload.topic_description,
topic_name: payload.topic_name,
question_count: payload.question_count,
under_which_subject: payload.under_which_subject,
description: payload.description,
name: payload.name,
questionCount: payload.questionCount,
underWhichSubject: payload.underWhichSubject,
});
await updatedTopic.save();
return updatedTopic;
};

export const getAllTopicsUnderSubjectDal = async (
subjectName: string
subjectId: number
): Promise<TopicUnderSubjectOutput[]> => {
const topics = await TopicUnderSubject.findAll({
where: { under_which_subject: subjectName },
where: { underWhichSubject: subjectId },
limit: 20,
});
if (topics.length === 0) throw `no topics found for ${subjectName}`;
if (topics.length === 0) throw `no topics found for ${subjectId}`;
return topics;
};

export const getTopicByNameDal = async (
topicName: string
topicId: number
): Promise<TopicUnderSubjectOutput> => {
const question = await TopicUnderSubject.findOne({
where: { topic_name: topicName },
where: { id: topicId },
});
if (!question) throw "Question not found";
return question;
};

export const deleteTopicByNameDal = async (topicName: string) => {
return await TopicUnderSubject.destroy({ where: { topic_name: topicName } });
export const deleteTopicByIdDal = async (topicId: number) => {
return await TopicUnderSubject.destroy({ where: { id: topicId } });
};
6 changes: 2 additions & 4 deletions src/controller/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ async function updateUser(request: Request, response: Response) {
if (!cookies?.jwt) return response.sendStatus(204);
try {
const refreshToken = cookies.jwt;
if (refreshToken.length !== 187) return response.sendStatus(401);
if (!user.verified) {
return successResponse(response, 200, {
message: "Account is not verified!",
Expand Down Expand Up @@ -142,7 +141,6 @@ async function logout(request: Request, response: Response) {

try {
const refreshToken: string = cookies.jwt;
if (refreshToken.length !== 187) return response.sendStatus(401);

let user = await getUserByRefreshTokenDal(refreshToken);
if (!user) {
Expand All @@ -161,11 +159,12 @@ async function logout(request: Request, response: Response) {

async function handleRefreshToken(request: Request, response: Response) {
const cookies = request.cookies;
console.log(cookies);
if (!cookies?.jwt) return response.sendStatus(204);

try {
const refreshToken: string = cookies.jwt;
if (refreshToken.length !== 187) return response.sendStatus(401);
console.log(refreshToken.length);

let user = await getUserByRefreshTokenDal(refreshToken);
if (!user) return response.sendStatus(403);
Expand All @@ -189,7 +188,6 @@ async function getUserRefreshToken(request: Request, response: Response) {

try {
const refreshToken: string = cookies.jwt;
if (refreshToken.length !== 187) return response.sendStatus(401);

let user = await getUserByRefreshTokenDal(refreshToken);
if (!user) return response.sendStatus(404);
Expand Down
30 changes: 17 additions & 13 deletions src/controller/question.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Request, Response } from "express";
import {
createQuestionDal,
deleteQuestionByNameDal,
getQuestionByNameDal,
deleteQuestionByIdDal,
getQuestionByIdDal,
getQuestionsUnderTopicDal,
updateQuestionDal,
} from "../DataAccessLayer/question.dal";
Expand All @@ -20,40 +20,44 @@ async function createQuestion(request: Request, response: Response) {

async function updateQuestion(request: Request, response: Response) {
const body = request.body;
const questionName = request.params.questionName;
try {
const updatedQuestion = await updateQuestionDal(questionName, body);
const updatedQuestion = await updateQuestionDal(
parseInt(body.questionId as string),
body
);
successResponse(response, 201, updatedQuestion);
} catch (error) {
return errorResponse(response, 500, error);
}
}

async function getQuestionsUnderATopic(request: Request, response: Response) {
const topicName = request.params.topicName;
const topicId = request.query.topicId;
try {
const questions = await getQuestionsUnderTopicDal(topicName);
const questions = await getQuestionsUnderTopicDal(
parseInt(topicId as string)
);
successResponse(response, 200, questions);
} catch (error) {
errorResponse(response, 500, error);
}
}

async function getQuestionByName(request: Request, response: Response) {
const questionName = request.params.questionName;
async function getQuestionById(request: Request, response: Response) {
const questionId = request.query.questionId;
try {
const question = await getQuestionByNameDal(questionName);
const question = await getQuestionByIdDal(parseInt(questionId as string));
successResponse(response, 200, question);
} catch (error) {
errorResponse(response, 500, error);
}
}

async function deleteQuestion(request: Request, response: Response) {
const questionName = request.params.questionName;
const questionId = request.body.questionId;
try {
await deleteQuestionByNameDal(questionName);
successResponse(response, 200, "Deleted successfully");
await deleteQuestionByIdDal(parseInt(questionId as string));
successResponse(response, 200, { deleted: questionId });
} catch (error) {
errorResponse(response, 500, error);
}
Expand All @@ -62,7 +66,7 @@ async function deleteQuestion(request: Request, response: Response) {
export default {
createQuestion,
deleteQuestion,
getQuestionByName,
getQuestionById,
getQuestionsUnderATopic,
updateQuestion,
};
Loading

0 comments on commit 1bb945e

Please sign in to comment.