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 #3

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
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": "ervan",
"password": "adetya11",
"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"
}
}
148 changes: 148 additions & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
"use strict"
const model = require('../models');
const View = require('../views');
const ViewAuthor = require('../views/author.js');
const ViewArticle = require('../views/article.js');
const ViewTag = require('../views/tag.js');

class Controller {
static manageCommand(command, option) {
if(command == 'author') {
if(option[0] == 'add') {
model.author.create({first_name:option[1],last_name:option[2],religion:option[3],gender:option[4],age:option[5]})
.then(task => {
View.addSuccess();
})
}
else if(option[0] == 'read_all') {
model.author.findAll({raw:true})
.then(data => {
ViewAuthor.view(data);
})
}
else if(option[0] == 'read_one') {
model.author.findOne({ raw:true, where: {id: option[1]} })
.then(data => {
ViewAuthor.view([data]);
})
}
else if(option[0] == 'update') {
let updateValues = Controller.updateSplitterFormat(option[2])
model.author.findOne({ raw:true, where: {id: option[1]} })
.then(data => {
for(let i in updateValues) {
if(updateValues[i][0] == 'first_name') data.first_name = updateValues[i][1];
else if(updateValues[i][0] == 'last_name') data.last_name = updateValues[i][1];
else if(updateValues[i][0] == 'religion') data.religion = updateValues[i][1];
else if(updateValues[i][0] == 'gender') data.gender = updateValues[i][1];
else if(updateValues[i][0] == 'age') data.age = updateValues[i][1];
}
return(data)
})
.then(function(data){
model.author.update(data, { where: { id: option[1] } }).then((result) => {
View.view(data);
});
})
}
else if(option[0] == 'delete') {
model.author.destroy({where: {id: option[1]} }).then(()=>{View.deleteSuccess();})
}
else View.help();
}
else if(command == 'tag') {
if(option[0] == 'add') {
model.tag.create({name:option[1]})
.then(data => {
View.addSuccess();
})
}
else if(option[0] == 'read_all') {
model.tag.findAll({raw:true})
.then(data => {
ViewTag.view(data);
})
}
else if(option[0] == 'read_one') {
model.tag.findOne({ raw:true, where: {id: option[1]} })
.then(data => {
ViewTag.view([data]);
})
}
else if(option[0] == 'update') {
let updateValues = Controller.updateSplitterFormat(option[2])
model.tag.findOne({ raw:true, where: {id: option[1]} })
.then(data => {
for(let i in updateValues) {
if(updateValues[i][0] == 'name') data.name = updateValues[i][1];
}
return(data)
})
.then(function(data){
model.tag.update(data, { where: { id: option[1] } }).then((result) => {
View.updateSuccess();
});
})
}
else if(option[0] == 'delete') {
model.tag.destroy({where: {id: option[1]} }).then(()=>{View.deleteSuccess();})
}
else View.help();
}
else if(command == 'article') {
if(option[0] == 'add') {
model.article.create({title:option[1],body:option[2],id_author:option[3],id_tag:option[4]})
.then(data => {
View.addSuccess();
})
}
else if(option[0] == 'read_all') {
model.article.findAll({raw:true})
.then(data => {
ViewArticle.view(data);
})
}
else if(option[0] == 'read_one') {
model.article.findOne({ raw:true, where: {id: option[1]} })
.then(data => {
ViewArticle.view([data]);
})
}
else if(option[0] == 'update') {
let updateValues = Controller.updateSplitterFormat(option[2])
model.article.findOne({ raw:true, where: {id: option[1]} })
.then(data => {
for(let i in updateValues) {
if(updateValues[i][0] == 'title') data.title = updateValues[i][1];
else if(updateValues[i][0] == 'body') data.body = updateValues[i][1];
else if(updateValues[i][0] == 'id_author') data.id_author = updateValues[i][1];
else if(updateValues[i][0] == 'id_tag') data.id_tag = updateValues[i][1];
}
return(data)
})
.then(function(data){
model.article.update(data, { where: { id: option[1] } }).then((result) => {
View.updateSuccess();
});
})
}
else if(option[0] == 'delete') {
model.article.destroy({where: {id: option[1]} }).then(()=>{View.deleteSuccess();})
}
else View.help();
}
else {
View.help()
}
console.log(`==========================================================`)
}

static updateSplitterFormat(option) {
console.log(option)
option = option.split(',').map(a => a.split(':'));
return option
// optionArr.map(a => a.split(':'))
}
}

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

let command = argv[2];
let option = argv.slice(3);
console.log(command, option)
Controller.manageCommand(command, option)
36 changes: 36 additions & 0 deletions migrations/20180221060314-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.STRING
},
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/20180221060327-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');
}
};
27 changes: 27 additions & 0 deletions migrations/20180221060338-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');
}
};
36 changes: 36 additions & 0 deletions migrations/20180221080732-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.STRING
},
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/20180221080732-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');
}
};
27 changes: 27 additions & 0 deletions migrations/20180221080738-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');
}
};
Loading