Skip to content

Instantly share code, notes, and snippets.

@kosinix
Created June 22, 2019 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosinix/4e046dec150bdbb394e7581543bf4c08 to your computer and use it in GitHub Desktop.
Save kosinix/4e046dec150bdbb394e7581543bf4c08 to your computer and use it in GitHub Desktop.
Kinabukasan ni Daggy
// 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