-
-
Save kosinix/4e046dec150bdbb394e7581543bf4c08 to your computer and use it in GitHub Desktop.
Kinabukasan ni Daggy
This file contains hidden or 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
// Modules | |
const express = require('express') | |
const mysql = require('mysql') | |
const bodyParser = require('body-parser') | |
// Settings | |
const port = 3000 | |
// Express | |
const app = express() | |
// Middlewares | |
app.use(bodyParser.json()) // to support JSON-encoded bodies | |
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies | |
extended: true | |
})) | |
// Database | |
const db = mysql.createConnection({ | |
host: 'localhost', | |
user: 'root', | |
password: '', | |
database: 'inventory', | |
multipleStatements: true | |
}); | |
db.connect((err) => { | |
if (err) | |
throw (err) | |
else | |
console.log('Connection Success'); | |
}); | |
app.get('/inventory', (req, res) => { | |
let dataQuery = "SELECT * FROM `items`"; | |
db.query(dataQuery, (err, results) => { | |
if (err) throw (err); | |
res.send(results) | |
}) | |
}); | |
app.get('/inventory/:id', (req, res) => { | |
db.query("SELECT * FROM `items` WHERE id = ?", [req.params.id], (err, results) => { | |
if (err) throw (err); | |
res.send(results) | |
}) | |
}); | |
app.delete('/inventory/:id', (req, res) => { | |
db.query('DELETE FROM `items` WHERE `id` = ?', [req.params.id], (err, results) => { | |
if (err) throw (err); | |
res.send(`Deleted ${results.affectedRows} rows.`) | |
}) | |
}) | |
app.post('/inventory', (req, res) => { | |
let post = req.body; | |
db.query('CALL addOrEditItem(?,?,?,?);', [post.id, post.name, post.qty, post.amount], (err, results) => { | |
if (err) throw (err); | |
if (post.id > 0) { | |
res.send('Item updated.') | |
} else { | |
res.send('Created item.') | |
} | |
}) | |
}) | |
app.listen(port, () => { | |
console.log('Listening to port ' + port) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment