diff --git a/Controllers/controller.js b/Controllers/controller.js new file mode 100644 index 0000000..0e2c04b --- /dev/null +++ b/Controllers/controller.js @@ -0,0 +1,122 @@ +const model = require("../model/model.js"); + +// POST : localhost:8080/api/create_categories +async function create_Categories(req, res) { + try { + const Create = new model.Categories({ + // type: req.body.type, + // color: req.body.color + type: "Investment", + color: "#9d4edd", + }); + + await Create.save(); + return res.json(Create); + } catch (error) { + console.error(error); + res.status(500).send({ message: "Error creating category" }); + } +} + +async function get_Categories(req, res) { + try { + let data = await model.Categories.find({}); + let filter = await data.map((v) => + Object.assign({}, { type: v.type, color: v.color }) + ); + return res.json(filter); + } catch (error) { + console.error(error); + res.status(500).send({ message: "Error fetching categories" }); + } +} + +//POST request to api/transaction +async function create_Transaction(req, res) { + if (!req.body) + return res.status(400).send({ message: "POST HTTP Data not provided" }); + try { + let { name, type, amount } = req.body; + + const create = await new model.Transaction({ + name, + type, + amount, + date: new Date(), + }); + + await create.save(); + return res.json(create); + } catch (error) { + console.error(error); + res.status(500).send({ message: "Error creating transaction" }); + } +} + +// get: http://localhost:8080/api/transaction +async function get_Transaction(req, res) { + let data = await model.Transaction.find({}); + return res.json(data); +} + +// delete: http://localhost:8080/api/transaction +async function delete_Transaction(req, res) { + if (!req.body) { + return res.status(400).json({ message: "Request body not Found" }); + } + + try { + // Use await with deleteOne() to handle the promise + await model.Transaction.deleteOne(req.body); + res.json("Record Deleted...!"); + } catch (error) { + console.error(error); + res + .status(500) + .json({ message: "Error while deleting Transaction Record" }); + } +} + +// get: http://localhost:8080/api/labels +async function get_Labels(req, res) { + model.Transaction.aggregate([ + { + $lookup: { + from: "categories", + localField: "type", + foreignField: "type", + as: "categories_info", + }, + }, + { + $unwind: "$categories_info", + }, + ]) + .then((result) => { + let data = result.map((v) => + Object.assign( + {}, + { + _id: v._id, + name: v.name, + type: v.type, + amount: v.amount, + color: v.categories_info["color"], + } + ) + ); + res.json(data); + }) + .catch((error) => { + res.status(400).json("Looup Collection Error"); + }); +} + +module.exports = { + create_Transaction, + get_Transaction, + create_Categories, + get_Categories, + delete_Transaction, + get_Labels, +}; diff --git a/DB/connection.js b/DB/connection.js new file mode 100644 index 0000000..c9b394e --- /dev/null +++ b/DB/connection.js @@ -0,0 +1,13 @@ +const mongoose = require("mongoose"); + +const conn = mongoose + .connect(process.env.ATLAS_URI) + .then((db) => { + console.log("Connected to MongoDB - Finance Tracker"); + return db; + }) + .catch((e) => { + console.log(e); + }); + +module.exports = conn; diff --git a/model/model.js b/model/model.js new file mode 100644 index 0000000..ed47fe4 --- /dev/null +++ b/model/model.js @@ -0,0 +1,26 @@ +const mongoose = require("mongoose"); + +const Schema = mongoose.Schema; + +// categories => field => ['type', 'color'] +const categories_model = new Schema({ + type: { type: String, default: "Investment" }, + color: { type: String, default: "#FCBE44" }, +}); + +// transactions => field => ['name', 'type', 'amount', 'date'] +const transaction_model = new Schema({ + name: { type: String, default: "Anonymous" }, + type: { type: String, default: "Investment" }, + amount: { type: Number }, + date: { type: Date, default: Date.now }, +}); + +const Categories = mongoose.model("categories", categories_model); +const Transaction = mongoose.model("transaction", transaction_model); + +exports.default = Transaction; +module.exports = { + Categories, + Transaction, +}; diff --git a/package-lock.json b/package-lock.json index 904ad37..1299856 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,7 @@ "express-async-handler": "^1.2.0", "jsonwebtoken": "^9.0.2", "mongoose": "^8.0.4", - "nodemon": "^3.0.2", + "nodemon": "^3.1.0", "punycode": "^2.3.1", "react-jwt": "^1.2.0" }, @@ -1325,9 +1325,9 @@ } }, "node_modules/nodemon": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.2.tgz", - "integrity": "sha512-9qIN2LNTrEzpOPBaWHTm4Asy1LxXLSickZStAQ4IZe7zsoIpD/A7LWxhZV3t4Zu352uBcqVnRsDXSMR2Sc3lTA==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.1.0.tgz", + "integrity": "sha512-xqlktYlDMCepBJd43ZQhjWwMw2obW/JRvkrLxq5RCNcuDDX1DbcPT+qT1IlIIdf+DhnWs90JpTMe+Y5KxOchvA==", "dependencies": { "chokidar": "^3.5.2", "debug": "^4", diff --git a/package.json b/package.json index c9669b5..4016ff7 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "express-async-handler": "^1.2.0", "jsonwebtoken": "^9.0.2", "mongoose": "^8.0.4", - "nodemon": "^3.0.2", + "nodemon": "^3.1.0", "punycode": "^2.3.1", "react-jwt": "^1.2.0" }, diff --git a/router/route.js b/router/route.js new file mode 100644 index 0000000..ff88dd6 --- /dev/null +++ b/router/route.js @@ -0,0 +1,17 @@ +const routes = require("express").Router(); +const controller = require("../Controllers/controller"); + +routes + .route("/api/categories") + .post(controller.create_Categories) + .get(controller.get_Categories); + +routes + .route("/api/transaction") + .post(controller.create_Transaction) + .get(controller.get_Transaction) + .delete(controller.delete_Transaction); + +routes.route("/api/labels").get(controller.get_Labels); + +module.exports = routes; diff --git a/server.js b/server.js new file mode 100644 index 0000000..01831ef --- /dev/null +++ b/server.js @@ -0,0 +1,30 @@ +const express = require("express"); +const app = express(); +const cors = require("cors"); + +const dotenv = require("dotenv"); +dotenv.config("./.env"); + +const PORT = process.env.BUDGET_PORT || 5000; + +app.use(cors()); +app.use(express.json()); +app.use(require("./router/route.js")); + +const con = require("./DB/connection.js"); + +con + .then((db) => { + if (!db) return process.exit(1); + + app.listen(PORT, () => { + console.log(`Server is running on port: ${PORT}`); + }); + + app.on("error", (err) => { + console.log(`Server error: ${err}`); + }); + }) + .catch((e) => { + console.log(e); + });