Skip to content
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": 42,
"terminal.integrated.fontSize": 62
"editor.fontSize": 12,
"terminal.integrated.fontSize": 12
}
61 changes: 38 additions & 23 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ app.use(express.urlencoded({ extended: true }))
app.use(express.json())


app.get('/',async (request, response)=>{
const todoItems = await db.collection('todos').find().toArray()
const itemsLeft = await db.collection('todos').countDocuments({completed: false})
response.render('index.ejs', { items: todoItems, left: itemsLeft })
// db.collection('todos').find().toArray()
// .then(data => {
// db.collection('todos').countDocuments({completed: false})
// .then(itemsLeft => {
// response.render('index.ejs', { items: data, left: itemsLeft })
// })
// })
// .catch(error => console.error(error))
app.get('/',/*async*/ (request, response)=>{
// const todoItems = await db.collection('todos').find().toArray()
// const itemsLeft = await db.collection('todos').countDocuments({completed: false})
// response.render('index.ejs', { items: todoItems, left: itemsLeft })
db.collection('todos').find().toArray()
.then(data => {
db.collection('todos').countDocuments({completed: false})
.then(itemsLeft => {
response.render('index.ejs', { items: data, left: itemsLeft })
})
})
.catch(error => console.error(error))
})

app.post('/addTodo', (request, response) => {
Expand All @@ -44,42 +44,54 @@ app.post('/addTodo', (request, response) => {
.catch(error => console.error(error))
})

// Mark a todo as complete
app.put('/markComplete', (request, response) => {
db.collection('todos').updateOne({thing: request.body.itemFromJS},{

// FILTER: find the todo whose "thing" text matches what the browser sent us.
// main.js sent it as { itemFromJS: 'the text' } in the request body.
db.collection('todos').updateOne({thing: request.body.itemFromJS},
{
$set: {
completed: true
completed: true // set completed -> true
}
},{
},
{ // prefer newest match; don’t create if missing
sort: {_id: -1},
upsert: false
})
.then(result => {
console.log('Marked Complete')
response.json('Marked Complete')
response.json('Marked Complete') // reply to the client
})
.catch(error => console.error(error))

})

// Mark a todo as NOT complete
app.put('/markUnComplete', (request, response) => {
db.collection('todos').updateOne({thing: request.body.itemFromJS},{
db.collection('todos').updateOne(

// Find the doc by its text again.
{thing: request.body.itemFromJS},{
$set: {
completed: false
completed: false // set completed -> false
}
},{
},{ // prefer newest match; don’t create if missing
sort: {_id: -1},
upsert: false
})
.then(result => {
console.log('Marked Complete')
response.json('Marked Complete')
response.json('Marked Complete') // reply to the client
})
.catch(error => console.error(error))

})

// Delete a todo
app.delete('/deleteItem', (request, response) => {
db.collection('todos').deleteOne({thing: request.body.itemFromJS})

// Remove exactly ONE document whose "thing" matches the text we got from the browser.
db.collection('todos').deleteOne({thing: request.body.itemFromJS})
.then(result => {
console.log('Todo Deleted')
response.json('Todo Deleted')
Expand All @@ -88,6 +100,9 @@ app.delete('/deleteItem', (request, response) => {

})

// Start server (use provider’s PORT if available)
app.listen(process.env.PORT || PORT, ()=>{
console.log(`Server running on port ${PORT}`)
})
})


2 changes: 2 additions & 0 deletions views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@
<script src='js/main.js'></script>
</body>
</html>