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

Reynaldipane #18

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": "purge",
"password": "creatEpwD",
"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"
}
}
63 changes: 63 additions & 0 deletions controllers/ArticleController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const model = require('../models/index')
const viewArticle = require('../views/article');

class ArticleController {
static menu(command, option) {
switch (command) {
case "add":
model.articles.create({
title: option[0],
body: option[1],
id_author: option[2],
id_tag: option[3]
})
.then(articles => {
viewArticle.addView(option);
})
break;
case "read_one":
model.articles.findAll({
where: {
id: option[0]
},
raw: true
})
.then(articles => {
viewArticle.read_oneView(articles)
})
break;
case "read_all":
model.articles.findAll({ raw: true })
.then(articles => {
viewArticle.read_allView(articles)
})
break;
case "update":
let updateField = option[0].split(',');
let updateObj = {}
for (let i = 0; i < updateField.length; i++) {
let splittedKey = updateField[i].split(':')
updateObj[splittedKey[0]] = splittedKey[1];
}

model.articles.update(
updateObj,
{ where: { id: option[1] } }
)
.then(articles => {
viewArticle.updateView(option[1]);
})
break;
case "erase":
model.articles.destroy({
where: { id: option[0] }
})
.then(articles => {
viewArticle.destroyView(option[0]);
})
break;
}
}
}

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

class AuthorController {
static menu(command, option) {
switch (command) {
case "add":
model.authors.create({
first_name : option[0],
last_name : option[1],
religion : option[2],
gender: option[3],
age: option[4]
})
.then(authors => {
viewAuthor.addView(option);
})
break;
case "read_one":
model.authors.findAll({
where: {
id: option[0]
},
raw: true
})
.then(authors => {
viewAuthor.read_oneView(authors)
})
break;
case "read_all":
model.authors.findAll({ raw: true })
.then(authors => {
viewAuthor.read_allView(authors)
})
break;
case "update":
let updateField = option[0].split(',');
let updateObj = {}
for (let i = 0; i < updateField.length; i++) {
let splittedKey = updateField[i].split(':')
updateObj[splittedKey[0]] = splittedKey[1];
}

model.authors.update(
updateObj,
{ where: { id: option[1] } }
)
.then(authors => {
viewAuthor.updateView(option[1]);
})
break;
case "erase":
model.authors.destroy({
where: { id: option[0] }
})
.then(authors => {
viewAuthor.destroyView(option[0]);
})
break;
}
}
}

module.exports = AuthorController;
60 changes: 60 additions & 0 deletions controllers/TagController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const model = require('../models/index')
const viewTag = require('../views/tag');

class TagController {
static menu(command, option) {
switch (command) {
case "add":
model.tags.create({
name : option[0]
})
.then(tag => {
viewTag.addView(option[0]);
})
break;
case "read_one":
model.tags.findAll({
where: {
id: option[0]
},
raw: true
})
.then(tags => {
viewTag.read_oneView(tags)
})
break;
case "read_all":
model.tags.findAll({ raw: true })
.then(tags => {
viewTag.read_allView(tags)
})
break;
case "update":
let updateField = option[0].split(',');
let updateObj = {}
for (let i = 0; i < updateField.length; i++) {
let splittedKey = updateField[i].split(':')
updateObj[splittedKey[0]] = splittedKey[1];
}

model.tags.update(
updateObj,
{ where: { id: option[1] } }
)
.then(tags => {
viewTag.updateView(option[1]);
})
break;
case "erase":
model.tags.destroy({
where: { id: option[0] }
})
.then(tags => {
viewTag.destroyView(option[0]);
})
break;
}
}
}

module.exports = TagController;
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const controller = require('./controllers/controller');
const ArticleController = require('./controllers/ArticleController');
const TagController = require('./controllers/TagController');
const AuthorController = require('./controllers/AuthorController');
// const controller = require('./controllers/controller');
let myArgv = process.argv;


switch(myArgv[2]) {
case "author":
AuthorController.menu(myArgv[3],myArgv.slice(4))
break;
case "tag":
TagController.menu(myArgv[3],myArgv.slice(4))
break;
case "article":
ArticleController.menu(myArgv[3],myArgv.slice(4))
break;
}
36 changes: 36 additions & 0 deletions migrations/20180221042106-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.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/20180221044427-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/20180221044644-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.TEXT,
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;
};
Loading