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

Reza Pramudhika #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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": "reza",
"password": "postgres",
"database": "reza",
"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"
}
}
47 changes: 47 additions & 0 deletions controllers/article.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const model = require('../models/index');
const view = require('../views/index');

class ArticleController{
static run(option){
switch(option[0]){
case 'add' :
model.articles.create({
title: option[1],
body: option[2],
id_author: option[3],
id_tag: option[4]
})
.then(article => {view.dislayTableArticle([article.dataValues]);})
.catch(err => {view.dislayStatus(err);}); break;
case 'readOne' :
model.articles.findOne({raw:true, where: {id: option[1]} })
.then(article => {view.dislayTableArticle([article]);})
.catch(err => {view.dislayStatus(err);}); break;
case 'readAll' :
model.articles.findAll({raw:true})
.then(article => {view.dislayTableArticle(article);})
.catch(err => {view.dislayStatus(err);}); break;
case 'update' :
let objToWrite = {};
let valueArr = option[2].split(',');
for (let i=0; i<valueArr.length; i++){
let row = valueArr[i].split(':');
switch(row[0]){
case 'title': objToWrite.title = row[1]; break;
case 'body': objToWrite.body = row[1]; break;
case 'id_author': objToWrite.id_author = row[1]; break;
case 'id_tag': objToWrite.id_tag = row[1]; break;
}
}
model.articles.update(objToWrite,{where: {id:option[1]}})
.then(article => {view.dislayStatus('Data updated');})
.catch(err => {view.dislayStatus(err);}); break;
case 'delete' :
model.articles.destroy({where: {id: option[1]}})
.then(article => {view.dislayStatus('Article deleted');})
.catch(err => {view.dislayStatus(err);}); break;
}
}
}

module.exports = ArticleController;
49 changes: 49 additions & 0 deletions controllers/author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const model = require('../models/index');
const view = require('../views/index');

class AuthorController{
static run(option){
switch(option[0]){
case 'add' :
model.authors.create({
first_name: option[1],
last_name: option[2],
religion: option[3],
gender: option[4],
age: option[5]
})
.then(author => {view.dislayTableAuthor([author.dataValues]);})
.catch(err => {view.dislayStatus(err);}); break;
case 'readOne' :
model.authors.findOne({raw:true, where: {id: option[1]} })
.then(author => {view.dislayTableAuthor([author]);})
.catch(err => {view.dislayStatus(err);}); break;
case 'readAll' :
model.authors.findAll({raw:true})
.then(authors => {view.dislayTableAuthor(authors);})
.catch(err => {view.dislayStatus(err);}); break;
case 'update' :
let objToWrite = {};
let valueArr = option[2].split(',');
for (let i=0; i<valueArr.length; i++){
let row = valueArr[i].split(':');
switch(row[0]){
case 'first_name': objToWrite.first_name = row[1]; break;
case 'last_name': objToWrite.last_name = row[1]; break;
case 'religion': objToWrite.religion = row[1]; break;
case 'gender': objToWrite.gender = row[1]; break;
case 'age': objToWrite.age = row[1]; break;
}
}
model.authors.update(objToWrite,{where: {id:option[1]}})
.then(authors => {view.dislayTableAuthor('Data updated');})
.catch(err => {view.dislayStatus(err);}); break;
case 'delete' :
model.authors.destroy({where: {id: option[1]}})
.then(authors => {view.dislayTableAuthor('Author deleted');})
.catch(err => {view.dislayStatus(err);}); break;
}
}
}

module.exports = AuthorController;
26 changes: 26 additions & 0 deletions controllers/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

const view = require('../views/index');
class Default {
static run(){
let data =
`=================Documentation==================
author add -> add<space> "data yang ingin dimasukan"
author readOne -> readOne<space> "masukkan id author"
author readAll -> readAll
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 readOne -> readOne<space> "masukkan id tag"
tag readAll -> readAll
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 readOne -> readOne<space> "masukkan id article"
article readAll -> readAll
article update -> update<space> "masukkan data yang ingin di update dan idnya"
article delete -> delete<space> "masukkan id article"
================================================`;
view.dislayStatus(data);
}
}
module.exports = Default;
44 changes: 44 additions & 0 deletions controllers/tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const model = require('../models/index');
const view = require('../views/index');

class TagController{
static run(option){
switch(option[0]){
case 'add' :
model.tags.create({
name: option[1]
})
.then(tag => {view.dislayTableTag([tag.dataValues]);})
.catch(err => {view.dislayStatus(err);}); break;
case 'readOne' :
model.tags.findOne({raw:true, where: {id: option[1]} })
.then(tag => {view.dislayTableTag([tag]);})
.catch(err => {view.dislayStatus(err);}); break;
case 'readAll' :
model.tags.findAll({raw:true})
.then(tag => {view.dislayTableTag(tag);})
.catch(err => {view.dislayStatus(err);}); break;
case 'update' :
let objToWrite = {};
let valueArr = option[2].split(',');
for (let i=0; i<valueArr.length; i++){
let row = valueArr[i].split(':');
switch(row[0]){
case 'title': objToWrite.title = row[1]; break;
case 'body': objToWrite.body = row[1]; break;
case 'id_author': objToWrite.id_author = row[1]; break;
case 'id_tag': objToWrite.id_tag = row[1]; break;
}
}
model.tags.update(objToWrite,{where: {id:option[1]}})
.then(tag => {view.dislayTableTag('Data updated');})
.catch(err => {view.dislayStatus(err);}); break;
case 'delete' :
model.tags.destroy({where: {id: option[1]}})
.then(tag => {view.dislayTableTag(`Tag ${option[1]} deleted`);})
.catch(err => {view.dislayStatus(err);}); break;
}
}
}

module.exports = TagController;
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const AuthorController = require('./controllers/author');
const ArticleController = require('./controllers/article');
const TagController = require('./controllers/tag');
const Default = require('./controllers/default');

const argv = process.argv;

switch(argv[2]){
case 'author': AuthorController.run(argv.splice(3)); break;
case 'article': ArticleController.run(argv.splice(3)); break;
case 'tag': TagController.run(argv.splice(3)); break;
default: Default.run();
}
39 changes: 39 additions & 0 deletions migrations/20180221050035-create-authors.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');
}
};
36 changes: 36 additions & 0 deletions migrations/20180221050103-create-articles.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.STRING
},
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');
}
};
27 changes: 27 additions & 0 deletions migrations/20180221050112-create-tags.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/articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var articles = sequelize.define('articles', {
title: DataTypes.STRING,
body: DataTypes.STRING,
id_author: DataTypes.INTEGER,
id_tag: DataTypes.INTEGER
}, {});
articles.associate = function(models) {
// associations can be defined here
};
return articles;
};
14 changes: 14 additions & 0 deletions models/authors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var authors = sequelize.define('authors', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
religion: DataTypes.STRING,
gender: DataTypes.STRING,
age: DataTypes.INTEGER
}, {});
authors.associate = function(models) {
// associations can be defined here
};
return authors;
};
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/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var tags = sequelize.define('tags', {
name: DataTypes.STRING
}, {});
tags.associate = function(models) {
// associations can be defined here
};
return tags;
};
Loading