[#4] Stubbed out a basic api with mongo connections. #5

Merged
maximx1 merged 1 commits from 4-stub-api into master 2023-02-25 22:20:37 -05:00
35 changed files with 2006 additions and 1 deletions
Showing only changes of commit 8d27c6fffa - Show all commits

2
.gitignore vendored
View File

@ -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
View 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: [{
}]

View File

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

View File

Before

Width:  |  Height:  |  Size: 5.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

Before

Width:  |  Height:  |  Size: 9.4 KiB

After

Width:  |  Height:  |  Size: 9.4 KiB

View File

Before

Width:  |  Height:  |  Size: 615 B

After

Width:  |  Height:  |  Size: 615 B

View File

Before

Width:  |  Height:  |  Size: 710 B

After

Width:  |  Height:  |  Size: 710 B

1866
server/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

18
server/package.json Normal file
View 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"
}
}

View 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
View 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}/`);
});

View File

@ -0,0 +1,9 @@
const mongo = require('./Mongo');
exports.getAccounts = () => {
return mongo.listAll('accounts');
};
exports.getAccountsForUser = (userId) => {
return mongo.find('accounts', { user: userId });
};

View 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);
};

View File

@ -0,0 +1,9 @@
const mongo = require('./Mongo');
exports.getUsers = () => {
return mongo.listAll('users');
};
exports.findUser = (username) => {
return mongo.findOne('users', { username: username });
}