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

Open
wants to merge 2 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
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": "password",
"database": "address",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "postgres",
"password": "password",
"database": "address",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"username": "postgres",
"password": "password",
"database": "address",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
67 changes: 67 additions & 0 deletions controllers/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const {Address, Contact} = require('../models')
const View = require('../views/address')
class Controller{
static list(){
Address.findAll().then(dataAddress=>{
dataAddress.map(data=>{
View.showList(data.dataValues)
})
process.exit();
}).catch(err=>console.log(err))
}
static add(street, city, zip_code, id_contact){
let objAddress = {
street : street,
city : city,
zip_code : zip_code,
id_contact : id_contact
}
Address.create(objAddress).then(()=>{
View.commandAdded()
process.exit();
}).catch(err=>console.log(err))
}

static update(id, column, value){
let objAddress = {}
if (column === 'street'){
objAddress = {
street : value
}
}else if(column === 'city'){
objAddress = {
city : value
}
}else if(column === 'zip_code'){
objAddress = {
zip_code : value
}
}else if(column === 'id_contact'){
objAddress = {
id_contact : value
}
}else{
console.log(`no column ${column} name exist`)
}
Address.update(objAddress,{where : {id : Number(id)}}).then(()=>{
View.commandUpdated(id)
process.exit();
}).catch(err=>console.log(err))
}
static delete(id){
Address.destroy({where:{ id : id}}).then(()=>{
View.commandDeleted(id)
process.exit();
})
}
static full_Address(id){
Address.findById(id).then(data=>{
let contactData = JSON.parse(JSON.stringify(data))
console.log(contactData)
//contactData.fullAddress()
console.log(contactData)
}).catch(err=>console.log(err))
}
}

module.exports = Controller
64 changes: 64 additions & 0 deletions controllers/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const {Contact, Address} = require('../models')
const View = require('../views/contact')
class Controller{
static list(){
Contact.findAll({
include : [Address]
}).then(dataContact=>{
let contactData = JSON.parse(JSON.stringify(dataContact))
View.showList(contactData)
process.exit();
}).catch(err=>console.log(err))
}
static finAddressById(id){
Contact.findById(Number(id),{
include:[Address]
}).then(data=>{
let addressDAta = JSON.parse(JSON.stringify(data))
View.showAddressByContact(addressDAta)
process.exit()
}).catch(err=>console.log(err))
}
static add(name, email, phone){
let objContact = {
name : name,
email : email,
phone : phone
}
Contact.create(objContact).then(()=>{
View.commandAdded()
process.exit();
}).catch(err=>console.log(err))
}

static update(id, column, value){
let objContact = {}
if (column === 'name'){
objContact = {
name : value
}
}else if(column === 'email'){
objContact = {
email : value
}
}else if(column === 'phone'){
objContact = {
phone : value
}
}else{
console.log(`no column ${column} name exist`)
}
Contact.update(objContact,{where : {id : Number(id)}}).then(()=>{
View.commandUpdated(id)
process.exit();
}).catch(err=>console.log(err))
}
static delete(id){
Contact.destroy({where:{ id : id}}).then(()=>{
View.commandDeleted(id)
process.exit();
})
}
}

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

if (process.argv[2] === 'contacts:list'){
Contact.list()
}else if (process.argv[2] === 'contacts:add'){
Contact.add(process.argv[3], process.argv[4], process.argv[5])
}else if(process.argv[2] === 'contacts:update'){
Contact.update(process.argv[3], process.argv[4], process.argv[5])
}else if (process.argv[2] === 'contacts:delete'){
Contact.delete(process.argv[3] )
}else if(process.argv[2] === 'contacts:showAddressById'){
Contact.finAddressById(process.argv[3] )
}else if (process.argv[2] === 'address:list'){
Address.list()
}else if (process.argv[2] === 'address:add'){
Address.add(process.argv[3], process.argv[4], process.argv[5], process.argv[6])
}else if(process.argv[2] === 'address:update'){
Address.update(process.argv[3], process.argv[4], process.argv[5])
}else if (process.argv[2] === 'address:delete'){
Address.delete(process.argv[3] )
}else if (process.argv[2] === 'address:full'){
Address.full_Address(process.argv[3] )
}
33 changes: 33 additions & 0 deletions migrations/20180222052251-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/20180222052604-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.INTEGER
},
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/20180222082827-addColumnid_contact.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);
},

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');
}
};
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,
id_contact: DataTypes.STRING
},);

Address.associate = function (models) {
Address.belongsTo(models.Contact,{foreignKey:'id_contact'})
};

Address.prototype.fullAddress = function() {
let full_Address = `Alamat lengkap : ${street} - ${city} (${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 = 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