Skip to content
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
122 changes: 122 additions & 0 deletions Controllers/controller.js
Original file line number Diff line number Diff line change
@@ -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,
};
13 changes: 13 additions & 0 deletions DB/connection.js
Original file line number Diff line number Diff line change
@@ -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;
26 changes: 26 additions & 0 deletions model/model.js
Original file line number Diff line number Diff line change
@@ -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,
};
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
17 changes: 17 additions & 0 deletions router/route.js
Original file line number Diff line number Diff line change
@@ -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;
30 changes: 30 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -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);
});