Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean-Haverstock committed Jul 26, 2020
2 parents cdb68d3 + fdbc42b commit 22958bd
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
.vscode
.env
Empty file.
20 changes: 20 additions & 0 deletions build/bundle.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion build/bundle.js.map

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "scratch-og",
"name": "bubble-stack",
"version": "1.0.0",
"description": "scratch",
"main": "index.js",
"description": "bubble-stack",
"main": "index.ts",
"scripts": {
"start": "cross-env NODE_ENV=production webpack & cross-env NODE_ENV=production nodemon server/server.js",
"server": "cross-env NODE_ENV=production webpack",
Expand All @@ -21,6 +21,7 @@
"homepage": "https://github.com/Star-Nosed-Mole/Scratch#readme",
"dependencies": {
"@nivo/circle-packing": "^0.62.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"npm-stats-api": "^1.1.3",
"pg": "^8.3.0",
Expand Down
41 changes: 41 additions & 0 deletions server/controllers/stackControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const db = require('../models/stackModels');
const { stack } = require('../routes/api');

const stackController = {};


stackController.getAll = (req, res, next) => {

const queryAll = `SELECT libraries.name, types.name AS type, framework.name AS framework FROM libraries
INNER JOIN types ON libraries.type_id = types.type_id
INNER JOIN framework ON libraries.framework_id = framework.framework_id;`;

db.query(queryAll)
.then(data=> {
res.locals.all = data.rows;
return next();
})
.catch((err) => {
return next(err);
});
}


stackController.findOne = (req, res, next) => {
const name = req.body.name;
const queryOne = `SELECT libraries.name, types.name AS type, framework.name AS framework FROM libraries
INNER JOIN types ON libraries.type_id = types.type_id
INNER JOIN framework ON libraries.framework_id = framework.framework_id
WHERE libraries.name = '${name}'; `;
db.query(queryOne)
.then(data=> {
console.log(data.rows);
res.locals.one = data.rows[0];
return next();
})
.catch((err) => {
return next(err);
});
}

module.exports = stackController;
19 changes: 19 additions & 0 deletions server/models/stackModels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// import PG_URI from '../../.env';
const { Pool } = require('pg');

require("dotenv").config();

// create a new pool here using the connection string above
const pool = new Pool({
connectionString: process.env.PG_URI
});

// We export an object that contains a property called query,
// which is a function that returns the invocation of pool.query() after logging the query
// This will be required in the controllers to be the access point to the database
module.exports = {
query: (text, params, callback) => {
console.log('executed query', text);
return pool.query(text, params, callback);
}
};
19 changes: 19 additions & 0 deletions server/routes/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require('express');
const modelsController = require('../controllers/stackControllers');
const router = express.Router();


router.get('/',
modelsController.getAll,
(req, res) => {
res.status(200).json(res.locals.all);
});

router.post('/one',
modelsController.findOne,
(req, res) => {
res.status(200).json(res.locals.one);
});

module.exports = router;

10 changes: 6 additions & 4 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ const express = require('express');
const app = express();

const path = require('path');
const PORT = 3000;
const apiRouter = require('./routes/api');

const PORT = 3000
app.use(express.json());

app.use(express.json())
// app.use('/', express.static('client'));
// app.use('/build', express.static(path.join(__dirname, '../build')));
// statically serve everything in the build folder on the route '/build
if (process.env.NODE_ENV === 'production') {
app.use('/', express.static('client'));
app.use('/build', express.static(path.join(__dirname, '../build')));
}

app.use('/api', apiRouter);

// global error handler
app.use('*', (req,res) => res.sendStatus(404) )

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"exclude": [
"node_modules"
]
}
}

0 comments on commit 22958bd

Please sign in to comment.