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

release 2 #1

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": "Cecha",
"password": "admin",
"database": "seq_database",
"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
70 changes: 70 additions & 0 deletions controllers/addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const {addresses, contacts} = require('../models');
const view = require('../views/addresses.js');

class Addresses {
constructor() {

}
static list(){
addresses.findAll({
include : [
{model : contacts}
]
}).then(data => {
let result = []
for(let i=0; i<data.length; i++){
result.push(data[i].dataValues)
}
view.showData(result)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static add(data){
addresses.create({
street : data[0],
city : data[1],
zip_code : data[2],
contactID : data[3]
}).then(data => {
view.showAdd(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static update(data){
let obj = {}
for(let i=0; i<data.length; i++){
if(i%2 !== 0){
obj[data[i]] = data[i+1]
}
}
addresses.update(obj, {
where:{
id : data[0]
}
}).then(result => {
view.showUpdate(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static delete(data){
addresses.destroy({where: {id : data}
}).then(result => {
view.showDelete(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
}

module.exports = Addresses;
81 changes: 81 additions & 0 deletions controllers/contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const {contacts, addresses} = require('../models');
const view = require('../views/contacts.js');

class Contacts {
constructor() {

}
static list(){
contacts.findAll({
include : [
{model: addresses}
]
}).then(data => {
let result = []
for(let i=0; i<data.length; i++){
result.push(data[i].dataValues)
}
view.showData(result)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static add(data){
contacts.create({
name : data[0],
email : data[1],
phone : data[2]
}).then(data => {
view.showAdd(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static update(data){
let obj = {}
for(let i=0; i<data.length; i++){
if(i%2 !== 0){
obj[data[i]] = data[i+1]
}
}
contacts.update(obj, {
where:{
id : data[0]
}
}).then(result => {
view.showUpdate(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static delete(data){
contacts.destroy({where: {id : data}
}).then(result => {
view.showDelete(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static findAddresses(data){
contacts.findOne({
include: {model: addresses},
where: {id : data}
}).then(data => {
view.showAddresses(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
}

module.exports = Contacts;
39 changes: 39 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const contacts = require('./controllers/contacts');
const addresses = require('./controllers/addresses');

let commandSplit = process.argv[2].split(':')
let category = commandSplit[0]
let task = commandSplit[1]
let data = process.argv.splice(3)

if(category === 'contacts'){
if(task === 'list'){
contacts.list()
}
else if(task === 'add'){
contacts.add(data)
}
else if(task === 'update'){
contacts.update(data)
}
else if(task === 'delete'){
contacts.delete(data)
}
else if(task === 'findAddresses'){
contacts.findAddresses(data)
}
}
else if(category === 'addresses'){
if(task === 'list'){
addresses.list()
}
else if(task === 'add'){
addresses.add(data)
}
else if(task === 'update'){
addresses.update(data)
}
else if(task === 'delete'){
addresses.delete(data)
}
}
33 changes: 33 additions & 0 deletions migrations/20180222055739-create-contacts.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/20180222055859-create-addresses.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.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('addresses');
}
};
11 changes: 11 additions & 0 deletions migrations/20180222081553-add-contactID-to-addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('addresses', 'contactID', Sequelize.INTEGER);
},

down: (queryInterface, Sequelize) => {
return queryInterface.removeColumn('addresses');
}
};
14 changes: 14 additions & 0 deletions models/addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var addresses = sequelize.define('addresses', {
street: DataTypes.STRING,
city: DataTypes.STRING,
zip_code: DataTypes.INTEGER,
contactID: DataTypes.INTEGER
}, {});
addresses.associate = function(models) {
addresses.belongsTo(models.contacts, {foreignKey: 'contactID'})
};

return addresses;
};
12 changes: 12 additions & 0 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var contacts = sequelize.define('contacts', {
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.STRING
}, {});
contacts.associate = function(models) {
contacts.hasMany(models.addresses, {foreignKey: 'contactID'})
};
return contacts;
};
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