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

not finished #22

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
10 changes: 10 additions & 0 deletions addresses.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1,711-2880 Nulla St,Mississippi,96522,1
2,8562 Fusce Rd,Nebraska,20620,4
3,606-3727 Ullamcorper Street,Roseville,11523,2
4,867-859 Sit Rd,New York,39531,4
5,7292 Dictum Av,San Antonio,47096,1
6,651-8679 Sodales Av,Tamuning,10855,5
7,191-103 Integer Rd,Corona New Mexico,08219,3
8,2508 Dolor. Av,Muskegon KY,12482,2
9,666-4366 Lacinia Avenue,Ohio,19253,3
10,Lacinia Road,San Bernardino,09289,5
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": "fitrul31",
"password": "fitrul31",
"database": "tugasKamis2",
"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"
}
}
5 changes: 5 additions & 0 deletions contacts.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1,Lani Rollins,[email protected],1-633-389-7173
2,McKenzie Burris,[email protected],1-906-235-0832
3,Amethyst Morgan,[email protected],1-548-366-6273
4,Lamar Hardin,[email protected],1-519-693-8091
5,Keegan Coleman,[email protected],1-998-626-8896
33 changes: 33 additions & 0 deletions controller/addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const models = require('../models');

class Addresses {
static list(){
// models.Address.findAll({raw:true}).then((dataAddress) => {
// console.log(dataAddress);
// });
models.Address.findAll({include: [{model:models.Contact}]}).then((dataAddress) => {
console.log(dataAddress);
process.exit();
});
}
static add(input3){
models.Address.create({street:input3[0],city:input3[1],zip_code:input3[2]})
.then((dataAddress) => {
models.Address.findAll({raw:true}).then((dataAddress) => {
console.log(dataAddress[dataAddress.length-1]);
});
});
}
static update(input3){
models.Address.update({
street:input3[1],city:input3[2],zip_code:input3[3]},
{where:{id:
input3[0]}});
}
static delete(input3){
models.Address.destroy({
where:{id:input3}
});
}
}
module.exports = Addresses;
33 changes: 33 additions & 0 deletions controller/contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const models = require('../models');

class Contacts {
static list(){
// models.Contact.findAll({raw:true}).then((dataContact) => {
// console.log(dataContact);
// });
models.Contact.findAll({include: [{model:models.Address}]}).then((dataContact) => {
console.log(dataContact);
process.exit();
});
}
static add(input3){
models.Contact.create({name:input3[0],email:input3[1],phone:input3[2]})
.then((dataContact) => {
models.Contact.findAll({raw:true}).then((dataContact) => {
console.log(dataContact[dataContact.length-1]);
});
});
}
static update(input3){
models.Contact.update({
name:input3[1],email:input3[2],phone:input3[3]},
{where:{id:
input3[0]}});
}
static delete(input3){
models.Contact.destroy({
where:{id:input3}
});
}
}
module.exports = Contacts;
36 changes: 36 additions & 0 deletions controller/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const Contacts = require('./contacts.js');
const Addresses = require('./addresses.js');

class Controller {
static command(input1,input2,input3){
if(input1==='contacts'){
if(input2==='list'){
Contacts.list();
} else if(input2==='add'){
Contacts.add(input3);
} else if(input2==='update'){
Contacts.update(input3);
} else if(input2==='delete'){
Contacts.delete(input3);
} else {
console.log('Wrong command');
}
} else if(input1==='addresses'){
if(input2==='list'){
Addresses.list();
} else if(input2==='add'){
Addresses.add(input3);
} else if(input2==='update'){
Addresses.update(input3);
} else if(input2==='delete'){
Addresses.delete(input3);
} else {
console.log('Wrong command');
}
} else {
console.log('Wrong command');
}
}
}

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('./controller/controller.js');
const argv = process.argv;

Controller.command(argv[2], argv[3], argv.slice(4));
33 changes: 33 additions & 0 deletions migrations/20180225065540-create-contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Contacts', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
phone: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Contacts');
}
};
33 changes: 33 additions & 0 deletions migrations/20180225065638-create-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Addresses', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
street: {
type: Sequelize.STRING
},
city: {
type: Sequelize.STRING
},
zipCode: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Addresses');
}
};
24 changes: 24 additions & 0 deletions migrations/20180225065742-addNewColumnOnAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
return queryInterface.addColumn('Addresses','ContactsId',{type: Sequelize.INTEGER});
},

down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.dropTable('users');
*/
}
};
12 changes: 12 additions & 0 deletions models/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Address = sequelize.define('Address', {
street: DataTypes.STRING,
city: DataTypes.STRING,
zipCode: DataTypes.STRING
}, {});
Address.associate = function(models) {
Address.belongsTo(models.Contact,{foreignKey:'ContactsId'});
};
return Address;
};
12 changes: 12 additions & 0 deletions models/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Contact = sequelize.define('Contact', {
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.STRING
}, {});
Contact.associate = function(models) {
Contact.hasMany(models.Address, {foreignKey:'ContactsId'})
};
return Contact;
};
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;
Loading