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 3 #4

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
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": "hacktiv8",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "hacktiv8",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"username": "hacktiv8",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
63 changes: 63 additions & 0 deletions controllers/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict'
const {Contact,Address} = require('../models')
const View = require('../views');

class Controller_address {

static getCommand ( command,input ) {
if( command === 'list' ) {
Address.findAll()
.then(addresses => {
let address = JSON.parse(JSON.stringify(addresses))
View.show(address)
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'add' ) {
let objAddress = {}
objAddress.street = input[0]
objAddress.city = input[1]
objAddress.zip_code = +input[2]
objAddress.contact_id = +input[3]
Address.create(objAddress)
.then(address => {
View.show(`Successfully add into Addresses`);
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'update' ) {
let objAddress = {}
objAddress[input[1]] = input[2]
Address.update(objAddress,{
where: {id: +input[0]}
})
.then(address => {
View.show(`Update success`);
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'delete' ) {
Address.destroy({
where: {id: +input}
})
.then(address => {
View.show(`Delete success`)
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'full_address') {
let a = new Address
console.log(a)
}
}
}

module.exports = Controller_address
79 changes: 79 additions & 0 deletions controllers/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict'
const {Contact,Address} = require('../models')
const View = require('../views');

class Controller_contact {

static getCommand ( command,input ) {
if( command === 'list' ) {
Contact.findAll()
.then(contacts => {
let arrOfcontact = []
contacts.forEach(contact => {
arrOfcontact.push(contact.dataValues)
})
View.show(arrOfcontact)
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'add' ) {
let objContact = {}
objContact.name = input[0]
objContact.email = input[1]
objContact.phone = input[2]
Contact.create(objContact)
.then(contact => {
View.show(`Successfully add into Contacts`);
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'update' ) {
let objContact = {}
objContact[input[1]] = input[2]
Contact.update(objContact,{
where: {id: +input[0]}
})
.then(contact => {
View.show(`Update success`);
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'delete') {
Contact.destroy({
where: {id: +input}
})
.then(contact => {
View.show(`Delete success`)
})
.catch(err => {
console.log(err);
})
} else if ( command === 'detail' ) {
Contact.findAll({
include: [ Address ]
}).then(contacts => {
let arr_contacts = []
contacts.forEach(contact => {
arr_contacts.push(contact.dataValues)
})
console.log(arr_contacts)
})
} else if ( command === 'address') {
Contact.findAll({
include: [Address],
where:{id: +input}
}).then(addresses => {
let address = JSON.parse(JSON.stringify(addresses))
console.log(address);
})
}
}
}

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

const dataFromArgv = process.argv[2].split(':')
const payload = process.argv.splice(3)
const table = dataFromArgv[0]
const command = dataFromArgv[1]

if(table === 'contacts') {
Contact.getCommand(command,payload)
} else if(table === 'addresses') {
Address.getCommand(command,payload)
}
17 changes: 17 additions & 0 deletions migrations/20180222075330-add-column-contact_id-to-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

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

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

Example:
return queryInterface.dropTable('users');
*/
}
};
17 changes: 17 additions & 0 deletions migrations/20180222093838-create-column-contact-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

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

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

Example:
return queryInterface.dropTable('users');
*/
}
};
19 changes: 19 additions & 0 deletions models/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Address = sequelize.define('Address', {
street: DataTypes.STRING,
city: DataTypes.STRING,
zip_code: DataTypes.INTEGER,
contact_id: DataTypes.INTEGER
});
Address.associate = models => {
Address.belongsTo ( models.Contact, {
foreignKey: 'contact_id'
})
}
Address.prototype.full_address = models => {
let full_address = `Alamat lengkap : [${this.street}] - [${this.city}] ([${this.zip_code}])`
return full_address
}
return Address
};
14 changes: 14 additions & 0 deletions models/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Contact = sequelize.define('Contact', {
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.STRING
});
Contact.associate = models => {
Contact.hasMany (models.Address, {
foreignKey: 'id'
})
}
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