FinanceBuddy.mp4
stockpredictor.mov
- Importing Mongoose library with an object (ODM), allowing to define schemas, models, and interact with Node.js.
- Connect your database to the MongoDB URL stored in the hidden .env file.
- Export the database so it can be used in other modules of the application.
- Parameters (request, response)
- Destructure title, amount, category, description from req.body
- Define IncomeSchema including all the requisites
- Implement Try & Catch for errors:
- If title, category, description, date are missing: throw error
- Ensure amount is a number
- Await income.save() and handle success or error
- Parameters (request, response)
- Implement Try & Catch for errors
- Fetch incomes using IncomeSchema.find().sort({createdAt: -1})
- Pattern to fetch records in reverse chronological order, with the most recent ones first.
- Parameters (request, response)
- Extract id parameter
- Use Mongoose's findByIdAndDelete() based on the id
- Handle successful operation with .then()
- Catch and handle errors with .catch()
- Import functions responsible for handling HTTP requests related to expenses/income.
- Set up Express's Router.
- const router = require('express').Router();
- Export the configured router for use in other parts of the app.
- Use the middleware for parsing JSON body and add cors to allow request in the frontend URL.
- Dynamic route mounting
- Allow access to the backend
cd backend
npm start
- Allow access to the frontend
cd frontend
npm start
- For this I use the globalContext.js Here I track different functions with the base URL of my backend route.
- I use the library of axios to POST, GET, DELETE elements.
- Sends an HTTP POST request to the specified URL ${BASE_URL}add-income, including the income data.
- Sets up an error handling mechanism using .catch() to handle any errors that occur during the request, updating the error state with the error message from the response if an error occurs.
- Calls the getIncomes() function after the request completes successfully, to fetch and update the list of incomes to reflect the newly added income.
- Sends an HTTP GET request to the specified URL ${BASE_URL} get-incomes, including the income data.
- Sends an HTTP DELETE request to the specified URL ${BASE_URL} delete-income and grabs the id that needs to be deleted.
- Calls the getIncomes() function after the request completes successfully, to fetch and update the list of incomes to reflect the newly deleted income.
- It iterates over each income entry in the incomes/expenses array using the forEach() method.
- For each income/expense entry, it adds the amount to the totalIncome variable.
- After iterating through all income entries, it returns the calculated totalIncome/totalExpenses, representing the sum of all income amounts in the array.
- For this function I only return the totalIncome() - totalExpenses()
- Generates a summary of the transaction history by combining income and expense records.
- Sorting them based on their creation dates in descending order. This is executed by the comparator function that compares 2 transactions a & b by their creation dates.
- Using splice to return the three most recent transactions.