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

Parsing done, CRUD done #6

Open
wants to merge 3 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": "hacktiv8",
"password": null,
"database": "database_development",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "hacktiv8",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"username": "hacktiv8",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
62 changes: 62 additions & 0 deletions controllers/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict'
const {Contact,Address} = require('../models')
const View = require('../views');

class Controller_address {

static getCommand ( command,input ) {
if( command === 'list' ) {
Address.findAll()
.then(addresses => {
let arrOfaddress = []
addresses.forEach(address => {
arrOfaddress.push(address.dataValues)
})
View.show(arrOfaddress)
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'add' ) {
let objAddress = {}
objAddress.street = input[0]
objAddress.city = input[1]
objAddress.zip_code = +input[2]
Address.create(objAddress)
.then(address => {
View.show(`Successfully add into Addresses`);
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'update' ) {
let objAddress = {}
objAddress[input[1]] = input[2]
Address.update(objAddress,{
where: {id: +input[0]}
})
.then(address => {
View.show(`Update success`);
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'delete') {
Address.destroy({
where: {id: +input}
})
.then(address => {
View.show(`Delete success`)
process.exit()
})
.catch(err => {
console.log(err);
})
}
}
}

module.exports = Controller_address
61 changes: 61 additions & 0 deletions controllers/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'
const {Contact,Address} = require('../models')
const View = require('../views');

class Controller_contact {

static getCommand ( command,input ) {
if( command === 'list' ) {
Contact.findAll()
.then(contacts => {
let arrOfcontact = []
contacts.forEach(contact => {
arrOfcontact.push(contact.dataValues)
})
View.show(arrOfcontact)
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'add' ) {
let objContact = {}
objContact.name = input[0]
objContact.email = input[1]
objContact.phone = input[2]
Contact.create(objContact)
.then(contact => {
View.show(`Successfully add into Contacts`);
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'update' ) {
let objContact = {}
objContact[input[1]] = input[2]
Contact.update(objContact,{
where: {id: +input[0]}
})
.then(contact => {
View.show(`Update success`);
process.exit()
})
.catch(err => {
console.log(err);
})
} else if ( command === 'delete') {
Contact.destroy({
where: {id: +input}
})
.then(contact => {
View.show(`Delete success`)
})
.catch(err => {
console.log(err);
})
}
}
}

module.exports = Controller_contact;
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Address = require ('./controllers/address')
const Contact = require ('./controllers/contact')

const dataFromArgv = process.argv[2].split(':')
const payload = process.argv.splice(3)
const table = dataFromArgv[0]
const command = dataFromArgv[1]

if(table === 'contacts') {
Contact.getCommand(command,payload)
} else if(table === 'addresses') {
Address.getCommand(command,payload)
}
33 changes: 33 additions & 0 deletions migrations/20180222055836-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: true,
type: Sequelize.DATE
},
updatedAt: {
allowNull: true,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Contacts');
}
};
33 changes: 33 additions & 0 deletions migrations/20180222055843-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
},
zip_code: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: true,
type: Sequelize.DATE
},
updatedAt: {
allowNull: true,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Addresses');
}
};
15 changes: 15 additions & 0 deletions models/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Address = sequelize.define('Address', {
street: DataTypes.STRING,
city: DataTypes.STRING,
zip_code: DataTypes.INTEGER
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return Address;
};
15 changes: 15 additions & 0 deletions models/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Contact = sequelize.define('Contact', {
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
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