Skip to content

Commit 41ba823

Browse files
committed
Build Nodejs CRUD Application with MySQL/PostgreSQL – Express RestAPIs + Ajax : Post/Get/Put/Delete Request
1 parent 8485514 commit 41ba823

File tree

14 files changed

+1388
-0
lines changed

14 files changed

+1388
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const env = require('./env.js');
2+
3+
const Sequelize = require('sequelize');
4+
const sequelize = new Sequelize(env.database, env.username, env.password, {
5+
host: env.host,
6+
dialect: env.dialect,
7+
operatorsAliases: false,
8+
9+
pool: {
10+
max: env.max,
11+
min: env.pool.min,
12+
acquire: env.pool.acquire,
13+
idle: env.pool.idle
14+
}
15+
});
16+
17+
const db = {};
18+
19+
db.Sequelize = Sequelize;
20+
db.sequelize = sequelize;
21+
22+
db.Customer = require('../models/customer.model.js')(sequelize, Sequelize);
23+
24+
module.exports = db;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const env = {
2+
database: 'loizenaidb',
3+
username: 'root',
4+
password: '12345',
5+
host: 'localhost',
6+
dialect: 'mysql',
7+
pool: {
8+
max: 5,
9+
min: 0,
10+
acquire: 30000,
11+
idle: 10000
12+
}
13+
};
14+
15+
module.exports = env;
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
const db = require('../config/db.config.js');
2+
const Customer = db.Customer;
3+
4+
/**
5+
* Save a Customer object to database MySQL/PostgreSQL
6+
* @param {*} req
7+
* @param {*} res
8+
*/
9+
exports.create = (req, res) => {
10+
let customer = {};
11+
12+
try{
13+
// Building Customer object from upoading request's body
14+
customer.firstname = req.body.firstname;
15+
customer.lastname = req.body.lastname;
16+
customer.address = req.body.address;
17+
customer.age = req.body.age;
18+
19+
// Save to MySQL database
20+
Customer.create(customer).then(result => {
21+
// send uploading message to client
22+
res.status(200).json({
23+
message: "Upload Successfully!",
24+
customer: result
25+
});
26+
});
27+
}catch(error){
28+
res.status(500).json({
29+
message: "Fail!",
30+
formdata: customer,
31+
error: error.message
32+
});
33+
}
34+
}
35+
36+
/**
37+
* Retrieve Customer information from database
38+
* @param {*} req
39+
* @param {*} res
40+
*/
41+
exports.retrieveInfos = (req, res) => {
42+
// find all Customer information from
43+
try{
44+
Customer.findAll({attributes: ['id', 'firstname', 'address']})
45+
.then(customerInfos => {
46+
res.status(200).json({
47+
message: "Get Customers' Infos!",
48+
customerInfos: customerInfos
49+
});
50+
})
51+
}catch(error) {
52+
// log on console
53+
console.log(error);
54+
55+
res.status(500).json({
56+
message: "Retrieving Error!",
57+
error: error
58+
});
59+
}
60+
}
61+
62+
/**
63+
*
64+
* Get a Customer by Id from database
65+
* @param {*} req
66+
* @param {*} res
67+
*/
68+
exports.findById = (req, res) => {
69+
// getting a customer id from request parameters
70+
let customerId = req.params.id;
71+
72+
try{
73+
Customer.findByPk(customerId).then(customer => {
74+
res.status(200).json({
75+
message: "Successfully! Retrieve a customer by id = " + customerId,
76+
customer: customer
77+
});
78+
});
79+
}catch(error) {
80+
// Send error to Client
81+
res.statas(500).json({
82+
message: "Error when retrieving a customer by id = " + customerId,
83+
error: error.message
84+
});
85+
}
86+
}
87+
88+
/**
89+
* Updating a Customer
90+
* @param {*} req
91+
* @param {*} res
92+
*/
93+
exports.updateById = async (req, res) => {
94+
try{
95+
let customerId = req.params.id;
96+
let customer = await Customer.findByPk(customerId);
97+
98+
if(!customer){
99+
// return a response to client
100+
res.status(404).json({
101+
message: "Not Found for updating a customer with id = " + customerId
102+
});
103+
} else {
104+
// update new change to database
105+
let updatedObject = {
106+
firstname: req.body.firstname,
107+
lastname: req.body.lastname,
108+
address: req.body.address,
109+
age: req.body.age
110+
}
111+
let result = await Customer.update(updatedObject, {returning: true, where: {id: customerId}});
112+
113+
// return the response to client
114+
if(!result) {
115+
res.status(500).json({
116+
message: "Error -> Can not update a customer with id = " + req.params.id,
117+
error: "Can NOT Updated"
118+
});
119+
}
120+
121+
res.status(200).json({
122+
message: "Update successfully a Customer with id = " + customerId,
123+
customer: updatedObject
124+
});
125+
}
126+
} catch(error){
127+
res.status(500).json({
128+
message: "Error -> Can not update a customer with id = " + req.params.id,
129+
error: error.message
130+
});
131+
}
132+
}
133+
134+
/**
135+
* Delete a Customer by ID
136+
* @param {*} req
137+
* @param {*} res
138+
*/
139+
exports.deleteById = async (req, res) => {
140+
try{
141+
let customerId = req.params.id;
142+
let customer = await Customer.findByPk(customerId);
143+
144+
if(!customer){
145+
res.status(404).json({
146+
message: "Does Not exist a Customer with id = " + customerId,
147+
error: "Not Found"
148+
});
149+
} else {
150+
await customer.destroy();
151+
res.status(200).json({
152+
message: "Delete Successfully a Customer with id = " + customerId,
153+
customer: customer
154+
});
155+
}
156+
} catch(error) {
157+
res.status(500).json({
158+
message: "Error -> Can NOT delete a customer with id = " + req.params.id,
159+
error: error.message
160+
});
161+
}
162+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = (sequelize, Sequelize) => {
2+
const Customer = sequelize.define('customer', {
3+
id: {
4+
type: Sequelize.INTEGER,
5+
autoIncrement: true,
6+
primaryKey: true
7+
},
8+
firstname: {
9+
type: Sequelize.STRING
10+
},
11+
lastname: {
12+
type: Sequelize.STRING
13+
},
14+
address: {
15+
type: Sequelize.STRING
16+
},
17+
age: {
18+
type: Sequelize.INTEGER
19+
}
20+
});
21+
22+
return Customer;
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let express = require('express');
2+
let router = express.Router();
3+
4+
const customers = require('../controllers/controller.js');
5+
6+
let path = __basedir + '/view/';
7+
8+
router.get('/', (req,res) => {
9+
console.log("__basedir" + __basedir);
10+
res.sendFile(path + "index.html");
11+
});
12+
13+
router.get('/customers/', (req,res) => {
14+
console.log("__basedir" + __basedir);
15+
res.sendFile(path + "customers.html");
16+
});
17+
18+
router.post('/api/customer/create', customers.create);
19+
router.get('/api/customer/retrieveinfos', customers.retrieveInfos);
20+
router.get('/api/customer/findone/:id', customers.findById);
21+
router.put('/api/customer/updatebyid/:id', customers.updateById);
22+
router.delete('/api/customer/deletebyid/:id', customers.deleteById);
23+
24+
module.exports = router;

0 commit comments

Comments
 (0)