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 0 #1

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": "purge",
"password": "creatEpwD",
"database": "conaddress",
"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"
}
}
65 changes: 65 additions & 0 deletions controllers/AddressController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const models = require('../models/index')
const AddressView = require('../views/AddressView')

class AddressController {
static menu(command, option) {
switch(command) {
case "list":
models.Address.findAll({raw:true})
.then(address => {
AddressView.listView(address);
})
.catch(err => {
AddressView.errorView(err);
})
break;
case "add":
models.Address.create({
street: option[0],
city: option[1],
zip_code: option[2]
})
.then(() => {
AddressView.addView(option);
})
.catch(err => {
AddressView.errorView(err);
})
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];
}

models.Address.update(
updateObj,
{ where: { id: option[1] } }
)
.then(() => {
AddressView.updateView(option[1]);
})
.catch(err => {
AddressView.errorView(err);
})
break;
case "delete":
models.Address.destroy({
where : {
id : option[0]
}
})
.then(() => {
AddressView.deleteView(option[0]);
})
.catch(err => {
AddressView.errorView(err);
})
break;
}
}
}

module.exports = AddressController;
65 changes: 65 additions & 0 deletions controllers/ContactController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const models = require('../models/index')
const ContactView = require('../views/ContactView')

class ContactController {
static menu(command, option) {
switch(command) {
case "list":
models.Contact.findAll({raw:true})
.then(contact => {
ContactView.listView(contact);
})
.catch(err => {
ContactView.errorView(err);
})
break;
case "add":
models.Contact.create({
name: option[0],
email: option[1],
phone: option[2]
})
.then(() => {
ContactView.addView(option);
})
.catch(err => {
ContactView.errorView(err);
})
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];
}

models.Contact.update(
updateObj,
{ where: { id: option[1] } }
)
.then(() => {
ContactView.updateView(option[1]);
})
.catch(err => {
ContactView.errorView(err);
})
break;
case "delete":
models.Contact.destroy({
where : {
id : option[0]
}
})
.then(() => {
ContactView.deleteView(option[0]);
})
.catch(err => {
ContactView.errorView(err);
})
break;
}
}
}

module.exports = ContactController;
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const AddressController = require('./controllers/AddressController')
const ContactController = require('./controllers/ContactController')

let myArgv = process.argv;

let arrCommand = myArgv[2].split(':');

switch(arrCommand[0]) {
case "contacts":
ContactController.menu(arrCommand[1],myArgv.slice(3));
break;
case "addresses":
AddressController.menu(arrCommand[1],myArgv.slice(3));
break;
}
33 changes: 33 additions & 0 deletions migrations/20180222045122-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/20180222045226-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.INTEGER
},
zip_code: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Addresses');
}
};
29 changes: 29 additions & 0 deletions migrations/20180222055648-Change-City-Type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'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.changeColumn('Addresses', 'city',
{
type: Sequelize.STRING,
allowNull: false
}
)
},

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.INTEGER,
zip_code: DataTypes.STRING
}, {});
Address.associate = function(models) {
// associations can be defined here
};
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) {
// 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