Skip to content

Instantly share code, notes, and snippets.

@laere
Last active February 15, 2019 22:23
Show Gist options
  • Save laere/db38c5740d1816bc1d1706a0bb1a131e to your computer and use it in GitHub Desktop.
Save laere/db38c5740d1816bc1d1706a0bb1a131e to your computer and use it in GitHub Desktop.
const mongoose = require('mongoose');
const { Schema } = mongoose;
const TransactionSchema = require('./Transaction');
const budgetSchema = new Schema({
title: String,
description: String,
amount: { type: Number, default: 0 },
startDate: Date,
endDate: Date,
transactions: [TransactionSchema],
_user: { type: Schema.Types.ObjectId, ref: 'User' },
dateCreated: Date
})
mongoose.model('budgets', budgetSchema);
const mongoose = require('mongoose');
const requireLogin = require('../middlewares/requireLogin');
const Budget = mongoose.model('budgets');
module.exports = app => {
// get budgets
app.get('/api/budgets', requireLogin, async (req, res) => {
const budgets = await Budget.find({ _user: req.user.id });
res.send(budgets);
});
app.post('/api/budgets', requireLogin, async (req, res) => {
const { title, description, amount, startDate, endDate, transactions } = req.body;
const budget = new Budget({
title,
description,
amount,
startDate,
endDate,
transactions,
_user: req.user.id,
dateCreated: Date.now()
});
try {
await budget.save();
req.user.totalBalance += parseFloat(amount);
const user = await req.user.save();
res.send(user);
} catch (err) {
res.status(422).send(err)
}
});
app.delete('/api/budgets', requireLogin, async (req, res) => {
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment