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

okodin #20

Open
wants to merge 11 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
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
POSTGRES_USERNAME = stevenli
POSTGRES_PASSWORD = joe
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
.env
47 changes: 47 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var express = require("express");
var path = require("path");
var favicon = require("serve-favicon");
var logger = require("morgan");
var cookieParser = require("cookie-parser");
var bodyParser = require("body-parser");

var index = require("./routes/index");
var login = require("./routes/login");
var users = require("./routes/users");

var app = express();

// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "hbs");

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

app.use("/", login);
app.use("/users", users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error("Not Found");
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};

// render the error page
res.status(err.status || 500);
res.render("error");
});

module.exports = app;
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('okodin!:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
25 changes: 25 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require("dotenv").config();

module.exports = {
development: {
username: process.env.POSTGRES_USERNAME,
password: process.env.POSTGRES_PASSWORD,
database: "okodin_development",
host: "127.0.0.1",
dialect: "postgres"
},
test: {
username: process.env.POSTGRES_USERNAME,
password: process.env.POSTGRES_PASSWORD,
database: "okodin_test",
host: "127.0.0.1",
dialect: "postgres"
},
production: {
username: process.env.POSTGRES_USERNAME,
password: process.env.POSTGRES_PASSWORD,
database: "okodin_production",
host: "127.0.0.1",
dialect: "postgres"
}
};
35 changes: 35 additions & 0 deletions migrations/20171205163557-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
},
profileId: {
type: Sequelize.INTEGER
},
username: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
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");
}
};
74 changes: 74 additions & 0 deletions migrations/20171205174741-create-profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"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
},
pets: {
type: Sequelize.STRING
},
education: {
type: Sequelize.STRING
},
age: {
type: Sequelize.INTEGER
},
distance: {
type: Sequelize.INTEGER
},
city: {
type: Sequelize.STRING
},
talents: {
type: Sequelize.STRING
},
favorites: {
type: Sequelize.STRING
},
kids: {
type: Sequelize.BOOLEAN
},
whyyoushouldmessageme: {
type: Sequelize.STRING
},
gender: {
type: Sequelize.STRING
},
martialstatus: {
type: Sequelize.STRING
},
bodytype: {
type: Sequelize.STRING
},
heightfeet: {
type: Sequelize.INTEGER
},
heightinches: {
type: Sequelize.INTEGER
},
occupation: {
type: Sequelize.STRING
},
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");
}
};
36 changes: 36 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(__filename);
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.js')[env];
var db = {};

if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
var 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 => {
var 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;
30 changes: 30 additions & 0 deletions models/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";
module.exports = (sequelize, DataTypes) => {
var Profile = sequelize.define("Profile", {
userId: DataTypes.INTEGER,
pets: DataTypes.STRING,
education: DataTypes.STRING,
age: DataTypes.INTEGER,
distance: DataTypes.INTEGER,
city: DataTypes.STRING,
talents: DataTypes.STRING,
favorites: DataTypes.STRING,
kids: DataTypes.BOOLEAN,
whyyoushouldmessageme: DataTypes.STRING,
gender: DataTypes.STRING,
martialstatus: DataTypes.STRING,
bodytype: DataTypes.STRING,
heightfeet: DataTypes.INTEGER,
heightinches: DataTypes.INTEGER,
occupation: DataTypes.STRING
});

Profile.associate = function(models) {
// associations can be defined here
Profile.hasOne(models.User, {
foreignKey: "profileId"
});
};

return Profile;
};
17 changes: 17 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";
module.exports = (sequelize, DataTypes) => {
var User = sequelize.define("User", {
profileId: DataTypes.INTEGER,
username: DataTypes.STRING,
email: DataTypes.STRING
});

User.associate = function(models) {
// associations can be defined here
User.hasOne(models.Profile, {
foreignKey: "userId"
});
};

return User;
};
Loading