Skip to content

add like feature #34

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"editor.fontSize": 38,
"terminal.integrated.fontSize": 60
"editor.fontSize": 15,
"terminal.integrated.fontSize": 10
}
2 changes: 1 addition & 1 deletion config/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PORT = 2121
DB_STRING = mongodb+srv://demo:demo@cluster0.nfzrd.mongodb.net/todolist?retryWrites=true&w=majority
DB_STRING = mongodb+srv://cheekygerold:database8@cluster0.5ojtsvw.mongodb.net/?retryWrites=true&w=majority
13 changes: 12 additions & 1 deletion controllers/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
},
createTodo: async (req, res)=>{
try{
await Todo.create({todo: req.body.todoItem, completed: false})
await Todo.create({todo: req.body.todoItem, completed: false, likes:0})
console.log('Todo has been added!')
res.redirect('/todos')
}catch(err){
Expand Down Expand Up @@ -50,5 +50,16 @@ module.exports = {
}catch(err){
console.log(err)
}
},
addLike: async (req,res)=>{
try {
await Todo.findOneAndUpdate({_id:req.body.todoIdFromJSFile},{
likes: +req.body.likesFromJSFile + 1
})
console.log('Added Like');
res.json('Added Like')
} catch (err) {
console.log(err)
}
}
}
4 changes: 4 additions & 0 deletions models/Todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const TodoSchema = new mongoose.Schema({
completed: {
type: Boolean,
required: true,
},
likes:{
type: Number,
required:true,
}
})

Expand Down
25 changes: 25 additions & 0 deletions public/js/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const deleteBtn = document.querySelectorAll('.del')
const todoItem = document.querySelectorAll('span.not')
const todoComplete = document.querySelectorAll('span.completed')
const likeItem = document.querySelectorAll('.like')

Array.from(deleteBtn).forEach((el)=>{
el.addEventListener('click', deleteTodo)
Expand All @@ -14,6 +15,30 @@ Array.from(todoComplete).forEach((el)=>{
el.addEventListener('click', markIncomplete)
})

Array.from(likeItem).forEach((el)=>{
el.addEventListener('click', addLike)
})

async function addLike(){
const todoId = this.parentNode.dataset.id
const likeId = this.parentNode.dataset.likes
try{
const response = await fetch('todos/addLike', {
method: 'put',
headers: {'Content-type': 'application/json'},
body: JSON.stringify({
'todoIdFromJSFile': todoId,
'likesFromJSFile' : likeId,
})
})
const data = await response.json()
console.log(data)
location.reload()
}catch(err){
console.log(err)
}
}

async function deleteTodo(){
const todoId = this.parentNode.dataset.id
try{
Expand Down
2 changes: 2 additions & 0 deletions routes/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ router.put('/markComplete', todosController.markComplete)

router.put('/markIncomplete', todosController.markIncomplete)

router.put('/addLike', todosController.addLike)

router.delete('/deleteTodo', todosController.deleteTodo)

module.exports = router
5 changes: 4 additions & 1 deletion views/todos.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
<h1>Todos</h1>
<ul>
<% todos.forEach( el => { %>
<li class='todoItem' data-id='<%=el._id%>'>
<li class='todoItem' data-id='<%=el._id%>' data-likes="<%=el.likes%>">
<span class='<%= el.completed === true ? 'completed' : 'not'%>'><%= el.todo %></span>
<span class='del'> Delete </span>
<span><%= el.likes %></span>
<span class = 'like'>Like</span>
</li>

<% }) %>
</ul>

Expand Down