whole structure done based on sequelize reccommendation
This commit is contained in:
@@ -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
|
||||
}
|
||||
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);
|
||||
});
|
||||
};
|
||||
@@ -19,10 +19,5 @@ module.exports = (sequelize, DataTypes) => {
|
||||
freezeTableName: true,
|
||||
}
|
||||
);
|
||||
|
||||
Campaigns.associate = (models) => {
|
||||
Campaigns.belongsTo(models.donors);
|
||||
};
|
||||
|
||||
return Campaigns;
|
||||
}
|
||||
@@ -19,10 +19,5 @@ module.exports = (sequelize, DataTypes) => {
|
||||
freezeTableName: true,
|
||||
}
|
||||
);
|
||||
|
||||
Donors.associate = (models) => {
|
||||
Donors.hasMany(models.campaigns);
|
||||
};
|
||||
|
||||
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",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.18.3",
|
||||
"express": "^4.16.4",
|
||||
"mysql2": "^1.6.4",
|
||||
"sequelize": "^4.42.0"
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
Reference in New Issue
Block a user