Skip to content

Commit aee3b7d

Browse files
committedOct 18, 2018
response changed and all semi-comma removed
1 parent c147e18 commit aee3b7d

File tree

2 files changed

+99
-50
lines changed

2 files changed

+99
-50
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ typings/
5959

6060
# next.js build output
6161
.next
62+
63+
# serverless deploy output
64+
.serverless

‎index.js

+96-50
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,126 @@
1-
const express = require('express');
2-
const serverless = require('serverless-http');
3-
const bodyParser = require('body-parser');
4-
const pool = require('./configs/dbConfig');
1+
const express = require('express')
2+
const serverless = require('serverless-http')
3+
const bodyParser = require('body-parser')
4+
const pool = require('./configs/dbConfig')
55

6-
const app = express();
6+
const app = express()
77

8-
app.use(bodyParser.json());
9-
app.use(bodyParser.urlencoded({ extended: false }));
8+
app.use(bodyParser.json())
9+
app.use(bodyParser.urlencoded({ extended: true }))
1010

11-
app.get(['/', '/pokemon/'], (req, res) => {
12-
pool.query('SELECT * FROM pokemon_tb', (err, results, fields) => {
13-
if (err){
14-
res.send({ data: null, status: 400, error: err.message });
11+
app.get('/pokemon/', (req, res) => {
12+
const query = 'SELECT * FROM pokemon_tb'
13+
pool.query(query, (err, results, fields) => {
14+
if (err) {
15+
const response = { data: null, message: err.message, }
16+
res.send(response)
1517
}
1618

17-
const pokemons = [...results];
18-
res.send({ data: pokemons });
19-
});
20-
});
19+
const pokemons = [...results]
20+
const response = {
21+
data: pokemons,
22+
message: 'All pokemons successfully retrieved.',
23+
}
24+
res.send(response)
25+
})
26+
})
2127

2228
app.get('/pokemon/:id', (req, res) => {
23-
const id = req.params.id;
24-
pool.query(`SELECT * FROM pokemon_tb WHERE id=${id}`, (err, results, fields) => {
29+
const id = req.params.id
30+
const query = `SELECT * FROM pokemon_tb WHERE id=${id}`
31+
pool.query(query, (err, results, fields) => {
2532
if (err) {
26-
res.send({ data: null, status: 400, error: err.message });
33+
const response = { data: null, message: err.message, }
34+
res.send(response)
2735
}
2836

29-
const pokemon = results[0];
30-
res.send({data: pokemon});
31-
});
32-
});
37+
const pokemon = results[0]
38+
const response = {
39+
data: pokemon,
40+
message: `Pokemon ${pokemon.name} successfully retrieved.`,
41+
}
42+
res.status(200).send(response)
43+
})
44+
})
3345

46+
// Handle pokemon POST route
3447
app.post('/pokemon/', (req, res) => {
35-
const { name, height, weight, avatar } = req.body;
36-
const query = `INSERT INTO pokemon_tb (name, height, weight, avatar) VALUES ('${name}', '${height}', '${weight}', '${avatar}')`;
48+
const { name, height, weight, avatar } = req.body
49+
50+
const query = `INSERT INTO pokemon_tb (name, height, weight, avatar) VALUES ('${name}', '${height}', '${weight}', '${avatar}')`
3751
pool.query(query, (err, results, fields) => {
3852
if (err) {
39-
res.send({ data: null, status: 400, error: err.message });
53+
const response = { data: null, message: err.message, }
54+
res.send(response)
55+
}
56+
57+
const { insertId } = results
58+
const pokemon = { id: insertId, name, height, weight, avatar }
59+
const response = {
60+
data: pokemon,
61+
message: `Pokemon ${name} successfully added.`,
4062
}
41-
const { insertId } = results;
42-
const insertedPokemon = { id: insertId, name, height, weight, avatar };
43-
res.send({ data: insertedPokemon });
44-
});
45-
});
63+
res.status(201).send(response)
64+
})
65+
})
4666

67+
// Handle pokemon PUT route
4768
app.put('/pokemon/:id', (req, res) => {
48-
const id = req.params.id;
49-
pool.query(`SELECT * FROM pokemon_tb WHERE id=${id} LIMIT 1`, (err, results, fields) => {
69+
const { id } = req.params
70+
const query = `SELECT * FROM pokemon_tb WHERE id=${id} LIMIT 1`
71+
pool.query(query, (err, results, fields) => {
5072
if (err) {
51-
res.send({ data: null, status: 400, error: err.message });
73+
const response = { data: null, message: err.message, }
74+
res.send(response)
5275
}
5376

54-
const { id, name, height, weight, avatar } = {...results[0], ...req.body};
55-
const query = `UPDATE pokemon_tb SET name='${name}', height='${height}', weight='${weight}', avatar='${avatar}' WHERE id='${id}'`;
77+
const { id, name, height, weight, avatar } = { ...results[0], ...req.body }
78+
const query = `UPDATE pokemon_tb SET name='${name}', height='${height}', weight='${weight}', avatar='${avatar}' WHERE id='${id}'`
5679
pool.query(query, (err, results, fields) => {
5780
if (err) {
58-
res.send({ data: null, status: 400, error: err.message });
81+
const response = { data: null, message: err.message, }
82+
res.send(response)
5983
}
6084

61-
res.send({ data: { id, name, height, weight, avatar } });
62-
});
63-
});
64-
});
85+
const pokemon = {
86+
id,
87+
name,
88+
height,
89+
weight,
90+
avatar,
91+
}
92+
const response = {
93+
data: pokemon,
94+
message: `Pokemon ${name} is successfully updated.`,
95+
}
96+
res.send(response)
97+
})
98+
})
99+
})
65100

101+
// Handler pokemon DELETE route
66102
app.delete('/pokemon/:id', (req, res) => {
67-
const id = req.params.id;
68-
pool.query(`DELETE FROM pokemon_tb WHERE id='${id}'`, (err, results, fields) => {
103+
const { id } = req.params
104+
const query = `DELETE FROM pokemon_tb WHERE id=${id}`
105+
pool.query(query, (err, results, fields) => {
69106
if (err) {
70-
res.send({ data: null, status: 400, error: err.message });
107+
const response = { data: null, message: err.message }
108+
res.send(response)
109+
}
110+
111+
const response = {
112+
data: null,
113+
message: `Pokemon with id: ${id} successfully deleted.`,
71114
}
72-
res.send({ data: null, message: 'Pokemon successfully deleted'});
73-
});
74-
});
115+
res.send(response)
116+
})
117+
})
75118

76-
app.all('*', function(req, res){
77-
res.send({ data: null, message: '404 - Route not found!!' });
78-
});
119+
// Handle in-valid route path
120+
app.all('*', function(req, res) {
121+
const response = { data: null, message: 'Route not found!!' }
122+
res.status(400).send(response)
123+
})
79124

80-
module.exports.handler = serverless(app);
125+
// wrap express app instance with serverless http function
126+
module.exports.handler = serverless(app)

0 commit comments

Comments
 (0)
Please sign in to comment.