Skip to content

Commit d6533c6

Browse files
authored
Merge pull request #5 from junerockwell/update-mysql-version
Update the API with MySQL Version
2 parents 61aa8e9 + 1074cf2 commit d6533c6

File tree

17 files changed

+1493
-406
lines changed

17 files changed

+1493
-406
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,4 @@ dist
104104
.tern-port
105105
.DS_Store
106106
API_MongoDB_Atlas/.env
107+
API_MySQL_AWS/.env

API_MSSQL_AZURE/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "",
55
"main": "server.js",
66
"scripts": {
7+
"watch:dev": "nodemon server.js",
8+
"prod": "pm2 start pm2.config.js",
79
"test": "echo \"Error: no test specified\" && exit 1"
810
},
911
"author": "June Rockwell",

API_MongoDB_Atlas/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "server.js",
66
"scripts": {
77
"watch:dev": "nodemon server.js",
8+
"prod": "pm2 start pm2.config.js",
89
"test": "echo \"Error: no test specified\" && exit 1"
910
},
1011
"author": "June Rockwell",

API_MongoDB_Atlas/routes/note.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = server => {
4141
}
4242
});
4343

44-
server.get('/note/author/:id', async(req, res, next) => {
44+
server.get('/notes/author/:id', async(req, res, next) => {
4545
try {
4646
const data = await Note.GetNotesByAuthorId(req.params.id);
4747
res.send(data.code, data.note);

API_MySQL_AWS/.env.sample

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ENV = 'development'
2+
API_PORT = 4000
3+
BASE_URL = 'http://localhost:4000',
4+
JWT_SECRET = 'api_jwt_secret'
5+
DB_HOST=localhost
6+
DB_USER=root
7+
DB_PASSWORD=root
8+
DB_PORT=3306
9+
DB_NAME=node_api_mysql

API_MySQL_AWS/MySQLData/MySQLData.js

Lines changed: 0 additions & 111 deletions
This file was deleted.

API_MySQL_AWS/MySQLData/Notes.js

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
const db = require('./index');
2+
const errors = require('restify-errors');
3+
4+
function CreateOneNote(data) {
5+
return new Promise((resolve, reject) => {
6+
const qry = "INSERT INTO notes SET ?";
7+
db.pool.query(qry, data, (err, result) => {
8+
if (err) {
9+
return reject({
10+
code: new errors.InternalServerError(),
11+
message: err
12+
});
13+
}
14+
15+
if (result.affectedRows <= 0) {
16+
return reject({
17+
code: new errors.InternalServerError(),
18+
message: 'Failed to create'
19+
});
20+
}
21+
22+
resolve({
23+
code: 201,
24+
message: '',
25+
note: result
26+
});
27+
});
28+
});
29+
}
30+
31+
function GetOneNoteById(id) {
32+
return new Promise((resolve, reject) => {
33+
const qry = "SELECT * FROM notes WHERE id = ?";
34+
db.pool.query(qry, id, (err, rows) => {
35+
if (err) {
36+
return reject({
37+
code: new errors.InternalServerError(),
38+
message: err
39+
});
40+
}
41+
42+
resolve({
43+
code: 200,
44+
message: '',
45+
note: rows[0] || {}
46+
});
47+
});
48+
});
49+
}
50+
51+
function GetAll() {
52+
return new Promise((resolve, reject) => {
53+
const qry = `SELECT
54+
notes.id as noteId,
55+
notes.text,
56+
author.id as authorId,
57+
author.first_name as author_firstname
58+
FROM notes
59+
RIGHT JOIN users as author
60+
ON author.id = notes.authorId
61+
WHERE notes.authorId IS NOT NULL
62+
AND notes.id IS NOT NULL`;
63+
db.pool.query(qry, {}, (err, rows) => {
64+
if (err) {
65+
return reject({
66+
code: new errors.InternalServerError(),
67+
message: err
68+
});
69+
}
70+
71+
resolve({
72+
code: 200,
73+
message: '',
74+
note: rows
75+
});
76+
});
77+
});
78+
}
79+
80+
function GetNotesByAuthorId(id) {
81+
return new Promise((resolve, reject) => {
82+
const qry = `SELECT
83+
notes.id as noteId,
84+
notes.text,
85+
author.id as authorId,
86+
author.first_name as author_firstname
87+
FROM notes
88+
RIGHT JOIN users as author
89+
ON author.id = notes.authorId
90+
WHERE notes.authorId = ?`;
91+
db.pool.query(qry, id, (err, rows) => {
92+
if (err) {
93+
return reject({
94+
code: new errors.InternalServerError(),
95+
message: err
96+
});
97+
}
98+
99+
resolve({
100+
code: 200,
101+
message: '',
102+
note: rows
103+
});
104+
});
105+
});
106+
}
107+
108+
function UpdateOneNote(id, newNote) {
109+
return new Promise((resolve, reject) => {
110+
const qry = `UPDATE notes SET text = ? WHERE id = ?`;
111+
db.pool.query(qry, [newNote.text, id], (err, rows) => {
112+
if (err) {
113+
return reject({
114+
code: new errors.InternalServerError(),
115+
message: err
116+
});
117+
}
118+
119+
if (rows.changedRows <= 0) {
120+
return reject({
121+
code: new errors.InternalServerError(),
122+
message: 'Failed to update'
123+
});
124+
}
125+
126+
resolve({
127+
code: 200,
128+
message: ''
129+
});
130+
});
131+
});
132+
}
133+
134+
function DeleteOneNoteById(id) {
135+
return new Promise((resolve, reject) => {
136+
const qry = `DELETE FROM notes WHERE id = ?`;
137+
db.pool.query(qry, id, (err, rows) => {
138+
if (err) {
139+
return reject({
140+
code: new errors.InternalServerError(),
141+
message: err
142+
});
143+
}
144+
145+
if (rows.affectedRows <= 0) {
146+
return reject({
147+
code: new errors.InternalServerError(),
148+
message: 'Failed to delete'
149+
});
150+
}
151+
152+
resolve({
153+
code: 200,
154+
message: '',
155+
});
156+
});
157+
});
158+
}
159+
160+
module.exports = {
161+
CreateOneNote,
162+
GetOneNoteById,
163+
GetAll,
164+
GetNotesByAuthorId,
165+
UpdateOneNote,
166+
DeleteOneNoteById
167+
};

0 commit comments

Comments
 (0)