whole structure done based on sequelize reccommendation
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
const Sequelize = require('sequelize'),
|
const Sequelize = require('sequelize'),
|
||||||
DonorsModel = require('./models/Donors.js'),
|
DonorsModel = require('./models/donors.model.js'),
|
||||||
CampaignsModel = require('./models/Campaigns.js');
|
CampaignsModel = require('./models/campaigns.model.js');
|
||||||
|
|
||||||
|
|
||||||
const host = 'am1shyeyqbxzy8gc.cbetxkdyhwsb.us-east-1.rds.amazonaws.com',
|
const host = 'am1shyeyqbxzy8gc.cbetxkdyhwsb.us-east-1.rds.amazonaws.com',
|
||||||
@@ -28,16 +28,15 @@ const sequelize = new Sequelize(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
//creating tables from external files
|
const db = {};
|
||||||
|
|
||||||
|
//creating tables/models from imported function
|
||||||
const Donors = DonorsModel(sequelize, Sequelize);
|
const Donors = DonorsModel(sequelize, Sequelize);
|
||||||
const Campaigns = CampaignsModel(sequelize, Sequelize);
|
const Campaigns = CampaignsModel(sequelize, Sequelize);
|
||||||
|
db.donors = Donors;
|
||||||
|
db.campaigns = Campaigns;
|
||||||
|
|
||||||
sequelize.sync({ force: true })
|
db.Sequelize = Sequelize;
|
||||||
.then(() => {
|
db.sequelize = sequelize;
|
||||||
console.log(`Database & tables created!`)
|
|
||||||
})
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = db;
|
||||||
Donors,
|
|
||||||
Campaigns
|
|
||||||
}
|
|
||||||
@@ -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);
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -19,10 +19,5 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
freezeTableName: true,
|
freezeTableName: true,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
Campaigns.associate = (models) => {
|
|
||||||
Campaigns.belongsTo(models.donors);
|
|
||||||
};
|
|
||||||
|
|
||||||
return Campaigns;
|
return Campaigns;
|
||||||
}
|
}
|
||||||
@@ -19,10 +19,5 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
freezeTableName: true,
|
freezeTableName: true,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
Donors.associate = (models) => {
|
|
||||||
Donors.hasMany(models.campaigns);
|
|
||||||
};
|
|
||||||
|
|
||||||
return Donors;
|
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);
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/talksik/Ucharify_api#readme",
|
"homepage": "https://github.com/talksik/Ucharify_api#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"body-parser": "^1.18.3",
|
||||||
"express": "^4.16.4",
|
"express": "^4.16.4",
|
||||||
"mysql2": "^1.6.4",
|
"mysql2": "^1.6.4",
|
||||||
"sequelize": "^4.42.0"
|
"sequelize": "^4.42.0"
|
||||||
|
|||||||
@@ -2,29 +2,42 @@
|
|||||||
const express = require('express'),
|
const express = require('express'),
|
||||||
app = express(),
|
app = express(),
|
||||||
router = express.Router(),
|
router = express.Router(),
|
||||||
|
bodyParser = require('body-parser'),
|
||||||
port = process.env.PORT || 4200,
|
port = process.env.PORT || 4200,
|
||||||
sequelize = require('./sequelize.js');
|
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
|
//define a route, usually this would be a bunch of routes imported from another file
|
||||||
router.get('/', function (req, res, next) {
|
router.get('/', function (req, res, next) {
|
||||||
res.send('Welcome to the Ucharify API');
|
res.send('Welcome to the Ucharify API');
|
||||||
});
|
});
|
||||||
|
|
||||||
//add routes to express app
|
//adding routes to Express app
|
||||||
// routes(app);
|
require('./app/route/donors.route.js')(app);
|
||||||
|
|
||||||
//start Express server on defined port
|
// Create a Server
|
||||||
app.listen(port);
|
var server = app.listen(port, function () {
|
||||||
|
|
||||||
//log to console to let us know it's working
|
var host = server.address().address
|
||||||
console.log('Ucharify API server started on: ' + port);
|
var port = server.address().port
|
||||||
|
|
||||||
// //verify connection to db
|
//server is successful
|
||||||
// sequelize
|
console.log("App listening at http://%s:%s", host, port)
|
||||||
// .authenticate()
|
})
|
||||||
// .then(() => {
|
|
||||||
// console.log('Connection has been established successfully.');
|
|
||||||
// })
|
|
||||||
// .catch(err => {
|
|
||||||
// console.error('Unable to connect to the database:', err);
|
|
||||||
// });
|
|
||||||
Reference in New Issue
Block a user