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

WikaSilo #8

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
10 changes: 10 additions & 0 deletions addresses.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1,711-2880 Nulla St, Mississippi, 96522
2,8562 Fusce Rd, Nebraska, 20620
3,606-3727 Ullamcorper Street, Roseville, 11523
4,867-859 Sit Rd, New York, 39531
5,7292 Dictum Av, San Antonio, 47096
6,651-8679 Sodales Av, Tamuning, 10855
7,191-103 Integer Rd, Corona New Mexico, 08219
8,2508 Dolor. Av, Muskegon KY, 12482
9,666-4366 Lacinia Avenue, Ohio, 19253
10,Lacinia Road, San Bernardino, 09289
23 changes: 23 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"development": {
"username": "postgres",
"password": null,
"database": "demow3d5",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
5 changes: 5 additions & 0 deletions contacts.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1,Lani Rollins,[email protected],1-633-389-7173
2,McKenzie Burris,[email protected],1-906-235-0832
3,Amethyst Morgan,[email protected],1-548-366-6273
4,Lamar Hardin,[email protected],1-519-693-8091
5,Keegan Coleman,[email protected],1-998-626-8896
75 changes: 75 additions & 0 deletions controller/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"use strict"
const view = require('../view')
const model = require('../models')

class Controller{
static navigation(command, option) {
switch(command){
case "contacts:list":
model.Contact.findAll({raw:true}).then(projects => {
view.printAll(projects)
}); break;

case "contacts:add":
model.Contact.create({
name: option[0],
email: option[1],
phone: option[2] }).then(projects => {
view.addFile()
}); break;

case "contacts:update":
let objContact = {}
objContact[option[1]] = option[2]
model.Contact.update(objContact, {where:{id: option[0]}}).then(projects => {
view.updateData('Contact', option, projects)
}); break;

case "contacts:delete":
model.Contact.destroy({where:{id: option[0]}}).then(projects => {
view.deleteData('Contact', option, projects)
}); break;

case "addresses:list":
model.Address.findAll({raw:true}).then(projects => {
view.printAll(projects)
}); break;

case "addresses:add":
model.Address.create({
street: option[0],
city: option[1],
zip_code: option[2],
ContactId: option[3]}).then(projects => {
view.addFile()
}); break;

case "addresses:update":
let objAddress = {}
objAddress[option[1]] = option[2]
model.Address.update(objAddress, {where:{id: option[0]}}).then(projects => {
view.updateData('Address', option, projects)
}); break;

case "addresses:delete":
model.Address.destroy({where:{id: option[0]}}).then(projects => {
view.deleteData('Address', option, projects)
}); break;

case "contacts:complete":
// Model.Contact.findAll({
// attributes: { include: [[sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']] }
model.Address.findAll({
include: models.Address,
raw:true
}).then(projects => {
view.printAll(projects)
});

case "help": view.help(); break;
default: view.help()
}
}
}

module.exports = Controller;
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict"
const Controller = require('./controller/')

let start = process.argv

Controller.navigation(start[2],start.slice(3))
33 changes: 33 additions & 0 deletions migrations/20180222055321-create-contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Contacts', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
phone: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Contacts');
}
};
33 changes: 33 additions & 0 deletions migrations/20180222055425-create-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Addresses', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
street: {
type: Sequelize.STRING
},
city: {
type: Sequelize.STRING
},
zip_code: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Addresses');
}
};
25 changes: 25 additions & 0 deletions migrations/20180222093624-add-column-idcontact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
return queryInterface.addColumn('Addresses', 'ContactId', { type: Sequelize.INTEGER })
},

down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.dropTable('users');
*/
return queryInterface.removeColumn('Addresses', 'ContactId')
}
};
13 changes: 13 additions & 0 deletions models/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Address = sequelize.define('Address', {
street: DataTypes.STRING,
city: DataTypes.INTEGER,
zip_code: DataTypes.STRING
}, {});
Address.associate = function(models) {
// associations can be defined here
Address.belongsTo(models.Contact, {foreignKey: 'ContactId'});
};
return Address;
};
13 changes: 13 additions & 0 deletions models/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Contact = sequelize.define('Contact', {
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.STRING
}, {});
Contact.associate = function(models) {
// associations can be defined here
Contact.hasMany(models.Address, {foreignKey: 'ContactId'});
};
return Contact;
};
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.json')[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;
Loading