-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yaroslav
committed
Feb 4, 2018
1 parent
ebaddff
commit 847b5ae
Showing
4 changed files
with
54 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
const express = require('express'); | ||
const MongoClient = require('mongodb').MongoClient; | ||
const mysql = require("mysql"); | ||
const bodyParser = require('body-parser'); | ||
const db = require('./config/db'); | ||
const app = express(); | ||
const port = 8000; | ||
|
||
app.use(bodyParser.urlencoded({ extended: true })); | ||
MongoClient.connect(db.url, (err, database) => { | ||
|
||
const con = mysql.createConnection(db); | ||
|
||
con.connect(err => { | ||
if (err) { | ||
return console.log(err); | ||
console.log(err); | ||
} | ||
require('./server')(app, database); | ||
require('./server')(app, con); | ||
app.listen(port, () => console.log('We are live on ' + port)); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,60 @@ | ||
var ObjectID = require('mongodb').ObjectID; | ||
|
||
module.exports = function(app, database) { | ||
const DB = database.db('test_database'); | ||
|
||
app.get('/notes', (req, res) => { | ||
DB.collection('notes').find().toArray((err, notes) => { | ||
const sql = "SELECT * FROM notes ORDER BY id"; | ||
|
||
database.query(sql, (err, result) => { | ||
if (err) { | ||
res.send({'error':'An error has occurred'}); | ||
} else { | ||
res.send(notes); | ||
res.send({ 'error': 'An error has occurred' }); | ||
} | ||
res.send(result); | ||
}); | ||
}); | ||
|
||
app.get('/notes/:id', (req, res) => { | ||
const ID = req.params.id; | ||
const DETAILS = { '_id': new ObjectID(ID) }; | ||
const sql = "SELECT * FROM notes WHERE id = ?"; | ||
|
||
DB.collection('notes').findOne(DETAILS, (err, item) => { | ||
database.query(sql, [ID], (err, result) => { | ||
if (err) { | ||
res.send({'error':'An error has occurred'}); | ||
} else { | ||
res.send(item); | ||
res.send({ 'error': 'An error has occurred' }); | ||
} | ||
res.send(result); | ||
}); | ||
}); | ||
|
||
app.put('/notes/:id', (req, res) => { | ||
const ID = req.params.id; | ||
const NOTE = { $set: { text: req.body.body, title: req.body.title } }; | ||
const DETAILS = { '_id': new ObjectID(ID) }; | ||
const sql = `UPDATE notes SET note = ?, date = NOW() WHERE id = ?`; | ||
|
||
DB.collection('notes').updateOne(DETAILS, NOTE, err => { | ||
database.query(sql, [req.body.note, ID], (err, result) => { | ||
if (err) { | ||
res.send({'error':'An error has occurred'}); | ||
} else { | ||
res.send(NOTE); | ||
res.send({ 'error': 'An error has occurred' }); | ||
} | ||
res.send(result); | ||
}); | ||
}); | ||
|
||
app.delete('/notes/:id', (req, res) => { | ||
const ID = req.params.id; | ||
const DETAILS = { '_id': new ObjectID(ID) }; | ||
const sql = "DELETE FROM notes WHERE id = ?"; | ||
|
||
DB.collection('notes').removeOne(DETAILS, err => { | ||
database.query(sql, [ID], (err, result) => { | ||
if (err) { | ||
res.send({'error': 'An error has occurred'}); | ||
} else { | ||
res.send(`Note ${ID} deleted!`); | ||
res.send({ 'error': 'An error has occurred' }); | ||
} | ||
res.send(`Notes deleted: ${result.affectedRows}`); | ||
}); | ||
}); | ||
|
||
app.post('/notes', (req, res) => { | ||
const NOTE = { text: req.body.body, title: req.body.title }; | ||
const sql = "INSERT INTO notes (note, date) VALUES (?, NOW())"; | ||
|
||
DB.collection('notes').insertOne(NOTE, (err, result) => { | ||
database.query(sql, [req.body.note], (err, result) => { | ||
if (err) { | ||
res.send({ 'error': 'An error has occurred' }); | ||
} else { | ||
res.send(result.ops[0]); | ||
} | ||
res.send(`1 record inserted, ID: ${result.insertId}`); | ||
}); | ||
}); | ||
}; |