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

Completed the hard-Todo App server #330

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
111 changes: 95 additions & 16 deletions 02-nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions 02-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"testTimeout": 10000
},
"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.18.2",
"fs": "^0.0.1-security",
"uuid": "^9.0.0"
}
}
92 changes: 92 additions & 0 deletions 02-nodejs/todoServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,101 @@
*/
const express = require('express');
const bodyParser = require('body-parser');
const fs= require('fs');

const app = express();
const port= 3000;

app.use(bodyParser.json());

function findIdIdx(arr, id){
for(let i=0; i<arr.length; i++){
if(arr[i].id === id){
return i;
}
}
return -1
}

app.get("/todos", (req, res) => {
//Retrieve all todo items
fs.readFile("./todos.json", "utf-8", (err, data) => {
if(err) res.send(err)
res.status(200).send(data)
})
})

app.get("/todos/:id", (req, res) => {
//Retrieve a specific todo item by ID

fs.readFile("./todos.json", "utf-8", (err, data) => {
if(err) res.status(500).send("Error loading items")

const todoItems= JSON.parse(data)
const result= findIdIdx(todoItems, parseInt(req.params.id))

if(result === -1)
res.status(404).send(`ID not found!`)
else
res.status(200).json(todoItems[result])
})
})

app.post("/todos", (req, res) => {
//Create a new todo item
const newTodo= req.body
fs.readFile("./todos.json", "utf-8", (err, data) => {
if(err) res.status(500).send("file note open")
var todoItems= JSON.parse(data)
todoItems.push(newTodo)

fs.writeFile("./todos.json", JSON.stringify(todoItems), "utf-8", (wrErr) => {
if(wrErr) res.status(500).send("Error: Item not Added")
res.status(201).send(`Success: Item added ${JSON.stringify(newTodo)}`)
})
})
})

app.put("/todos/:id", (req, res) => {
//Update an existing todo item by ID
const id= parseInt(req.params.id)
const newTodo= req.body
fs.readFile("./todos.json", "utf-8", (err, data) => {
if(err) res.status(500).send("file not open")
var todoItems= JSON.parse(data)
const idx= findIdIdx(todoItems, id)
if(idx === -1)
res.status(404).send("Item ")
todoItems.splice(idx, 1)
todoItems.push(newTodo)

fs.writeFile("./todos.json", JSON.stringify(todoItems), (wrErr) => {
if(wrErr) res.status(500).send("error: file not updated!")
res.status(200).send("Success: Item was found and updated")
})
})
})

app.delete("/todos/:id", (req, res) => {
//Delete a todo item by ID
const id= parseInt(req.params.id)
fs.readFile("./todos.json", "utf-8", (err, data) => {
if(err) res.status(500).send("file not open")
var todoItems= JSON.parse(data)
const idx= findIdIdx(todoItems, id)
if(idx === -1)
res.status(404).send("Item ")
todoItems.splice(idx, 1)

fs.writeFile("./todos.json", JSON.stringify(todoItems), (wrErr) => {
if(wrErr) res.status(500).send("error: file not updated!")
res.status(200).send("Success: Item was Deleted")
})
})
})

app.listen(port, () => {
console.log(`listning on port ${port}`)
})

module.exports = app;
32 changes: 32 additions & 0 deletions 02-nodejs/todos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"id": 1,
"title": "homework",
"completed": false,
"desc": "complete your homework before evening."
},
{
"id": 2,
"title": "clean the house",
"completed": false,
"desc": "clean the all rooms except first floor bedroom."
},
{
"id": 3,
"todo": "do laundry",
"completed": false,
"desc": "wash clothes and dry them"
},
{
"id": 5,
"todo": "learn Backend Development",
"completed": true,
"desc": "complete the harkirat's course and practice new things"
},
{
"id": 6,
"todo": "Go to gym",
"completed": false,
"desc": "complete the leg day challenge"
}
]