-
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.
[backend] Create half-routes for editorials
- Loading branch information
1 parent
4f466bb
commit 2297194
Showing
11 changed files
with
217 additions
and
13 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,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, | ||
}; |
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
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,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 }; |
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
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,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; |
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
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