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 #17

Open
wants to merge 1 commit 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,8219
8,2508 Dolor. Av,Muskegon KY,12482
9,666-4366 Lacinia Avenue,Ohio,19253
10,Lacinia Road,San Bernardino,9289
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": "admin",
"password": 12345,
"database": "db_many",
"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
142 changes: 142 additions & 0 deletions controller/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
const models = require('../models')
const views = require('../view')
class Controller{
constructor(input2,input3,input4,input5){
this.input2=input2
this.input3=input3
this.input4=input4
this.input5=input5

}
showData(){
if(this.input3==='add'){
var obj={};
var newData=this.input4
var pisah= newData.split(',')
if(this.input2==='contacts'){
for(let i=0;i<pisah.length;i++){
var titikDua=pisah[i].toString().split(':')
if(titikDua[0]==='name'){
obj.name=titikDua[1]
}
else if (titikDua[0]==='email') {
obj.email=titikDua[1]
}
else if (titikDua[0]==='phone') {
obj.phone=titikDua[1]
}
}
models.Contact.create(obj);
}
else if(this.input2==='addresses'){
for(let i=0;i<pisah.length;i++){
var titikDua=pisah[i].toString().split(':')
if(titikDua[0]==='street'){
obj.street=titikDua[1]
}
else if (titikDua[0]==='city') {
obj.city=titikDua[1]
}
else if (titikDua[0]==='zip_code') {
obj.zip_code=titikDua[1]
}
}
models.Address.create(obj);
}

}
else if(this.input3==='list'){
if(this.input2==='contacts'){
models.Contact.findAll({
raw:true,
}).then(contactsData => {
views.printView(contactsData)
});
}
else if(this.input2==='addresses'){
models.Address.findAll({
raw:true,
}).then(addressesData => {
views.printView(addressesData)
});
}
}

else if(this.input3==='update'){
var obj={};
var newData=this.input4
var pisah= newData.split(',')
if(this.input2==='contacts'){
for(let i=0;i<pisah.length;i++){
var titikDua=pisah[i].toString().split(':')
if(titikDua[0]==='name'){
obj.name=titikDua[1]
}
else if (titikDua[0]==='email') {
obj.email=titikDua[1]
}
else if (titikDua[0]==='phone') {
obj.phone=titikDua[1]
}
}
models.Contact.update(
obj, {
where: {
id: this.input5
}
});
}
else if(this.input2==='addresses'){
for(let i=0;i<pisah.length;i++){
var titikDua=pisah[i].toString().split(':')
if (titikDua[0]==='street') {
obj.street=titikDua[1]
}
else if (titikDua[0]==='city') {
obj.city=titikDua[1]
}
else if (titikDua[0]==='zip_code') {
obj.zip_code=titikDua[1]
}
}
models.Address.update(
obj, {
where: {
id: this.input5
}
});
}
}


else if(this.input3==='delete'){
if(this.input2==='contacts'){
models.Contact.destroy({
where: {
id: this.input4
}
});
}
else if(this.input2==='addresses'){
models.Address.destroy({
where: {
id: this.input4
}
});
}
}

else if(this.input3==='findAddress'){

models.Contact.findAll({include: models.Address,raw:true,
where: { id: this.input4 }})
.then(contactsData => {
views.printView(contactsData)
});
}

}
}


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 @@
const argv = process.argv;
var sequelize = require('sequelize');
const Controller = require('./controller')

var controller = new Controller(argv[2],argv[3],argv[4],argv[5]);
controller.showData()
36 changes: 36 additions & 0 deletions migrations/20180222094405-create-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'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
},
id_contact: {
type: Sequelize.INTEGER
},
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Addresses');
}
};
33 changes: 33 additions & 0 deletions migrations/20180222094503-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');
}
};
16 changes: 16 additions & 0 deletions models/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Address = sequelize.define('Address', {
street: DataTypes.STRING,
city: DataTypes.STRING,
zip_code: DataTypes.STRING
}, {});
Address.associate = function(models) {
// associations can be defined here
Address.belongsTo(models.Contact, {
foreignKey: 'id_contact'
})

};
return Address;
};
15 changes: 15 additions & 0 deletions models/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'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: 'id_contact'
})
};
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