Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confirmation Page for Blog Post Deletion #95

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions server/models/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ const PostSchema = new Schema({
updatedAt: {
type: Date,
default: Date.now
}
},
likes: { // Counter for the number of likes
type: Number,
default: 0
},
likedBy: [{ // Array of user IDs who liked the post, to prevent duplicate likes
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}]
});

module.exports = mongoose.model('Post', PostSchema);
module.exports = mongoose.model('Post', PostSchema);
14 changes: 14 additions & 0 deletions server/routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ router.delete('/delete-post/:id', authMiddleware, async (req, res) => {
}
});

// POST: Add a like to a post
app.post('/posts/:id/like', async (req, res) => {
const postId = req.params.id;
const userId = req.user.id; // Assume user is authenticated

const post = await Post.findById(postId);
if (!post.likedBy.includes(userId)) {
post.likes += 1;
post.likedBy.push(userId);
await post.save();
}
res.status(200).json(post);
});

/**
* POST /register
* Admin Registration Route
Expand Down
12 changes: 12 additions & 0 deletions server/routes/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ router.post('/send-message', async (req, res) => {
}
});

const handleLike = async () => {
if (!liked) {
await axios.post(`/posts/${postId}/like`);
} else {
await axios.delete(`/posts/${postId}/like`);
}
setLiked(!liked); // Update UI state
fetchPostData(); // Re-fetch or update post data
};
<span>{post.likes} Reactions</span>


// function insertPostData() {
// Post.insertMany([
// {
Expand Down
59 changes: 59 additions & 0 deletions views/admin/delete-post.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Delete Post</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.confirmation-box {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
.btn {
margin-top: 10px;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.btn-delete {
background-color: #ff5f6d;
color: white;
}
.btn-delete:hover {
background-color: #ff4e50;
}
.btn-cancel {
background-color: #ccc;
color: #333;
}
.btn-cancel:hover {
background-color: #bbb;
}
</style>
</head>
<body>

<div class="confirmation-box">
<h2>Are you sure you want to delete this post?</h2>
<p>This action cannot be undone.</p>
<form action="/delete-post/<%= data._id %>" method="POST" onsubmit="return confirm('Are you sure you want to delete this post?');">
<button type="submit" class="btn btn-delete">Delete</button>
<a href="/dashboard" class="btn btn-cancel">Cancel</a>
</form>
</div>

</body>
</html>