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 expense-express task #2

Open
wants to merge 2 commits 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
102 changes: 101 additions & 1 deletion App.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,103 @@
const expense = [{id:1,description:"Food",amount:35}]
const express= require("express")
const app = express()
const port = 8000;

// middleware
app.use(express.json())

const expense = [
{id:1,description:"Food",amount:35},
{id:2,description:"internet bill",amount:25},
{id:3,description:"water",amount:10}
]


// just get one expense with that specific id passed
app.get("/expense/:id",(req,res)=>{
const index = expense.findIndex(expense=> expense.id == req.params.id)

const getSpecificExp= {
id:req.params.id ,
description: expense[index].description ,
amount:expense[index].amount
}

console.log(getSpecificExp)
res.status(200).json({Message: getSpecificExp})

})




// get the list of expense
app.get("/expense",(req,res)=>{
res.status(200).json({found:expense.length, list:expense})
})



// added new item in expense list
app.post("/expense",(req,res)=>{
// if operation that finds the last id of expense list , If list is empty the default id would be 1
let id;
if(id >= 0 ){
id= expense[expense.length-1].id+1
}else{
id =1;
}

expense.push({id:id ,description:req.body.description ,amount:req.body.amount})
res.status(201).json({message:expense})
})




//deleted the specific expense where the id is passed
app.delete("/expense/:id",(req,res)=>{

const remove = req.params.id -1
expense.splice(remove,1)
res.status(200).json({deletedMessage:expense})
})



// return the total amount of all expenses
app.get("/expense/:total",(req,res)=>{
let total = 0;
let expenseSum= 0;
expense.forEach(element => {
expenseSum += element.amount;
});
total = total + expenseSum;
res.status(200).json({totalExpenses:total})

})



// we edit the specific expense and the body should include description or amount or both
app.put("/expense/:id",(req,res)=>{
const index = expense.findIndex(expense=> expense.id == req.params.id)

expense.splice(index,1,{
id: parseInt(req.params.id ),
description:req.body.description,
amount:req.body.amount
})

res.status(200).json({editedMessage:expense})

})



//listening the port
app.listen(port,()=>{
console.log("App running on the port 8000 ")
})



18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
{
"name": "expense-express",
"version": "1.0.0",
"description": "",
"description": "Your are required to create some endpoints and each endpoint must do a specific task. This project you will be adding, removing, editing and getting expenses data. Below are the endpoints and their methods that are required:",
"main": "App.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node App.js"
},
"keywords": [],
"author": "",
"author": "zayid mohamed",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
}
},
"devDependencies": {},
"repository": {
"type": "git",
"url": "git+https://[email protected]/zayidmohamedy/expense-express.git"
},
"bugs": {
"url": "https://github.com/zayidmohamedy/expense-express/issues"
},
"homepage": "https://github.com/zayidmohamedy/expense-express#readme"
}