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

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_orm",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
92 changes: 92 additions & 0 deletions controller/article.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const sequelize = require('sequelize')
const View = require('../views/views.js')
const {article} = require('../models')

class Article {
constructor() {
}

static showAll(){
article.findAll()
.then(function(data){
let arr = [];
for(let i=0;i<data.length;i++){

arr.push(data[i].dataValues);
}
View.showArticle(arr);
process.exit()
}).catch(function(err){
console.log('error pada saat baca data article');
process.exit();
})
}

static showOne(data){
article.findOne({
where:{
id:data
}
}).then(function(data){
View.showArticle([data.dataValues])
process.exit()
}).catch(function(err){
console.log('error pas baca nyari data satu article');
process.exit()
})
}

static add(data){
let objArticle = {};
objArticle.title = data[0];
objArticle.body = data[1];
objArticle.id_author = data[2];
objArticle.id_tag = data[3];
article.create(objArticle)
.then(function(data){
console.log('isi data article sukses');
process.exit();
}).catch(function(err){
console.log('error pas masukin data');
process.exit();
})
}

static edit(target,edit){
let objArticle = {};
objArticle.title = edit[0];
objArticle.body = edit[1];
objArticle.id_author = edit[2];
objArticle.id_tag = edit[3];
article.create(objArticle)
article.update(objArticle,{
where:{
id:target
}
})
.then(function(data){
console.log(data);
console.log('edit data tags sukses');
process.exit()
}).catch(function(err){
console.log('error pas edit data');
process.exit()
})
}

static delete(data){
article.destroy({
where:{
id:data
}
}).then(function(data){
console.log('data berhasil di delete');
process.exit()
}).catch(function(err){
console.log('error pas baca nyari data satu tags');
process.exit()
})
}
}

module.exports = Article
92 changes: 92 additions & 0 deletions controller/author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const sequelize = require('sequelize')
const View = require('../views/views.js')
const {author} = require('../models')

class Author {
constructor() {
}

static showAll(){
author.findAll()
.then(function(data){
let arr = [];
for(let i=0;i<data.length;i++){
arr.push(data[i].dataValues);
}
View.showList(arr);
process.exit()
}).catch(function(err){
console.log('error pada saat baca data author');
process.exit();
})
}

static showOne(data){
author.findOne({
where:{
id:data
}
}).then(function(data){
View.showList([data.dataValues])
process.exit()
}).catch(function(err){
console.log('error pas baca nyari data satu author');
process.exit()
})
}

static add(data){
let objAuthor = {};
objAuthor.first_name = data[0];
objAuthor.last_name = data[1];
objAuthor.religion = data[2];
objAuthor.gender = data[3];
objAuthor.age = data[4];
author.create(objAuthor)
.then(function(data){
console.log('isi data author sukses');
process.exit();
}).catch(function(err){
console.log('error pas masukin data');
process.exit();
})
}

static edit(target,data){
let objAuthor = {};
objAuthor.first_name = data[0];
objAuthor.last_name = data[1];
objAuthor.religion = data[2];
objAuthor.gender = data[3];
objAuthor.age = data[4];
author.update(objAuthor,{
where:{
id:target
}
})
.then(function(data){
console.log('edit data author sukses');
process.exit()
}).catch(function(err){
console.log('error pas edit data');
process.exit()
})
}

static showOne(data){
author.destroy({
where:{
id:data
}
}).then(function(data){
console.log('data berhasil di delete');
process.exit()
}).catch(function(err){
console.log('error pas baca nyari data satu author');
process.exit()
})
}

}

module.exports = Author
86 changes: 86 additions & 0 deletions controller/tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const sequelize = require('sequelize')
const View = require('../views/views.js')
const {tags} = require('../models')

class Tags{
constructor() {
}

static showAll(){
tags.findAll()
.then(function(data){
let arr = [];
for(let i=0;i<data.length;i++){
arr.push(data[i].dataValues);
}
View.showTags(arr);
process.exit()
}).catch(function(err){
console.log('error pada saat baca data tags');
process.exit();
})
}

static showOne(data){
tags.findOne({
where:{
id:data
}
}).then(function(data){
View.showTags([data.dataValues])
process.exit()
}).catch(function(err){
console.log('error pas baca nyari data satu tags');
process.exit()
})
}

static add(data){
let objTags = {};
objTags.name = data[0];
tags.create(objTags)
.then(function(data){
console.log('isi data tags sukses');
process.exit();
}).catch(function(err){
console.log('error pas masukin data');
process.exit();
})
}

static edit(target,edit){
let objTags = {
name : edit[0]
}
tags.update(objTags,{
where:{
id:target
}
})
.then(function(data){
console.log(data);
console.log('edit data tags sukses');
process.exit()
}).catch(function(err){
console.log('error pas edit data');
process.exit()
})
}

static delete(data){
tags.destroy({
where:{
id:data
}
}).then(function(data){
console.log('data berhasil di delete');
process.exit()
}).catch(function(err){
console.log('error pas baca nyari data satu tags');
process.exit()
})
}

}

module.exports = Tags
79 changes: 79 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const Author = require('./controller/author.js')
const Tag = require('./controller/tags.js')
const Article = require('./controller/article.js')
const View = require('./views/views.js')

class Index{
constructor(){}

static author(data){
let command = data[0]
let datas = data.slice(1,data.length)
let id = data[1]
let edit = data.slice(2,data.length)
if(command === 'add'){
Author.add(datas)
}else if(command === 'showOne'){
Author.showOne(datas)
}else if(command === 'showAll'){
Author.showAll()
}else if(command === 'edit'){
Author.edit(id,edit)
}else if(command === 'delete'){
Author.delete(datas)
}
}

static tags(data){
let command = data[0]
let datas = data.slice(1)
let id = data[1]
let edit = data.slice(2)
if(command === 'add'){
Tag.add(datas)
}else if(command === 'showOne'){
Tag.showOne(datas)
}else if(command === 'showAll'){
Tag.showAll()
}else if(command === 'edit'){
Tag.edit(id,edit)
}else if(command === 'delete'){
Tag.delete(datas)
}
}

static article(data){
let command = data[0]
let datas = data.slice(1,data.length)
let id = data[1]
let edit = data.slice(2,data.length)
if(command === 'add'){
Article.add(datas)
}else if(command === 'showOne'){
Article.showOne(datas)
}else if(command === 'showAll'){
Article.showAll()
}else if(command === 'edit'){
Article.edit(id,edit)
}else if(command === 'delete'){
Article.delete(datas)
}
}
}

let argv = process.argv.slice(2,process.argv.length)
let command = argv[0]
let data = argv.slice(1,process.length)

if(command === 'author'){
Index.author(data)
process.exit()
} else if(command === 'tags'){
Index.tags(data)
process.exit()
} else if(command === 'article'){
Index.article(data)
process.exit()
} else {
View.showHelp()
}
Loading