-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #329 from abckhush/h1
added expenseTrackerAPI
- Loading branch information
Showing
11 changed files
with
1,210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Expense Tracker API | ||
|
||
## Description | ||
This API allows users to manage their expenses by providing functionalities to add, update, delete, and retrieve expenses. Users can also register and log in to secure their data. The API supports filtering expenses by various criteria, including month and year. | ||
|
||
## Features | ||
1. User registration and login to secure expense data. | ||
2. Add new expenses. | ||
3. Update existing expenses. | ||
4. Delete expenses. | ||
5. Retrieve expenses by various criteria (e.g., month, year, category). | ||
6. Retrieve expenses for a specific month and year. | ||
|
||
## Requirements | ||
- Node.js | ||
- MongoDB | ||
- Postman | ||
|
||
## Installation | ||
1. Clone the Repository: | ||
|
||
```bash | ||
git clone https://github.com/dishamodi0910/APIVerse.git | ||
cd New_APIs | ||
cd Expense_Tracker_API | ||
``` | ||
|
||
2.Install Dependencies: | ||
|
||
```bash | ||
npm install | ||
``` | ||
4. Start the Server: | ||
|
||
```bash | ||
node server.js | ||
``` | ||
|
||
## API Endpoints: | ||
|
||
1. User Registration | ||
|
||
```http | ||
POST /api/auth/register | ||
``` | ||
|
||
2.User Login | ||
|
||
```http | ||
POST /api/auth/login | ||
``` | ||
|
||
3. Add Expenses | ||
|
||
```http | ||
POST /api/expenses | ||
``` | ||
|
||
4. Update Expenses | ||
|
||
```http | ||
PUT /api/expenses/:id | ||
``` | ||
|
||
5. Delete Expenses | ||
|
||
```http | ||
GET /api/expenses | ||
``` | ||
|
||
6. Get Expenses by Month and Year | ||
|
||
```http | ||
GET /api/expenses/month/:month/year/:year | ||
``` | ||
|
||
## Notes | ||
- Ensure the server is running at 5000. | ||
- Use Postman or similar tools to test and interact with the API endpoints. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const dbPath = path.resolve(__dirname, '../database.json'); | ||
|
||
// Initialize the database file if it does not exist | ||
const initializeDB = () => { | ||
if (!fs.existsSync(dbPath)) { | ||
const initialData = { users: [], expenses: [] }; | ||
fs.writeFileSync(dbPath, JSON.stringify(initialData, null, 2)); | ||
} | ||
}; | ||
|
||
initializeDB(); | ||
|
||
const readData = () => { | ||
const data = fs.readFileSync(dbPath); | ||
return JSON.parse(data); | ||
}; | ||
|
||
const writeData = (data) => { | ||
fs.writeFileSync(dbPath, JSON.stringify(data, null, 2)); | ||
}; | ||
|
||
module.exports = { readData, writeData }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"users": [ | ||
{ | ||
"id": 1721424141648, | ||
"name": "Khushi Kalra", | ||
"email": "[email protected]", | ||
"password": "$2a$10$hfW1tOhVl4JK77JY0uJmSeexuEbWcXU3BFEl0LJYRXR9Z3dIKMBUm" | ||
}, | ||
{ | ||
"id": 1721454350397, | ||
"name": "XYZ", | ||
"email": "[email protected]", | ||
"password": "$2a$10$Rn4gwRZyHjMRdZka23KSXuEP2GAJkGbmUZzH.zhExyiNO3yZOrDmq" | ||
} | ||
], | ||
"expenses": [ | ||
{ | ||
"id": 1721452253157, | ||
"userId": 1721424141648, | ||
"amount": 100, | ||
"description": "Office Supplies", | ||
"category": "Work", | ||
"date": "2024-07-20T05:10:53.157Z" | ||
}, | ||
{ | ||
"id": 1721452284744, | ||
"userId": 1721424141648, | ||
"amount": 50, | ||
"description": "Markers", | ||
"category": "Work", | ||
"date": "2024-07-20T05:11:24.744Z" | ||
}, | ||
{ | ||
"id": 1721452309611, | ||
"userId": 1721424141648, | ||
"amount": 250, | ||
"description": "Outing", | ||
"category": "Food", | ||
"date": "2024-06-20T05:11:49.611Z" | ||
}, | ||
{ | ||
"id": 1721454204902, | ||
"userId": 1721424141648, | ||
"amount": 50, | ||
"description": "Outing", | ||
"category": "Food", | ||
"date": "2024-07-20T05:43:24.902Z" | ||
}, | ||
{ | ||
"id": 1721454410029, | ||
"userId": 1721454350397, | ||
"amount": 150, | ||
"description": "Outing", | ||
"category": "Food", | ||
"date": "2024-07-20T05:46:50.029Z" | ||
}, | ||
{ | ||
"id": 1721454479574, | ||
"userId": 1721454350397, | ||
"amount": 50, | ||
"description": "Outing", | ||
"category": "Food", | ||
"date": "2024-07-20T05:47:59.574Z" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const { readData, writeData } = require('../config/db'); | ||
|
||
const createExpense = (userId, amount, description, category) => { | ||
const data = readData(); | ||
const newExpense = { id: Date.now(), userId, amount, description, category, date: new Date() }; | ||
data.expenses.push(newExpense); | ||
writeData(data); | ||
return newExpense; | ||
}; | ||
|
||
const getExpensesByUserId = (userId) => { | ||
const data = readData(); | ||
return data.expenses.filter(expense => expense.userId === userId); | ||
}; | ||
|
||
const getExpensesByCategory = (userId, category) => { | ||
const data = readData(); | ||
return data.expenses.filter(expense => expense.userId === userId && expense.category === category); | ||
}; | ||
|
||
const getTotalAmountByCategory = (userId, category) => { | ||
const expenses = getExpensesByCategory(userId, category); | ||
return expenses.reduce((total, expense) => total + expense.amount, 0); | ||
}; | ||
|
||
const deleteExpenseById = (id) => { | ||
const data = readData(); | ||
data.expenses = data.expenses.filter(expense => expense.id !== id); | ||
writeData(data); | ||
}; | ||
|
||
const getMonthlyExpenses = (userId) => { | ||
const data = readData(); | ||
|
||
const expensesByMonth = data.expenses | ||
.filter(expense => expense.userId === userId) | ||
.reduce((acc, expense) => { | ||
const expenseDate = new Date(expense.date); | ||
const month = expenseDate.getMonth() + 1; // JavaScript months are 0-based | ||
const year = expenseDate.getFullYear(); | ||
const key = `${year}-${month.toString().padStart(2, '0')}`; | ||
|
||
if (!acc[key]) { | ||
acc[key] = { total: 0, expenses: [] }; | ||
} | ||
acc[key].total += expense.amount; | ||
acc[key].expenses.push(expense); | ||
|
||
return acc; | ||
}, {}); | ||
|
||
return expensesByMonth; | ||
}; | ||
|
||
const getExpensesByMonthYear = (userId, year, month) => { | ||
const data = readData(); | ||
const filteredExpenses = data.expenses.filter(expense => { | ||
const expenseDate = new Date(expense.date); | ||
return expense.userId === userId && expenseDate.getFullYear() === year && (expenseDate.getMonth() + 1) === month; | ||
}); | ||
|
||
const totalAmount = filteredExpenses.reduce((total, expense) => total + expense.amount, 0); | ||
return { totalAmount, expenses: filteredExpenses }; | ||
}; | ||
|
||
module.exports = { | ||
createExpense, | ||
getExpensesByUserId, | ||
getExpensesByCategory, | ||
getTotalAmountByCategory, | ||
deleteExpenseById, | ||
getMonthlyExpenses, | ||
getExpensesByMonthYear | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const { readData, writeData } = require('../config/db'); | ||
|
||
const createUser = (name, email, password) => { | ||
const data = readData(); | ||
const newUser = { id: Date.now(), name, email, password }; | ||
data.users.push(newUser); | ||
writeData(data); | ||
return newUser; | ||
}; | ||
|
||
const getUserByEmail = (email) => { | ||
const data = readData(); | ||
return data.users.find(user => user.email === email); | ||
}; | ||
|
||
module.exports = { createUser, getUserByEmail }; |
Oops, something went wrong.