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

sudah #9

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": "Cecha",
"password": "admin",
"database": "seq_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"
}
}
61 changes: 61 additions & 0 deletions controllers/addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const {addresses} = require('../models');
const view = require('../views/addresses.js');

class Addresses {
constructor() {

}
static list(){
addresses.findAll().then(data => {
let result = []
for(let i=0; i<data.length; i++){
result.push(data[i].dataValues)
}
view.showData(result)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static add(data){
addresses.create({
street : data[0],
city : data[1],
zip_code : data[2]
}).then(data => {
view.showAdd(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static update(data){
let obj = {}
obj[data[1]] = data[2]
addresses.update(obj, {
where:{
id : data[0]
}
}).then(result => {
view.showUpdate(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static delete(data){
addresses.destroy({where: {id : data}
}).then(result => {
view.showDelete(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
}

module.exports = Addresses;
61 changes: 61 additions & 0 deletions controllers/contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const {contacts} = require('../models');
const view = require('../views/contacts.js');

class Contacts {
constructor() {

}
static list(){
contacts.findAll().then(data => {
let result = []
for(let i=0; i<data.length; i++){
result.push(data[i].dataValues)
}
view.showData(result)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static add(data){
contacts.create({
name : data[0],
email : data[1],
phone : data[2]
}).then(data => {
view.showAdd(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static update(data){
let obj = {}
obj[data[1]] = data[2]
contacts.update(obj, {
where:{
id : data[0]
}
}).then(result => {
view.showUpdate(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
static delete(data){
contacts.destroy({where: {id : data}
}).then(result => {
view.showDelete(data)
process.exit()
}).catch(error => {
console.log(error)
process.exit()
})
}
}

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

let commandSplit = process.argv[2].split(':')
let category = commandSplit[0]
let task = commandSplit[1]
let data = process.argv.splice(3)

if(category === 'contacts'){
if(task === 'list'){
contacts.list()
}
else if(task === 'add'){
contacts.add(data)
}
else if(task === 'update'){
contacts.update(data)
}
else if(task === 'delete'){
contacts.delete(data)
}
}
else if(category === 'addresses'){
if(task === 'list'){
addresses.list()
}
else if(task === 'add'){
addresses.add(data)
}
else if(task === 'update'){
addresses.update(data)
}
else if(task === 'delete'){
addresses.delete(data)
}
}
33 changes: 33 additions & 0 deletions migrations/20180222055739-create-contacts.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/20180222055859-create-addresses.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');
}
};
15 changes: 15 additions & 0 deletions models/addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var addresses = sequelize.define('addresses', {
street: DataTypes.STRING,
city: DataTypes.STRING,
zip_code: DataTypes.INTEGER
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return addresses;
};
15 changes: 15 additions & 0 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var contacts = sequelize.define('contacts', {
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return contacts;
};
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