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

add hasil release 2 minus belum pake table #22

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": "postgres",
"password": "katakunci",
"database": "migrasi_database",
"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"
}
}
102 changes: 102 additions & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const Models = require('../models');
const Views = require('../views')

class Controller {
constructor() {
}

static manageCommand(command,options) {
switch(true) {
case (command == 'help') : this.help(); break;
case (command == 'contacts:list') : this.list(command); break;
case (command == 'addresses:list') : this.list(command); break;
case (command == 'contacts:add') : this.addContact(options); break;
case (command == 'addresses:add') : this.addAddresses(options); break;
case (command == 'contacts:update') : this.updateContact(options); break;
case (command == 'addresses:update') : this.updateAddresses(options); break;
case (command == 'contacts:delete') : this.deleteContact(options); break;
case (command == 'addresses:delete') : this.deleteAddresses(options); break;
default: this.help();
}
}

static help() {
Views.showHelp();
}

static list(command) {
if (command == 'contacts:list') {
Models.Contact.findAll({raw:true})
.then(project => {
Views.showList(project);
})
} else if (command == 'addresses:list') {
Models.Address.findAll({raw:true})
.then(project => {
Views.showList(project);
})
}
}

static addContact(arrOptions) {

Models.Contact.create({ name: arrOptions[0], email: arrOptions[1], phone: arrOptions[2] })
.then(() => Models.Contact.findOrCreate({where: {name: arrOptions[0]}, defaults: {}}))
.spread((Contact, created) => {
Views.addContact(Contact,created);
})

}

static addAddresses(arrOptions) {

Models.Address.create({ street: arrOptions[0], city: arrOptions[1], zip_code: arrOptions[2] })
.then(() => Models.Address.findOrCreate({where: {street: arrOptions[0]}, defaults: {}}))
.spread((Address, created) => {
Views.addAddresses(Address,created);
})

}

static updateContact(arrOptions) {

var updateObj = {};
updateObj[arrOptions[1]] = arrOptions[2];
Models.Contact.update(updateObj, {where: {id:arrOptions[0]}})
.then(() => {
Views.updateContact();
})

}

static updateAddresses(arrOptions) {

var updateObj = {};
updateObj[arrOptions[1]] = arrOptions[2];
Models.Address.update(updateObj, {where: {id:arrOptions[0]}})
.then(() => {
Views.updateAddresses();
})

}

static deleteContact(arrOptions) {
Models.Contact.destroy({where: {id:arrOptions[0]}})
.then(() => {
Views.deleteContact();
})
}

static deleteAddresses(arrOptions) {
Models.Address.destroy({where: {id:arrOptions[0]}})
.then(() => {
Views.deleteAddresses();
})
}



}


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

var argv_data = process.argv;

Controller.manageCommand(argv_data[2],argv_data.slice(3)) // array of strings
33 changes: 33 additions & 0 deletions migrations/20180222055831-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/20180222060000-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 models/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'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
};
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;
};
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