Merge pull request '[#4] Stubbed out a basic api with mongo connections.' (#5) from 4-stub-api into master
Reviewed-on: #5
2
.gitignore
vendored
@ -1,7 +1,7 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
|
||||
24
client/mongodbnotes.txt
Normal file
@ -0,0 +1,24 @@
|
||||
use budgetdb
|
||||
|
||||
db.createCollection(“users”)
|
||||
db.createCollection(“accounts”)
|
||||
db.createCollection("budgets")
|
||||
db.createCollection("budgetPeriods")
|
||||
|
||||
users: [{
|
||||
_id: <id>
|
||||
username: “maximx1”
|
||||
}]
|
||||
|
||||
accounts: [{
|
||||
name: amazon,
|
||||
user: <id>
|
||||
}]
|
||||
|
||||
budgets: [{
|
||||
name: "main"
|
||||
}]
|
||||
|
||||
budgetPeriods: [{
|
||||
|
||||
}]
|
||||
|
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 5.2 KiB After Width: | Height: | Size: 5.2 KiB |
|
Before Width: | Height: | Size: 9.4 KiB After Width: | Height: | Size: 9.4 KiB |
|
Before Width: | Height: | Size: 615 B After Width: | Height: | Size: 615 B |
|
Before Width: | Height: | Size: 710 B After Width: | Height: | Size: 710 B |
1866
server/package-lock.json
generated
Normal file
18
server/package.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "budget-demo-server",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node src/server.js",
|
||||
"dev": "nodemon src/server.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"mongodb": "^5.1.0",
|
||||
"nodemon": "^2.0.20"
|
||||
}
|
||||
}
|
||||
12
server/src/managers/UserManager.js
Normal file
@ -0,0 +1,12 @@
|
||||
const userStore = require('../stores/UserStore'),
|
||||
accountStore = require('../stores/AccountStore');
|
||||
|
||||
exports.getUserAccounts = (username) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
userStore.findUser(username).then((user) => {
|
||||
accountStore.getAccountsForUser(user._id).then(accounts => {
|
||||
resolve(accounts);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
23
server/src/server.js
Normal file
@ -0,0 +1,23 @@
|
||||
const express = require('express'),
|
||||
port = process.env.PORT || 3001,
|
||||
app = express(),
|
||||
userManager = require('./managers/UserManager'),
|
||||
userStore = require('./stores/UserStore'),
|
||||
accountStore = require('./stores/AccountStore');
|
||||
|
||||
app.get('/users', (req, res) => {
|
||||
userStore.getUsers().then(data => res.json({ users: data }));
|
||||
});
|
||||
|
||||
app.get('/:username/accounts', (req, res) => {
|
||||
const { username } = req.params;
|
||||
userManager.getUserAccounts(username).then(data => res.json(data));
|
||||
});
|
||||
|
||||
app.get('/accounts', (req, res) => {
|
||||
accountStore.getAccounts().then(data => res.json({ accounts: data }));
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`===> Server started: http://localhost:${port}/`);
|
||||
});
|
||||
9
server/src/stores/AccountStore.js
Normal file
@ -0,0 +1,9 @@
|
||||
const mongo = require('./Mongo');
|
||||
|
||||
exports.getAccounts = () => {
|
||||
return mongo.listAll('accounts');
|
||||
};
|
||||
|
||||
exports.getAccountsForUser = (userId) => {
|
||||
return mongo.find('accounts', { user: userId });
|
||||
};
|
||||
44
server/src/stores/Mongo.js
Normal file
@ -0,0 +1,44 @@
|
||||
const { MongoClient } = require('mongodb'),
|
||||
mongoUri = process.env.MONGOURI || 'mongodb://localhost:27017',
|
||||
mongoDb = process.env.MONGODB || 'budgetdb';
|
||||
|
||||
const find = (collectionName, query) => {
|
||||
return new Promise(async(resolve, reject) => {
|
||||
const client = new MongoClient(mongoUri);
|
||||
|
||||
try {
|
||||
const db = client.db(mongoDb),
|
||||
collection = db.collection(collectionName);
|
||||
|
||||
resolve(await collection.find(query).toArray());
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
reject(err);
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
});
|
||||
};
|
||||
exports.find = find;
|
||||
|
||||
exports.findOne = (collectionName, query) => {
|
||||
return new Promise(async(resolve, reject) => {
|
||||
const client = new MongoClient(mongoUri);
|
||||
|
||||
try {
|
||||
const db = client.db(mongoDb),
|
||||
collection = db.collection(collectionName);
|
||||
|
||||
resolve(await collection.findOne(query));
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
reject(err);
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
exports.listAll = (collectionName) => {
|
||||
return find(collectionName);
|
||||
};
|
||||
9
server/src/stores/UserStore.js
Normal file
@ -0,0 +1,9 @@
|
||||
const mongo = require('./Mongo');
|
||||
|
||||
exports.getUsers = () => {
|
||||
return mongo.listAll('users');
|
||||
};
|
||||
|
||||
exports.findUser = (username) => {
|
||||
return mongo.findOne('users', { username: username });
|
||||
}
|
||||