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

check #24

Open
wants to merge 2 commits into
base: master
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
77 changes: 77 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const express = require('express');
const app = express();

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

const expressHandlebars = require('express-handlebars');

const hbs = expressHandlebars.create({
partialsDir: 'views/',
defaultLayout: 'application'
});

app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');

//method methodOverride
const methodOverride = require('method-override');
const getPostSupport = require('express-method-override-get-post-support');

//morgan logging
const morgan = require('morgan');
app.use(morgan('tiny'));

app.use((req, res, next) => {
['query', 'params', 'body'].forEach((key) => {
if (req[key]) {
var capKey = key[0].toUpperCase() + key.substr(1);
var value = JSON.stringify(req[key], null, 2);
console.log(`${ capKey }: ${ value }`);
}
});
// console.log('req.session', req.session)
next();
});

//session
const session = require('express-session');
app.use(
session({
secret: '1234',
resave: false,
saveUninitialized: true
})
);

//routes
const userRouter = require('./routers/users');
app.use('/', userRouter);

const profileRouter = require('./routers/profiles');
app.use('/profiles', profileRouter);

const writingRouter = require('./routers/writings');
app.use('/writings', writingRouter);



//server
var port = process.env.PORT ||
process.argv[2] ||
3000;
var host = 'localhost';


var args;
process.env.NODE_ENV === 'production' ?
args = [port] :
args = [port, host];


args.push(() => {
console.log(`Listening: http://${ host }:${ port }`);
});


app.listen.apply(app, args);
20 changes: 20 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"development": {
"username": "goenkija",
"password": null,
"database": "assignment_okodin_development",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "goenkija",
"password": null,
"database": "assignment_okodin_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"use_env_variable": "DATABASE_URL",
"dialect": "postgres"
}
}
35 changes: 35 additions & 0 deletions migrations/20190406063513-create-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
username: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
profileId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('NOW')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('NOW')
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Users');
}
};
41 changes: 41 additions & 0 deletions migrations/20190406081042-create-profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Profiles', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER
},
writingId: {
type: Sequelize.INTEGER
},
gender: {
type: Sequelize.STRING
},
height: {
type: Sequelize.INTEGER
},
age: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('NOW')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('NOW')
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Profiles');
}
};
41 changes: 41 additions & 0 deletions migrations/20190411092434-create-writing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Writings', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
about: {
type: Sequelize.STRING
},
talents: {
type: Sequelize.STRING
},
favorites: {
type: Sequelize.STRING
},
proposal: {
type: Sequelize.STRING
},
profileId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('NOW')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn('NOW')
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Writings');
}
};
37 changes: 37 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'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 db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

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

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
28 changes: 28 additions & 0 deletions models/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const Profile = sequelize.define('Profile', {
userId: DataTypes.INTEGER,
writingId: DataTypes.INTEGER,
gender: {
type: DataTypes.STRING
},
height: {
type: DataTypes.INTEGER
},
age: {
type: DataTypes.INTEGER
}
}, {});
Profile.associate = function(models) {
// associations can be defined here

Profile.hasOne(models.User, {
foreignKey: "profileId"
});

Profile.hasOne(models.Writing, {
foreignKey: "profileId"
})
};
return Profile;
};
35 changes: 35 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
username: {
type: DataTypes.STRING,
validate: {
notEmpty: {
msg: "Must provide a username"
}
},
unique: {
msg: 'Username already in use'
}
},
email: {
type: DataTypes.STRING,
validate: {
notEmpty: {
isEmail: "Enter a valid email address"
}
},
unique: {
msg: 'Email already in use'
}
},
profileId: DataTypes.INTEGER
}, {});
User.associate = function(models) {
User.hasOne(models.Profile, {
foreignKey: "userId"
});
// associations can be defined here
};
return User;
};
22 changes: 22 additions & 0 deletions models/writing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const Writing = sequelize.define('Writing', {
about: DataTypes.STRING,
talents: DataTypes.STRING,
favorites: DataTypes.STRING,
proposal: DataTypes.STRING,
profileId: DataTypes.INTEGER
}, {});
Writing.associate = function(models) {
// associations can be defined here
// Writing.belongsTo(models.Profile, {
// foreignKey: "writingId"
// })

Writing.hasOne(models.Profile, {
foreignKey: "writingId"
})

};
return Writing;
};
Loading