From 4c58636f079317034d3f5f49c7ceba3555d69959 Mon Sep 17 00:00:00 2001 From: zayid Date: Tue, 27 Dec 2022 02:07:01 +0600 Subject: [PATCH 1/2] Completed the expense-express task --- App.js | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++- package.json | 18 ++++++++--- 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/App.js b/App.js index 0c42caf..8fa40a7 100644 --- a/App.js +++ b/App.js @@ -1,3 +1,90 @@ -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} +] + + + +// 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)=>{ + // "ternery if" that finds the last id of expense list , If list is empty the default id would be 1 + let id= expense[expense.length-1].id+1 ? id= expense[expense.length-1].id+1 : 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.amount == req.params.id) + + const updatedExpense= { + id:req.params.id , + description:req.body.description, + amount:req.body.amount + } + expense.splice(index,1,updatedExpense) + + res.status(200).json({editedMessage:expense}) + +}) + +// just get one expense with that specific id passed +app.get("/expense/:id",(req,res)=>{ + const index = expense.findIndex(expense=> expense.amount == 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}) + +}) + +//listening the port +app.listen(port,()=>{ + console.log("App running on the port 8000 ") +}) + diff --git a/package.json b/package.json index 5b73468..6901e2a 100644 --- a/package.json +++ b/package.json @@ -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://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@github.com/zayidmohamedy/expense-express.git" + }, + "bugs": { + "url": "https://github.com/zayidmohamedy/expense-express/issues" + }, + "homepage": "https://github.com/zayidmohamedy/expense-express#readme" } From cf3a959aa5af21ebc2a82c948ba008d894c2c9aa Mon Sep 17 00:00:00 2001 From: zayid Date: Tue, 27 Dec 2022 22:26:23 +0600 Subject: [PATCH 2/2] resolved the expense-express task --- App.js | 57 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/App.js b/App.js index 8fa40a7..5c5496c 100644 --- a/App.js +++ b/App.js @@ -12,16 +12,40 @@ const expense = [ ] +// 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)=>{ - // "ternery if" that finds the last id of expense list , If list is empty the default id would be 1 - let id= expense[expense.length-1].id+1 ? id= expense[expense.length-1].id+1 : 1; + // 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}) @@ -39,6 +63,7 @@ app.delete("/expense/:id",(req,res)=>{ }) + // return the total amount of all expenses app.get("/expense/:total",(req,res)=>{ let total = 0; @@ -51,35 +76,23 @@ app.get("/expense/:total",(req,res)=>{ }) + + // 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.amount == req.params.id) + const index = expense.findIndex(expense=> expense.id == req.params.id) - const updatedExpense= { - id:req.params.id , - description:req.body.description, - amount:req.body.amount - } - expense.splice(index,1,updatedExpense) + expense.splice(index,1,{ + id: parseInt(req.params.id ), + description:req.body.description, + amount:req.body.amount + }) res.status(200).json({editedMessage:expense}) }) -// just get one expense with that specific id passed -app.get("/expense/:id",(req,res)=>{ - const index = expense.findIndex(expense=> expense.amount == 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}) - -}) //listening the port app.listen(port,()=>{