From 13ab4ea6eb9a3b4901520101e6ef802b3bf2e8c2 Mon Sep 17 00:00:00 2001 From: Manan Gandhi Date: Thu, 31 Oct 2024 18:23:30 +0530 Subject: [PATCH] [backend] Created all routes for editorials --- backend/handlers/editorial.js | 96 +++++++++++++++++++++++++++++++++-- 1 file changed, 93 insertions(+), 3 deletions(-) diff --git a/backend/handlers/editorial.js b/backend/handlers/editorial.js index 0d7aa73..872ae7d 100644 --- a/backend/handlers/editorial.js +++ b/backend/handlers/editorial.js @@ -52,7 +52,7 @@ const getEditorialById = async (req, res) => { const newEditorial = async (req, res) => { const { problemStatementId } = req.params; const { title, content } = req.body; - if (!title || !content || !title.trim() || !content.trim()) { + if (!title || !content || title.trim() == "" || content.trim() == "") { return res.status(400).json({ success: false, message: "Title and Content are required", @@ -97,6 +97,9 @@ const newEditorial = async (req, res) => { problemStatementId: problemStatementId, userId: req.user.id, }, + select: { + id: true, + }, }); } catch (e) { return res.status(500).json({ @@ -114,9 +117,96 @@ const newEditorial = async (req, res) => { }); }; -const deleteEditorial = async (req, res) => {}; +const deleteEditorial = async (req, res) => { + const { editorialId } = req.params; + if (!editorialId) { + return res.status(400).json({ + success: false, + message: "Editorial Id is required", + data: null, + }); + } + const editorial = await prisma.editorial.findUnique({ + where: { id: editorialId, userId: req.user.id }, + }); + if (!editorial) { + return res.status(404).json({ + success: false, + message: "Editorial not found", + data: null, + }); + } + try { + await prisma.editorial.delete({ + where: { id: editorialId }, + }); + } catch (e) { + return res.status(500).json({ + success: false, + message: "Could not delete editorial", + data: null, + }); + } + res.json({ + success: true, + message: "Editorial delete successfully", + data: null, + }); +}; -const updateEditorial = async (req, res) => {}; +const updateEditorial = async (req, res) => { + const { editorialId } = req.params; + if (!editorialId) { + return res.status(400).json({ + success: false, + message: "Editorial Id is required", + data: null, + }); + } + const editorial = await prisma.editorial.findUnique({ + where: { id: editorialId, userId: req.user.id }, + }); + if (!editorial) { + return res.status(404).json({ + success: false, + message: "Editorial not found", + data: null, + }); + } + 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, + }); + } + var updatedEditorial; + try { + updatedEditorial = await prisma.editorial.update({ + where: { + id: editorialId, + }, + data: { title, content }, + select: { + id: true, + }, + }); + } catch (e) { + res.status(500).json({ + success: false, + message: "Could not update editorial", + data: null, + }); + } + res.json({ + success: true, + message: "Editorial updated successfully", + data: { + id: updatedEditorial.id, + }, + }); +}; export { getEditorials,