-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummy.js
172 lines (153 loc) · 5.29 KB
/
dummy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const mysql = require('mysql');
// parse application/json
app.use(bodyParser.json());
//create database connection
const conn = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '123my456sql',
database: 'restful_db'
});
//connect to database
conn.connect((err) =>{
if(err) throw err;
console.log('Mysql Connected...');
});
//show all AUTHORS
app.get('/author',(req, res) => {
let sql = "SELECT * FROM AUTHORS";
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//show single AUTHOR
app.get('/author/:authorid',(req, res) => {
let sql = "SELECT * AUTHORS WHERE AUTH_ID="+req.params.id;
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//add new AUTHOR
app.post('/author',(req, res) => {
let data = {AUTH_NAME: req.body.author_name, AUTH_ID: req.params.id};
let sql = "INSERT INTO AUTHORS SET ?";
let query = conn.query(sql, data,(err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//update author
app.put('/author/:authorid',(req, res) => {
let sql = "UPDATE AUTHORS SET AUTH_NAME='"+req.body.author_name+"' WHERE product_id="+req.params.id;
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//Delete author
app.delete('/author/:authorid',(req, res) => {
let sql = "DELETE FROM AUTHORS WHERE AUTH_ID="+req.params.id+"";
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//show all books
app.get('/book',(req, res) => {
let sql = "SELECT * FROM books";
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//show single book
app.get('/book/:bookid',(req, res) => {
let sql = "SELECT * FROM books WHERE book_id="+req.params.id;
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
// show all books by an author
app.get('book?authorID=x',(req, res) => {
let sql = "SELECT * FROM books WHERE authid="+req.params.id+"";
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//add new AUTHOR
app.post('/author',(req, res) => {
let data = {AUTH_NAME: req.body.author_name, AUTH_ID: req.params.id};
let sql = "INSERT INTO AUTHORS SET ?";
let query = conn.query(sql, data,(err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//update author
app.put('/author/:authorid',(req, res) => {
let sql = "UPDATE AUTHORS SET AUTH_NAME='"+req.body.author_name+"' WHERE product_id="+req.params.id;
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//Delete author
app.delete('/author/:authorid',(req, res) => {
let sql = "DELETE FROM AUTHORS WHERE AUTH_ID="+req.params.id+"";
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//show all AUTHORS
app.get('/author',(req, res) => {
let sql = "SELECT * FROM AUTHORS";
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//show single AUTHOR
app.get('/author/:authorid',(req, res) => {
let sql = "SELECT * AUTHORS WHERE AUTH_ID="+req.params.id;
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//add new AUTHOR
app.post('/author',(req, res) => {
let data = {AUTH_NAME: req.body.author_name, AUTH_ID: req.params.id};
let sql = "INSERT INTO AUTHORS SET ?";
let query = conn.query(sql, data,(err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//update author
app.put('/author/:authorid',(req, res) => {
let sql = "UPDATE AUTHORS SET AUTH_NAME='"+req.body.author_name+"' WHERE product_id="+req.params.id;
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//Delete author
app.delete('/author/:authorid',(req, res) => {
let sql = "DELETE FROM AUTHORS WHERE AUTH_ID="+req.params.id+"";
let query = conn.query(sql, (err, results) => {
if(err) throw err;
res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
});
});
//Server listening
app.listen(3000,() =>{
console.log('Server started on port 3000...');
});