Skip to content

Commit

Permalink
[backend] Created all routes for editorials
Browse files Browse the repository at this point in the history
  • Loading branch information
MananGandhi1810 committed Oct 31, 2024
1 parent 3a993f0 commit 13ab4ea
Showing 1 changed file with 93 additions and 3 deletions.
96 changes: 93 additions & 3 deletions backend/handlers/editorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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({
Expand All @@ -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,
Expand Down

0 comments on commit 13ab4ea

Please sign in to comment.