Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sw/eslint pre push #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"env": {
"browser": true,
"es6": true
},
"extends": "airbnb",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
"no-console": "off"
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ Install the heroku cli if you don't already have it.
```bash
# on mac
brew install heroku/brew/heroku
# on Windows or Linux follow instructions here:
# https://devcenter.heroku.com/articles/heroku-cli

heroku login
```

Expand Down
10 changes: 5 additions & 5 deletions api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ const bodyParser = require('body-parser');
const morgan = require('morgan');
const path = require('path');
const db = require('./models');

const app = express();
const PORT = process.env.PORT || 8000;


// this lets us parse 'application/json' content in http requests
app.use(bodyParser.json())
app.use(bodyParser.json());

// add http request logging to help us debug and audit app use
const logFormat = process.env.NODE_ENV==='production' ? 'combined' : 'dev';
const logFormat = process.env.NODE_ENV === 'production' ? 'combined' : 'dev';
app.use(morgan(logFormat));

// this mounts controllers/index.js at the route `/api`
app.use('/api', require('./controllers'));

// for production use, we serve the static react build folder
if(process.env.NODE_ENV==='production') {
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, '../client/build')));

// all unknown routes should be handed to our react app
app.get('*', function (req, res) {
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../client/build', 'index.html'));
});
}
Expand Down
3 changes: 2 additions & 1 deletion api/controllers/appConfig.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const express = require('express');

const router = express.Router();

router.get('/', (req, res) => {
Expand All @@ -9,4 +10,4 @@ router.get('/', (req, res) => {
});


module.exports = router;
module.exports = router;
3 changes: 2 additions & 1 deletion api/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const express = require('express');

const router = express.Router();


Expand All @@ -12,4 +13,4 @@ router.use('/posts', postsController);
router.use('/application-configuration', appConfigController);


module.exports = router;
module.exports = router;
46 changes: 26 additions & 20 deletions api/controllers/posts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const express = require('express');

const router = express.Router();
const db = require('../models');

const { Post } = db;

// This is a simple example for providing basic CRUD routes for
Expand All @@ -9,27 +11,27 @@ const { Post } = db;
// POST /posts
// GET /posts/:id
// PUT /posts/:id
// DELETE /posts/:id
// DELETE /posts/:id

// There are other styles for creating these route handlers, we typically
// explore other patterns to reduce code duplication.
// TODO: Can you spot where we have some duplication below?


router.get('/', (req,res) => {
router.get('/', (req, res) => {
Post.findAll({})
.then(posts => res.json(posts));
.then((posts) => res.json(posts));
});


router.post('/', (req, res) => {
let { content } = req.body;
const { content } = req.body;

Post.create({ content })
.then(post => {
.then((post) => {
res.status(201).json(post);
})
.catch(err => {
.catch((err) => {
res.status(400).json(err);
});
});
Expand All @@ -38,9 +40,10 @@ router.post('/', (req, res) => {
router.get('/:id', (req, res) => {
const { id } = req.params;
Post.findByPk(id)
.then(post => {
if(!post) {
return res.sendStatus(404);
.then((post) => {
if (!post) {
res.sendStatus(404);
return;
}

res.json(post);
Expand All @@ -51,17 +54,19 @@ router.get('/:id', (req, res) => {
router.put('/:id', (req, res) => {
const { id } = req.params;
Post.findByPk(id)
.then(post => {
if(!post) {
return res.sendStatus(404);
.then((obj) => {
const post = obj;
if (!post) {
res.sendStatus(404);
return;
}

post.content = req.body.content;
post.save()
.then(post => {
res.json(post);
.then((updatedPost) => {
res.json(updatedPost);
})
.catch(err => {
.catch((err) => {
res.status(400).json(err);
});
});
Expand All @@ -71,9 +76,10 @@ router.put('/:id', (req, res) => {
router.delete('/:id', (req, res) => {
const { id } = req.params;
Post.findByPk(id)
.then(post => {
if(!post) {
return res.sendStatus(404);
.then((post) => {
if (!post) {
res.sendStatus(404);
return;
}

post.destroy();
Expand All @@ -82,4 +88,4 @@ router.delete('/:id', (req, res) => {
});


module.exports = router;
module.exports = router;
13 changes: 6 additions & 7 deletions api/models/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');

const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const config = require('../config/config.json')[env];

const db = {};

let sequelize;
Expand All @@ -17,17 +18,15 @@ if (config.use_env_variable) {

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
.filter((file) => (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'))
.forEach((file) => {
// const model = sequelize['import'](path.join(__dirname, file));
const model = sequelize.import(path.join(__dirname, file));
const modelName = model.name.charAt(0).toUpperCase() + model.name.slice(1);
db[modelName] = model;
});

Object.keys(db).forEach(modelName => {
Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
Expand Down
8 changes: 4 additions & 4 deletions api/models/post.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';

const { Model } = require('sequelize');


Expand All @@ -11,16 +11,16 @@ module.exports = (sequelize, DataTypes) => {
validate: {
len: [3, 250],
notEmpty: true,
}
},
},
}, {
sequelize,
modelName: 'post'
modelName: 'post',
});

Post.associate = (models) => {
// associations can be defined here
};

return Post;
};
};
Loading