whole structure done based on sequelize reccommendation
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
const Sequelize = require('sequelize'),
|
||||
DonorsModel = require('./models/donors.model.js'),
|
||||
CampaignsModel = require('./models/campaigns.model.js');
|
||||
|
||||
|
||||
const host = 'am1shyeyqbxzy8gc.cbetxkdyhwsb.us-east-1.rds.amazonaws.com',
|
||||
username = 'fyro63k2989tyibh',
|
||||
password = 'ykjkyenyvxig208z',
|
||||
port = '3306',
|
||||
database = 'n0j9gxnf4ijr7g8t';
|
||||
|
||||
const sequelize = new Sequelize(
|
||||
database,
|
||||
username,
|
||||
password,
|
||||
{
|
||||
host: host,
|
||||
dialect: 'mysql',
|
||||
operatorsAliases: false,
|
||||
|
||||
// research for pool/connections
|
||||
pool: {
|
||||
max: 5,
|
||||
min: 0,
|
||||
acquire: 30000,
|
||||
idle: 10000
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
const db = {};
|
||||
|
||||
//creating tables/models from imported function
|
||||
const Donors = DonorsModel(sequelize, Sequelize);
|
||||
const Campaigns = CampaignsModel(sequelize, Sequelize);
|
||||
db.donors = Donors;
|
||||
db.campaigns = Campaigns;
|
||||
|
||||
db.Sequelize = Sequelize;
|
||||
db.sequelize = sequelize;
|
||||
|
||||
module.exports = db;
|
||||
@@ -0,0 +1,50 @@
|
||||
const db = require('../config/db.config.js');
|
||||
const Donors = db.donors;
|
||||
|
||||
// Post a Customer
|
||||
exports.create = (req, res) => {
|
||||
// Save to MySQL database
|
||||
Donors.create({
|
||||
firstname: req.body.firstname,
|
||||
lastname: req.body.lastname,
|
||||
age: req.body.age
|
||||
}).then(customer => {
|
||||
// Send created customer to client
|
||||
res.send(customer);
|
||||
});
|
||||
};
|
||||
|
||||
// FETCH all Customers
|
||||
exports.findAll = (req, res) => {
|
||||
Donors.findAll().then(customers => {
|
||||
// Send all customers to Client
|
||||
res.send(customers);
|
||||
});
|
||||
};
|
||||
|
||||
// Find a Customer by Id
|
||||
exports.findById = (req, res) => {
|
||||
Donors.findById(req.params.customerId).then(customer => {
|
||||
res.send(customer);
|
||||
})
|
||||
};
|
||||
|
||||
// Update a Customer
|
||||
exports.update = (req, res) => {
|
||||
const id = req.params.customerId;
|
||||
Donors.update( { firstname: req.body.firstname, lastname: req.body.lastname, age: req.body.age },
|
||||
{ where: {id: req.params.customerId} }
|
||||
).then(() => {
|
||||
res.status(200).send("updated successfully a customer with id = " + id);
|
||||
});
|
||||
};
|
||||
|
||||
// Delete a Customer by Id
|
||||
exports.delete = (req, res) => {
|
||||
const id = req.params.customerId;
|
||||
Donors.destroy({
|
||||
where: { id: id }
|
||||
}).then(() => {
|
||||
res.status(200).send('deleted successfully a customer with id = ' + id);
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Campaigns = sequelize.define('campaigns', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
first_name: DataTypes.STRING,
|
||||
middle_name: DataTypes.STRING,
|
||||
last_name: DataTypes.STRING,
|
||||
email: DataTypes.STRING,
|
||||
phone: DataTypes.STRING,
|
||||
address: DataTypes.STRING,
|
||||
city: DataTypes.STRING,
|
||||
state: DataTypes.STRING,
|
||||
country: DataTypes.STRING,
|
||||
},
|
||||
{
|
||||
freezeTableName: true,
|
||||
}
|
||||
);
|
||||
return Campaigns;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
const Donors = sequelize.define('donors', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true
|
||||
},
|
||||
first_name: DataTypes.STRING,
|
||||
middle_name: DataTypes.STRING,
|
||||
last_name: DataTypes.STRING,
|
||||
email: DataTypes.STRING,
|
||||
phone: DataTypes.STRING,
|
||||
address: DataTypes.STRING,
|
||||
city: DataTypes.STRING,
|
||||
state: DataTypes.STRING,
|
||||
country: DataTypes.STRING,
|
||||
},
|
||||
{
|
||||
freezeTableName: true,
|
||||
}
|
||||
);
|
||||
return Donors;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
module.exports = function(app) {
|
||||
|
||||
const donors = require('../controller/donors.controller.js');
|
||||
|
||||
// Create a new Donor
|
||||
app.post('/api/donors', donors.create);
|
||||
|
||||
// Retrieve all Donor
|
||||
app.get('/api/donors', donors.findAll);
|
||||
|
||||
// Retrieve a single Donor by Id
|
||||
app.get('/api/donors/:DonorId', donors.findById);
|
||||
|
||||
// Update a Donor with Id
|
||||
app.put('/api/donors/:DonorId', donors.update);
|
||||
|
||||
// Delete a Donor with Id
|
||||
app.delete('/api/donors/:DonorId', donors.delete);
|
||||
}
|
||||
Reference in New Issue
Block a user