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 1 #2

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
9 changes: 9 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"development": {
"username": "postgres",
"password": "postgres",
"database": "latihan-kamis",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
83 changes: 83 additions & 0 deletions controllers/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const {Addresses} = require('../models')
const AddressView = require ('../views/addressView.js')


class Address {
constructor() {

}

static showAll(){
Addresses.findAll()
.then(function(data){
AddressView.showAddress(data)
process.exit()
})
.catch(function(err){
console.log(err);
})
}

static showOne(data){
Addresses.findOne({
where :{
id : data
}
})
.then(function(data){
AddressView.showAddress([data])
process.exit()
})
.catch(function(err){
console.log(err);
})
}

static add(input){
Addresses.create({
street : input[0],
city : input[1],
zip_code : input[2]
}).then(function(){
console.log(`data ${input[0]} berhasil dibuat`);
process.exit()
})
.catch(function(err){
console.log('ada yang masukin datanya salah nich');
})
}

static delete(data){
Addresses.destroy({
where:{
id:data
}
}).then(function(err){
console.log(`data berhasil didelete`);
process.exit()
})
.catch(function(err){
console.log(`error pas delete mas`);
})
}

static edit(target,edit){
Addresses.update({
street : edit[0],
city : edit[1],
zip_code : edit[2]
},{
where:{
id:target
}
}).then(function(){
console.log('data berhasil diedit')
process.exit()
})
.catch(function(err){
console.log('edit data gagal');
})
}
}

module.exports = Address;
83 changes: 83 additions & 0 deletions controllers/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const {Contacts} = require('../models')
const ContactView = require ('../views/contactsView.js')


class Contact {
constructor() {

}

static showAll(){
Contacts.findAll()
.then(function(data){
ContactView.showContact(data)
process.exit()
})
.catch(function(err){
console.log(err);
})
}

static showOne(data){
Contacts.findOne({
where :{
id : data
}
})
.then(function(data){
ContactView.showContact([data])
process.exit()
})
.catch(function(err){
console.log(err);
})
}

static add(input){
Contacts.create({
name : input[0],
email : input[1],
phone : input[2]
}).then(function(){
console.log(`data ${input[0]} berhasil dibuat`);
process.exit()
})
.catch(function(err){
console.log('ada yang masukin datanya salah nich');
})
}

static delete(data){
Contacts.destroy({
where:{
id:data
}
}).then(function(err){
console.log(`data berhasil didelete`);
process.exit()
})
.catch(function(err){
console.log(`error pas delete mas`);
})
}

static edit(target,edit){
Contacts.update({
name : edit[0],
email : edit[1],
phone : edit[2]
},{
where:{
id:target
}
}).then(function(){
console.log('data berhasil diedit')
process.exit()
})
.catch(function(err){
console.log('edit data gagal');
})
}
}

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



class Index {
constructor() {

}

static address(data){
let command = data[0]
let payload = data.slice(1)
let target = data[1]
let edit = data.slice(2)
if(command === 'showAll'){
Address.showAll()
} else if(command === 'showOne'){
Address.showOne(payload)
} else if(command === 'add'){
Address.add(payload)
} else if(command === 'delete'){
Address.delete(payload)
} else if(command === 'edit'){
Address.edit(target,edit)
} else {
console.log('error');
}

}

static contact(data){
let command = data[0]
let payload = data.slice(1)
let target = data[1]
let edit = data.slice(2)
if(command === 'showAll'){
Contact.showAll()
} else if(command === 'showOne'){
Contact.showOne(payload)
} else if(command === 'add'){
Contact.add(payload)
} else if(command === 'delete'){
Contact.delete(payload)
} else if(command === 'edit'){
Contact.edit(target,edit)
} else {
console.log('error');
}

}
}


let command = process.argv[2]
let data = process.argv.slice(3)

if(command === 'address'){
Index.address(data)
} else if(command === 'contact'){
Index.contact(data)
} else {
Help.showHelp()
}
33 changes: 33 additions & 0 deletions migrations/20180222060444-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: true,
type: Sequelize.DATE
},
updatedAt: {
allowNull: true,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Addresses');
}
};
33 changes: 33 additions & 0 deletions migrations/20180222060451-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: true,
type: Sequelize.DATE
},
updatedAt: {
allowNull: true,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Contacts');
}
};
12 changes: 12 additions & 0 deletions models/addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Addresses = sequelize.define('Addresses', {
street: DataTypes.STRING,
city: DataTypes.STRING,
zip_code: DataTypes.INTEGER
}, {});
Addresses.associate = function(models) {
// associations can be defined here
};
return Addresses;
};
12 changes: 12 additions & 0 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Contacts = sequelize.define('Contacts', {
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.STRING
}, {});
Contacts.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