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

DONE #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

DONE #19

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 .~lock.contacts.csv#
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
,eki,eki,22.02.2018 12:58,file:///home/eki/.config/libreoffice/4;
20 changes: 10 additions & 10 deletions addresses.csv
Original file line number Diff line number Diff line change
@@ -1,10 +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
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": "new_db",
"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"
}
}
134 changes: 134 additions & 0 deletions controller/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
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
}
});
}
}

}
}


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()
33 changes: 33 additions & 0 deletions migrations/20180222054023-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/20180222054148-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/20180222054440-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/20180222081432-addForeignKey.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', 'id_contact', Sequelize.STRING);
},

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

Example:
return queryInterface.dropTable('users');
*/
return queryInterface.addColumn('Addresses', 'id_contact', Sequelize.STRING);
}
};
24 changes: 24 additions & 0 deletions migrations/20180222082433-addForeignKeyLagi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'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', 'id_contact', Sequelize.STRING);
},

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

Example:
return queryInterface.dropTable('users');
*/
}
};
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('Contacts', { foreignKey: 'id_contact' })
};
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(Addresses, { foreignKey: 'uid' })
};
return Contact;
};
Loading