Skip to content

Update the API with MySQL Version #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ dist
.tern-port
.DS_Store
API_MongoDB_Atlas/.env
API_MySQL_AWS/.env
2 changes: 2 additions & 0 deletions API_MSSQL_AZURE/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"description": "",
"main": "server.js",
"scripts": {
"watch:dev": "nodemon server.js",
"prod": "pm2 start pm2.config.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "June Rockwell",
Expand Down
1 change: 1 addition & 0 deletions API_MongoDB_Atlas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "server.js",
"scripts": {
"watch:dev": "nodemon server.js",
"prod": "pm2 start pm2.config.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "June Rockwell",
Expand Down
2 changes: 1 addition & 1 deletion API_MongoDB_Atlas/routes/note.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = server => {
}
});

server.get('/note/author/:id', async(req, res, next) => {
server.get('/notes/author/:id', async(req, res, next) => {
try {
const data = await Note.GetNotesByAuthorId(req.params.id);
res.send(data.code, data.note);
Expand Down
9 changes: 9 additions & 0 deletions API_MySQL_AWS/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ENV = 'development'
API_PORT = 4000
BASE_URL = 'http://localhost:4000',
JWT_SECRET = 'api_jwt_secret'
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=root
DB_PORT=3306
DB_NAME=node_api_mysql
111 changes: 0 additions & 111 deletions API_MySQL_AWS/MySQLData/MySQLData.js

This file was deleted.

167 changes: 167 additions & 0 deletions API_MySQL_AWS/MySQLData/Notes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
const db = require('./index');
const errors = require('restify-errors');

function CreateOneNote(data) {
return new Promise((resolve, reject) => {
const qry = "INSERT INTO notes SET ?";
db.pool.query(qry, data, (err, result) => {
if (err) {
return reject({
code: new errors.InternalServerError(),
message: err
});
}

if (result.affectedRows <= 0) {
return reject({
code: new errors.InternalServerError(),
message: 'Failed to create'
});
}

resolve({
code: 201,
message: '',
note: result
});
});
});
}

function GetOneNoteById(id) {
return new Promise((resolve, reject) => {
const qry = "SELECT * FROM notes WHERE id = ?";
db.pool.query(qry, id, (err, rows) => {
if (err) {
return reject({
code: new errors.InternalServerError(),
message: err
});
}

resolve({
code: 200,
message: '',
note: rows[0] || {}
});
});
});
}

function GetAll() {
return new Promise((resolve, reject) => {
const qry = `SELECT
notes.id as noteId,
notes.text,
author.id as authorId,
author.first_name as author_firstname
FROM notes
RIGHT JOIN users as author
ON author.id = notes.authorId
WHERE notes.authorId IS NOT NULL
AND notes.id IS NOT NULL`;
db.pool.query(qry, {}, (err, rows) => {
if (err) {
return reject({
code: new errors.InternalServerError(),
message: err
});
}

resolve({
code: 200,
message: '',
note: rows
});
});
});
}

function GetNotesByAuthorId(id) {
return new Promise((resolve, reject) => {
const qry = `SELECT
notes.id as noteId,
notes.text,
author.id as authorId,
author.first_name as author_firstname
FROM notes
RIGHT JOIN users as author
ON author.id = notes.authorId
WHERE notes.authorId = ?`;
db.pool.query(qry, id, (err, rows) => {
if (err) {
return reject({
code: new errors.InternalServerError(),
message: err
});
}

resolve({
code: 200,
message: '',
note: rows
});
});
});
}

function UpdateOneNote(id, newNote) {
return new Promise((resolve, reject) => {
const qry = `UPDATE notes SET text = ? WHERE id = ?`;
db.pool.query(qry, [newNote.text, id], (err, rows) => {
if (err) {
return reject({
code: new errors.InternalServerError(),
message: err
});
}

if (rows.changedRows <= 0) {
return reject({
code: new errors.InternalServerError(),
message: 'Failed to update'
});
}

resolve({
code: 200,
message: ''
});
});
});
}

function DeleteOneNoteById(id) {
return new Promise((resolve, reject) => {
const qry = `DELETE FROM notes WHERE id = ?`;
db.pool.query(qry, id, (err, rows) => {
if (err) {
return reject({
code: new errors.InternalServerError(),
message: err
});
}

if (rows.affectedRows <= 0) {
return reject({
code: new errors.InternalServerError(),
message: 'Failed to delete'
});
}

resolve({
code: 200,
message: '',
});
});
});
}

module.exports = {
CreateOneNote,
GetOneNoteById,
GetAll,
GetNotesByAuthorId,
UpdateOneNote,
DeleteOneNoteById
};
Loading