Skip to content

Commit

Permalink
[backend] Create half-routes for editorials
Browse files Browse the repository at this point in the history
  • Loading branch information
MananGandhi1810 committed Oct 31, 2024
1 parent 4f466bb commit 2297194
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 13 deletions.
4 changes: 4 additions & 0 deletions backend/execution-worker/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const executeFromQueue = async (message, channel) => {
const { code, language, submissionId, problemStatementId, temp } =
JSON.parse(message);
if (
code == "" ||
language == "" ||
submissionId == "" ||
problemStatementId == "" ||
code.trim() == "" ||
language.trim() == "" ||
(submissionId.trim() == "" && !temp) ||
Expand Down
127 changes: 127 additions & 0 deletions backend/handlers/editorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const getEditorials = async (req, res) => {
const { problemStatementId } = req.params;
const editorials = await prisma.editorial.findMany({
where: {
problemStatementId,
hidden: false,
},
select: {
id: true,
title: true,
createdAt: true,
},
});
res.json({
success: true,
message: "Editorials found succesfully",
data: {
editorials,
},
});
};

const getEditorialById = async (req, res) => {
const { problemStatementId, editorialId } = req.params;
const editorial = await prisma.editorial.findUnique({
where: {
problemStatementId,
id: editorialId,
hidden: false,
},
});
if (!editorial) {
return res.status(404).json({
success: false,
message: "Editorial not found",
data: null,
});
}
res.json({
success: true,
message: "Editorial found",
data: {
editorial,
},
});
};

const newEditorial = async (req, res) => {
const { problemStatementId } = req.params;
const { title, content } = req.body;
if (!title || !content || !title.trim() || !content.trim()) {
return res.status(400).json({
success: false,
message: "Title and Content are required",
data: null,
});
}
const successfulSubmissionCount = await prisma.submission.count({
where: {
problemStatementId,
userId: req.user.id,
success: true,
},
});
if (successfulSubmissionCount == 0) {
return res.status(403).json({
success: false,
message:
"You cannot submit an editorial without succesfully solving this problem statement",
data: null,
});
}
const alreadySubmitted = await prisma.editorial.findFirst({
where: {
problemStatementId,
userId: req.user.id,
hidden: false,
},
});
if (alreadySubmitted) {
return res.status(403).json({
success: false,
message: "You can only submit 1 Editorial per Problem Statement",
data: null,
});
}
var editorial;
try {
editorial = await prisma.editorial.create({
data: {
title,
content,
problemStatementId: problemStatementId,
userId: req.user.id,
},
});
} catch (e) {
return res.status(500).json({
success: false,
message: "Could not create editorial",
data: null,
});
}
res.json({
success: true,
message: "Editorial submitted succesfully",
data: {
id: editorial.id,
},
});
};

const deleteEditorial = async (req, res) => {};

const updateEditorial = async (req, res) => {};

export {
getEditorials,
getEditorialById,
newEditorial,
deleteEditorial,
updateEditorial,
};
12 changes: 6 additions & 6 deletions backend/handlers/problem-statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ const newProblemStatementHandler = async (req, res) => {
const { title, description, difficulty, testCases } = req.body;
if (
!title ||
title.trim() == "" ||
!description ||
description.trim() == "" ||
!difficulty ||
!["Easy", "Medium", "Hard"].includes(difficulty.trim()) ||
!testCases ||
title.trim() == "" ||
description.trim() == "" ||
!["Easy", "Medium", "Hard"].includes(difficulty.trim()) ||
testCases.length == 0
) {
return res.status(400).json({
Expand Down Expand Up @@ -132,12 +132,12 @@ const editProblemStatementHandler = async (req, res) => {
const { title, description, difficulty, testCases } = req.body;
if (
!title ||
title.trim() == "" ||
!description ||
description.trim() == "" ||
!difficulty ||
!["Easy", "Medium", "Hard"].includes(difficulty.trim()) ||
!testCases ||
title.trim() == "" ||
description.trim() == "" ||
!["Easy", "Medium", "Hard"].includes(difficulty.trim()) ||
testCases.length == 0
) {
return res.status(400).json({
Expand Down
2 changes: 2 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import codeRouter from "./router/code.js";
import problemStatementRouter from "./router/problem-statement.js";
import leaderboardRouter from "./router/leaderboard.js";
import userRouter from "./router/user.js";
import editorialsRouter from "./router/editorial.js";
import logger from "morgan";

const app = express();
Expand All @@ -23,6 +24,7 @@ app.use("/code", codeRouter);
app.use("/problem-statement", problemStatementRouter);
app.use("/leaderboard", leaderboardRouter);
app.use("/user", userRouter);
app.use("/editorial", editorialsRouter);

app.use(function (req, res, next) {
res.status(404).json({
Expand Down
28 changes: 28 additions & 0 deletions backend/middlewares/problem-statement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const checkProblemStatement = async (req, res, next) => {
const { problemStatementId } = req.params;
if (!problemStatementId) {
return res.status(400).json({
success: false,
message: "Problem Statement Id is required",
data: null,
});
}
const problemStatementExists =
(await prisma.problemStatement.count({
where: { id: problemStatementId },
})) > 0;
if (!problemStatementExists) {
return res.status(404).json({
success: false,
message: "Problem Statement does not exist",
data: null,
});
}
next();
};

export { checkProblemStatement };
4 changes: 1 addition & 3 deletions backend/router/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
verifyOtpHandler,
} from "../handlers/auth.js";

import { checkAuth } from "../middlewares/auth.js";

var router = Router();
const router = Router();

router.post("/register", registerHandler);
router.get("/verify", verifyHandler);
Expand Down
2 changes: 1 addition & 1 deletion backend/router/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
aiHelperHandler,
} from "../handlers/code.js";

var router = Router();
const router = Router();

router.post("/submit/:problemStatementId/:language", checkAuth, (req, res) =>
queueCodeHandler(req, res),
Expand Down
45 changes: 45 additions & 0 deletions backend/router/editorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Router } from "express";
import {
getEditorials,
getEditorialById,
newEditorial,
deleteEditorial,
updateEditorial,
} from "../handlers/editorial.js";
import { checkAuth } from "../middlewares/auth.js";
import { checkProblemStatement } from "../middlewares/problem-statement.js";

const router = Router();

router.get(
"/:problemStatementId/",
checkAuth,
checkProblemStatement,
getEditorials,
);
router.get(
"/:problemStatementId/:editorialId",
checkAuth,
checkProblemStatement,
getEditorialById,
);
router.post(
"/:problemStatementId/new",
checkAuth,
checkProblemStatement,
newEditorial,
);
router.delete(
"/:problemStatementId/:editorialId/delete",
checkAuth,
checkProblemStatement,
deleteEditorial,
);
router.put(
"/:problemStatementId/:editorialId/update",
checkAuth,
checkProblemStatement,
updateEditorial,
);

export default router;
2 changes: 1 addition & 1 deletion backend/router/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getUserPointsHandler,
} from "../handlers/leaderboard.js";

var router = Router();
const router = Router();

router.get("/", getLeaderboardHandler);
router.get("/getUserPoints", checkAuth, getUserPointsHandler);
Expand Down
2 changes: 1 addition & 1 deletion backend/router/problem-statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
deleteProblemStatementHandler,
} from "../handlers/problem-statement.js";

var router = Router();
const router = Router();

router.get("/all", checkAuth, getProblemStatementsHandler);
router.get("/:problemStatementId", checkAuth, getProblemStatementByIdHandler);
Expand Down
2 changes: 1 addition & 1 deletion backend/router/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { userDataHandler, getUserByIdHandler } from "../handlers/user.js";

import { checkAuth } from "../middlewares/auth.js";

var router = Router();
const router = Router();

router.get("/", checkAuth, userDataHandler);
router.get("/:id", getUserByIdHandler);
Expand Down

0 comments on commit 2297194

Please sign in to comment.