diff --git a/sequelize.js b/app/config/db.config.js similarity index 69% rename from sequelize.js rename to app/config/db.config.js index 8557d55..c697549 100644 --- a/sequelize.js +++ b/app/config/db.config.js @@ -1,7 +1,7 @@ const Sequelize = require('sequelize'), - DonorsModel = require('./models/Donors.js'), - CampaignsModel = require('./models/Campaigns.js'); - + DonorsModel = require('./models/donors.model.js'), + CampaignsModel = require('./models/campaigns.model.js'); + const host = 'am1shyeyqbxzy8gc.cbetxkdyhwsb.us-east-1.rds.amazonaws.com', username = 'fyro63k2989tyibh', @@ -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 Campaigns = CampaignsModel(sequelize, Sequelize); +db.donors = Donors; +db.campaigns = Campaigns; -sequelize.sync({ force: true }) - .then(() => { - console.log(`Database & tables created!`) - }) +db.Sequelize = Sequelize; +db.sequelize = sequelize; -module.exports = { - Donors, - Campaigns -} \ No newline at end of file +module.exports = db; \ No newline at end of file diff --git a/app/controllers/donors.controller.js b/app/controllers/donors.controller.js new file mode 100644 index 0000000..d257a93 --- /dev/null +++ b/app/controllers/donors.controller.js @@ -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); + }); +}; \ No newline at end of file diff --git a/models/Campaigns.js b/app/models/campaigns.model.js similarity index 88% rename from models/Campaigns.js rename to app/models/campaigns.model.js index 4c97489..cf55878 100644 --- a/models/Campaigns.js +++ b/app/models/campaigns.model.js @@ -19,10 +19,5 @@ module.exports = (sequelize, DataTypes) => { freezeTableName: true, } ); - - Campaigns.associate = (models) => { - Campaigns.belongsTo(models.donors); - }; - return Campaigns; } \ No newline at end of file diff --git a/models/Donors.js b/app/models/donors.model.js similarity index 88% rename from models/Donors.js rename to app/models/donors.model.js index 5226d4e..eb32163 100644 --- a/models/Donors.js +++ b/app/models/donors.model.js @@ -19,10 +19,5 @@ module.exports = (sequelize, DataTypes) => { freezeTableName: true, } ); - - Donors.associate = (models) => { - Donors.hasMany(models.campaigns); - }; - return Donors; } \ No newline at end of file diff --git a/app/routes/donors.route.js b/app/routes/donors.route.js new file mode 100644 index 0000000..45f2b68 --- /dev/null +++ b/app/routes/donors.route.js @@ -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); +} \ No newline at end of file diff --git a/package.json b/package.json index f1b7cc1..01597f5 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/server.js b/server.js index ef2eaab..85262ee 100644 --- a/server.js +++ b/server.js @@ -2,29 +2,42 @@ const express = require('express'), app = express(), router = express.Router(), + bodyParser = require('body-parser'), 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 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/route/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); - -// //verify connection to db -// sequelize -// .authenticate() -// .then(() => { -// console.log('Connection has been established successfully.'); -// }) -// .catch(err => { -// console.error('Unable to connect to the database:', err); -// }); +// 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 http://%s:%s", host, port) +}) \ No newline at end of file