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 1 - CRUD Address #19

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, 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": "yosa",
"password": "123456",
"database": "mvcsequelize",
"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
48 changes: 48 additions & 0 deletions controllers/controller-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const model = require('../models')
const view = require('../views/view-addresses.js')

class ControllerAddresses {
constructor(){

}
static manageAction(act, value){
if(act === 'list'){
model.Address.findAll()
.then(address => {
view.list(address)
})
} else if(act === 'add'){
model.Address.create({
street: value[0],
city: value[1],
zip_code: value[2],
contactId: value[3]
})
.then(addAddress =>{
view.notifAdd(addAddress)
})
} else if(act === 'update'){
model.Address.update({
street: value[1],
city: value[2],
zip_code: value[3],
contactId: value[4]
}, {where: {id: value[0]}
})
.then(updateAddress => {
view.notifUpdate(updateAddress)
})
} else if(act === 'delete'){
model.Address.destroy({
where: { id: value[0] }
})
.then(deleteAddress =>{
view.notifDelete(deleteAddress)
})

}
}

}

module.exports = ControllerAddresses
46 changes: 46 additions & 0 deletions controllers/controller-contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const model = require('../models')
const view = require('../views/views-contacts.js')

class ControllerContacts {
constructor(){

}
static manageAction(act, value){
if(act === 'list'){
model.Contact.findAll()
.then(contact => {
// console.log(contact)
view.list(contact)
})
} else if(act === 'add'){
model.Contact.create({
name: value[0],
email: value[1],
phone: value[2]
})
.then(addContact =>{
view.notifAdd(addContact)
})
} else if(act === 'update'){
model.Contact.update({
name: value[1],
email: value[2],
phone: value[3]
}, {where: {id: value[0]}
})
.then(updateContact => {
view.notifUpdate(updateContact)
})
} else if(act === 'delete'){
model.Contact.destroy({
where: { id: value[0] }
})
.then(deleteContact =>{
view.notifDelete(deleteContact)
})

}
}
}

module.exports = ControllerContacts
17 changes: 17 additions & 0 deletions controllers/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
let controlContact = require('./controller-contact.js')
let controlAddress = require('./controller-address.js')

class Controller {
constructor(){

}
static manageOption(option, act, value){
if(option === 'contacts'){
controlContact.manageAction(act, value)
} else if(option === 'addresses'){
controlAddress.manageAction(act, value)
}
}
}

module.exports = Controller
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let control = require('./controllers/controller.js')
let argv = process.argv;

if(argv[2] === null){
console.log('contacts list --- untuk menampilkan daftar kontak')
console.log('contacts add --- untuk menambahkan data kontak')
console.log('contacts update --- untuk mengubah kontak')
console.log('contacts delete --- untuk menghapus data kontak')
} else {
// kirim option, act, dan value
control.manageOption(argv[2], argv[3], argv.slice(4))
}
33 changes: 33 additions & 0 deletions migrations/20180222051108-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/20180222051255-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.INTEGER
},
zip_code: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Addresses');
}
};
14 changes: 14 additions & 0 deletions migrations/20180222081435-ubahTypeData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {

return queryInterface.changeColumn('Addresses', 'city', {type: Sequelize.STRING});

},

down: (queryInterface, Sequelize) => {

// return queryInterface.changeColumn('Addresses', 'city', {type: Sequelize.STRING});
}
};
33 changes: 33 additions & 0 deletions migrations/20180222123058-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/20180222123256-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');
}
};
12 changes: 12 additions & 0 deletions migrations/20180222152840-add-column.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {

return queryInterface.addColumn('Addresses', 'contactId', { type: Sequelize.INTEGER})
},

down: (queryInterface, Sequelize) => {

}
};
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,
contactId: DataTypes.INTEGER
}, {});
Address.associate = function(models) {
// associations can be defined here
};
return Address;
};
12 changes: 12 additions & 0 deletions models/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'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
};
return Contact;
};
Loading