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

Ardhiansyah #14

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": "root",
"password": "root",
"database": "demo",
"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"
}
}
86 changes: 86 additions & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict';
const Model = require('../models');
const View = require('../views');

class Controller {
static menu (command, options) {
switch(options[0]) {
case "add" :
let data = {};
if (command == 'author') data = { first_name: options[1], last_name: options[2], religion: options[3], gender: options[4], age: options[5] }; else
if (command == 'tag') data = { name: options[1] }; else
if (command == 'article') data = { title: options[1], body: options[2], id_author: options[3], id_tag: options[4] };

Controller.add(command, data, result => View.ShowData(`Data ${command} ${result.first_name} ${result.last_name} telah ditambahkan dengan id : "${result.id}"`));
break;

case "read_one" :
Controller.read_one(command, options[1], row => View.read_one(command, row));
break;

case "read_all" :
Controller.read_all(command, rows => View.read_all(command, rows));
break;

case "update" :
let obj = {};
obj[options[1]] = options[2];
Controller.update(command, obj, options[3], result => {
if (result) View.ShowData(`Data ${command} dengan id : ${options[3]} telah diupdate`);
});
break;

case "delete" :
Controller.delete(command, options[1], result => {
if (result) View.ShowData(`Data ${command} dengan id : ${options[1]} telah dihapus`)
});
break;
default :
View.ShowData(`
==== documentation =========================================
author add -> add<space> "data yang ingin dimasukan"
author read_one -> read_one<space> "masukkan id author"
author read_all -> read_all
author update -> update<space> "masukkan data yang ingin di update dan idnya"
author delete -> delete<space> "masukkan id author"
tag add -> add<space> "data yang ingin dimasukan"
tag read_one -> read_one<space> "masukkan id tag"
tag read_all -> read_all
tag update -> update<space> "masukkan data yang ingin di update dan idnya"
tag delete -> delete<space> "masukkan id tag"
article add -> add<space> "data yang ingin dimasukan"
article read_one -> read_one<space> "masukkan id article"
article read_all -> read_all
article update -> update<space> "masukkan data yang ingin di update dan idnya"
article delete -> delete<space> "masukkan id article"
`);
}
}

static add (dbName, data, callback) {
Model[dbName].create(data, { plain: true })
.then(result => callback(result));
}

static read_one (dbName, data, callback) {
Model[dbName].findById(data, { raw: true })
.then(row => callback(row));
}

static read_all (dbName, callback) {
Model[dbName].findAll()
.then(rows => callback(rows));
}

static update (dbName, data, id, callback) {
Model[dbName].update(data, { where: { id: id }})
.then(result => callback(result));
}

static delete (dbName, data, callback) {
Model[dbName].destroy({ where: { id: data }})
.then(result => callback(result));
}
}

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

const input = process.argv;
Controller.menu(input[2], input.splice(3));
36 changes: 36 additions & 0 deletions migrations/20180221080727-create-article.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('articles', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
title: {
type: Sequelize.STRING
},
body: {
type: Sequelize.TEXT
},
id_author: {
type: Sequelize.INTEGER
},
id_tag: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('articles');
}
};
39 changes: 39 additions & 0 deletions migrations/20180221080926-create-author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('authors', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
first_name: {
type: Sequelize.STRING
},
last_name: {
type: Sequelize.STRING
},
religion: {
type: Sequelize.STRING
},
gender: {
type: Sequelize.STRING
},
age: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('authors');
}
};
27 changes: 27 additions & 0 deletions migrations/20180221081013-create-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('tags', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('tags');
}
};
13 changes: 13 additions & 0 deletions models/article.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var article = sequelize.define('article', {
title: DataTypes.STRING,
body: DataTypes.TEXT,
id_author: DataTypes.INTEGER,
id_tag: DataTypes.INTEGER
}, {});
article.associate = function(models) {
// associations can be defined here
};
return article;
};
14 changes: 14 additions & 0 deletions models/author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var author = sequelize.define('author', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
religion: DataTypes.STRING,
gender: DataTypes.STRING,
age: DataTypes.INTEGER
}, {});
author.associate = function(models) {
// associations can be defined here
};
return author;
};
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;
10 changes: 10 additions & 0 deletions models/tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var tag = sequelize.define('tag', {
name: DataTypes.STRING
}, {});
tag.associate = function(models) {
// associations can be defined here
};
return tag;
};
Loading