Last active
February 15, 2019 22:23
-
-
Save laere/db38c5740d1816bc1d1706a0bb1a131e to your computer and use it in GitHub Desktop.
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
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); |
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
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