Newspaper Web App: A dynamic online news platform with roles for guests, subscribers, writers, editors, and admins. Features include full-text search, category/tag filters, premium article access, WYSIWYG editor, and secure authentication using Express.js, TypeScript, MongoDB, and EJS
Implement a route to handle form submission for updating the draft or rejected article.
Validate the form input and save the changes to the database.
Update the article's status (if needed), such as changing a draft to "re-submitted" or updating the rejected article with feedback.
Example route to handle article updates:
app.post('/writer/articles/edit/:id', checkAuthentication, async (req, res) => {
const { title, summary, content, category, tags, youtubeLink } = req.body;
const article = await Article.findById(req.params.id);
if (!article || article.author.toString() !== req.user.id) {
return res.status(403).send('Forbidden');
}
// Validate input
if (!title || !summary || !content) {
return res.status(400).send('All fields are required');
}
// Update article fields
article.title = title;
article.summary = summary;
article.content = content;
article.category = category;
article.tags = tags.split(',').map(tag => tag.trim());
article.youtubeLink = youtubeLink;
// Handle file upload if new image is provided
if (req.files && req.files.thumbnail) {
const thumbnailPath = await uploadImage(req.files.thumbnail); // Define an uploadImage function to handle file upload
article.thumbnail = thumbnailPath;
}
// Update the article status if required (e.g., from draft to re-submitted)
if (article.status === 'rejected') {
article.status = 'draft'; // Reset status if the article was rejected
}
await article.save();
res.redirect(`/writer/articles/drafts/edit/${article._id}`);
});
File Upload Handling
Implement file upload logic to allow writers to upload new images or videos (e.g., YouTube links) during editing. For file uploads, use a library like multer in Express.
Implement file validation to ensure the uploaded image is of a valid type and size.
Ensure that all form data is validated before saving, especially the required fields (e.g., title, summary, content).
Handle any errors gracefully, such as missing fields or invalid input.
Update Article Status (Optional)
If an article has been rejected, mark the article as "re-submitted" after the writer makes changes. This step ensures the editor or admin knows that the article has been edited and is ready for re-evaluation.
Example status change logic:
if (article.status === 'rejected') {
article.status = 're-submitted'; // Mark as re-submitted after editing
}
Deliverables
Edit Routes: Routes for editing draft and rejected articles.
Edit Form: Functional form for editing articles, pre-populated with existing article data.
Form Validation: Input validation before saving the article changes to the database.
File Upload: Handling of image uploads and other media (e.g., YouTube links).
Article Status Update: Logic to update the article's status after editing (e.g., marking rejected articles as "re-submitted").
Error Handling: Graceful handling of errors during the editing process.
Acceptance Criteria
Editable Articles: Writers can edit their draft and rejected articles.
Validation: The system validates the form inputs before saving the article.
File Upload: Writers can upload new images or video links during editing.
Status Update: Rejected articles are marked as "re-submitted" after editing.
No Unauthorized Access: Only the article's author can edit their drafts or rejected articles.
UI: The UI should allow writers to easily edit and save changes to their articles.
Objective
Implement functionality that allows writers to edit articles that are in the rejected or draft status.
Requirements
Create Edit Routes for Rejected/Draft Articles
Example route structure in Express:
Create Article Edit Form
Example edit form template (EJS):
Handle Form Submission for Editing Articles
Example route to handle article updates:
File Upload Handling
multer
in Express.Example file upload with
multer
:Validate and Save Changes
Update Article Status (Optional)
Example status change logic:
Deliverables
Acceptance Criteria