@@ -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,58 @@
|
||||
const db = require('../config/db.config.js');
|
||||
const Donors = db.donors;
|
||||
|
||||
// Post a Donor
|
||||
exports.create = (req, res) => {
|
||||
// Save to MySQL database
|
||||
Donors.create({
|
||||
first_name: req.body.first_name,
|
||||
middle_name: req.body.middle_name,
|
||||
last_name: req.body.last_name,
|
||||
email: req.body.email,
|
||||
age: req.body.age,
|
||||
phone: req.body.phone,
|
||||
address: req.body.address,
|
||||
city: req.body.city,
|
||||
state: req.body.state,
|
||||
country: req.body.country
|
||||
}).then(donor => {
|
||||
// Send created donor to client
|
||||
res.send(donor);
|
||||
});
|
||||
};
|
||||
|
||||
// FETCH all Donors
|
||||
exports.findAll = (req, res) => {
|
||||
Donors.findAll().then(donors => {
|
||||
// Send all donors to Client
|
||||
res.send(donors);
|
||||
});
|
||||
};
|
||||
|
||||
// Find a Donor by Id
|
||||
exports.findById = (req, res) => {
|
||||
Donors.findById(req.params.donor_id).then(donor => {
|
||||
res.send(donor);
|
||||
})
|
||||
};
|
||||
|
||||
// Delete a Donor by Id
|
||||
exports.delete = (req, res) => {
|
||||
const id = req.params.donor_id;
|
||||
Donors.destroy({
|
||||
where: { id: id }
|
||||
}).then(() => {
|
||||
res.status(200).send('deleted successfully a donor with id = ' + id);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
// // Update a Donor
|
||||
// exports.update = (req, res) => {
|
||||
// const id = req.params.donor_id;
|
||||
// Donors.update( { firstname: req.body.firstname, lastname: req.body.lastname, age: req.body.age },
|
||||
// { where: {id: req.params.donorId} }
|
||||
// ).then(() => {
|
||||
// res.status(200).send("updated successfully a donor 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,24 @@
|
||||
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,
|
||||
age: DataTypes.INTEGER,
|
||||
phone: DataTypes.INTEGER,
|
||||
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('../controllers/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);
|
||||
|
||||
// Delete a Donor with Id
|
||||
app.delete('/api/donors/:DonorId', donors.delete);
|
||||
|
||||
// // Update a Donor with Id
|
||||
// app.put('/api/donors/:DonorId', donors.update);
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/talksik/Ucharify_api#readme",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.18.3",
|
||||
"express": "^4.16.4",
|
||||
"mysql2": "^1.6.4",
|
||||
"sequelize": "^4.42.0"
|
||||
|
||||
@@ -2,7 +2,26 @@
|
||||
const express = require('express'),
|
||||
app = express(),
|
||||
router = express.Router(),
|
||||
port = process.env.PORT || 4200;
|
||||
bodyParser = require('body-parser'),
|
||||
port = process.env.PORT || 4200,
|
||||
db = require('./app/config/db.config.js');
|
||||
|
||||
app.use(bodyParser.json())
|
||||
|
||||
//verify connection to db
|
||||
db.sequelize
|
||||
.authenticate()
|
||||
.then(() => {
|
||||
console.log('Connection has been established successfully.');
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('Unable to connect to the database:', err);
|
||||
});
|
||||
|
||||
// force: true will drop the table if it already exists
|
||||
db.sequelize.sync({force: true}).then(() => {
|
||||
console.log('Drop and Resync with { force: true }');
|
||||
});
|
||||
|
||||
|
||||
//define a route, usually this would be a bunch of routes imported from another file
|
||||
@@ -10,11 +29,15 @@ router.get('/', function (req, res, next) {
|
||||
res.send('Welcome to the Ucharify API');
|
||||
});
|
||||
|
||||
//add routes to express app
|
||||
// routes(app);
|
||||
//adding routes to Express app
|
||||
require('./app/routes/donors.route.js')(app);
|
||||
|
||||
//start Express server on defined port
|
||||
app.listen(port);
|
||||
|
||||
//log to console to let us know it's working
|
||||
console.log('Ucharify API server started on: ' + port);
|
||||
// Create a Server
|
||||
var server = app.listen(port, function () {
|
||||
|
||||
var host = server.address().address;
|
||||
var port = server.address().port;
|
||||
|
||||
//server is successful
|
||||
console.log(`App listening at port: ${port}`)
|
||||
})
|
||||
Reference in New Issue
Block a user