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

Open
wants to merge 4 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": "hari2018",
"database": "latihan_migration",
"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
96 changes: 96 additions & 0 deletions controllers/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const {address} = require('../models')
const View_address = require('../views/address')

class Controller_address{
constructor(){

}
static command(order,input){
// console.log(order,'-----------------')
if(order === 'list'){
address.findAll().then(dataAll=>{
// console.log(dataAll)
let arraddress = []
dataAll.map(detailData=>{
arraddress.push(detailData.dataValues)
})
View_address.showAll(arraddress)
process.exit()
}).catch(err=>{
View_address.showErr(err)
})
}
else if(order === 'add'){
let splitInput = input[0].split(',')
let objAdd ={
street : splitInput[0],
city: splitInput[1],
zip_code: splitInput[2],
id_contact:splitInput[3],
createdAt : new Date(),
updatedAt : new Date()
}
// console.log(objAdd)
address.create(objAdd).then(()=>{
View_address.showAdd(objAdd)
process.exit()
}).catch(err=>{
View_address.showErr(err)
})
}
else if(order === 'update'){
// 'input,,,,' id
let inputSplit = input[0].split(',')
address.findById(input[1]).then(dataId =>{
let objUpdate ={
street : inputSplit[0] === ''? dataId.dataValues.street:inputSplit[0],
city: inputSplit[1] === ''? dataId.dataValues.city:inputSplit[1],
zip_code: inputSplit[2] === ''? dataId.dataValues.zip_code:inputSplit[2],
id_contact:inputSplit[3] === ''? dataId.dataValues.id_contact:inputSplit[3],
createdAt : new Date(),
updatedAt : new Date()
}
address.update(objUpdate,{where:{id:input[1]}}).then(()=>{
View_address.showUpdate(input[1])
process.exit()
})
})
}
else if(order === 'delete'){
address.destroy({where:{id:input}}).then(()=>{
View_address.showDelete(input)
process.exit()
}).catch(err=>{
View_address.showErr(err)
})
}
else if(order === 'full_address'){
address.findOne({where:{id:input}}).then(data=>{
let detail = JSON.parse(JSON.stringify(data))
let newAdd = new address()
View_address.showAll(newAdd.full_address(detail.street,detail.city,detail.zip_code))

process.exit()
})
}
else if(order === 'north_area'){
address.north_area().then(data=>{
// console.log(data)
let temp = JSON.parse(JSON.stringify(data))
View_address.showAll(temp)
process.exit()
})
}
else if(order === 'south_area'){
address.south_area().then(data=>{
// console.log(data)
let temp = JSON.parse(JSON.stringify(data))
View_address.showAll(temp)
process.exit()
})
}
}

}
// address.prototype.full_address('jalan','city',15532)
module.exports = Controller_address
82 changes: 82 additions & 0 deletions controllers/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const {contact,address} = require('../models')
const View_contact = require('../views/contact')

class Controller_contact{
constructor(){

}
static command(order,input){
if(order === 'list'){
contact.findAll().then(dataAll=>{
// console.log(dataAll)
let arrContact = []
for(let i=0;i<dataAll.length;i++){
// console.log(dataAll[i].dataValues)
arrContact.push(dataAll[i].dataValues)
}
View_contact.showAll(arrContact)
process.exit()
}).catch(err=>{
View_contact.showErr(err)
})
}
else if(order === 'listAll'){
contact.findAll({
include:[address]
}).then(dataAll=>{
let temp = JSON.parse(JSON.stringify(dataAll))
View_contact.showAll(temp)
process.exit()
})
}
else if(order === 'showOne'){
contact.findOne({where:{id:input},include:[address]}).then(data=>{
let temp = JSON.parse(JSON.stringify(data))
View_contact.showAll(temp)

process.exit()
})
}
else if(order === 'add'){
let splitInput = input[0].split(',')
let objAdd ={
name : splitInput[0],
email: splitInput[1],
phone: splitInput[2],
createdAt : new Date(),
updatedAt : new Date()
}
// console.log(objAdd)
contact.create(objAdd).then(()=>{
View_contact.showAdd(objAdd)
process.exit()
})
}
else if(order === 'update'){
// 'input,,,,' id
let inputSplit = input[0].split(',')
contact.findById(input[1]).then(dataId =>{
let objUpdate ={
name : inputSplit[0] === ''? dataId.dataValues.name:inputSplit[0],
email: inputSplit[1] === ''? dataId.dataValues.email:inputSplit[1],
phone: inputSplit[2] === ''? dataId.dataValues.phone:inputSplit[2],
createdAt : new Date(),
updatedAt : new Date()
}
contact.update(objUpdate,{where:{id:input[1]}}).then(()=>{
View_contact.showUpdate(input[1])
process.exit()
})
})
}
else if(order === 'delete'){
contact.destroy({where:{id:input}}).then(()=>{
View_contact.showDelete(input)
process.exit()
})
}
}

}

module.exports = Controller_contact
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//runner
const Controller_contact = require('./controllers/contact')
const Controller_address = require('./controllers/address')

// node app.js addresses:list
let commandInput = process.argv[2].split(':')
let input = process.argv.splice(3)
// console.log(commandInput[0])

if(commandInput[0]=== 'contacts'){
Controller_contact.command(commandInput[1],input)
}
else if(commandInput[0] === 'addresses'){
Controller_address.command(commandInput[1],input)
}
25 changes: 25 additions & 0 deletions migrations/20180222081217-addColumn.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.INTEGER,{second:true});
},

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

Example:
return queryInterface.dropTable('users');
*/
return queryInterface.removeColumn('addresses','id_contact');
}
};
36 changes: 36 additions & 0 deletions models/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
const Sequelize = require('sequelize');
const Op = Sequelize.Op
module.exports = (sequelize, DataTypes) => {
let address = sequelize.define('address', {
street: DataTypes.STRING,
city: DataTypes.STRING,
zip_code: DataTypes.INTEGER,
id_contact: DataTypes.INTEGER
});
address.associate = function(models){
address.belongsTo(models.contact,{foreignKey: 'id_contact'})
};
address.prototype.full_address = function (street,city,zip_code) {
let temp = `Alamat lengkap : ${street} - ${city} (${zip_code})`
return temp
};
address.north_area = function(){
return address.findAll({
where:{
zip_code :{[Op.lt]:50000}
}
})

}
address.south_area = function(){
return address.findAll({
where:{
zip_code:{[Op.gt]:50000}
}
})

}

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){
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