Skip to content
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

Jake #92

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Jake #92

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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.env
79 changes: 79 additions & 0 deletions controllers/drugs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const mysql = require('mysql')
const pool = require('../sql/connection')
const { handleSQLError } = require('../sql/error')


const getAllDrugs = (req, res) => {

pool.query("SELECT * FROM DRUGS", (err, rows) => {
if (err) return handleSQLError(res, err)
return res.json(rows);
})
}

const getDrugsByID = (req, res) => {
// SELECT USERS WHERE ID = <REQ PARAMS ID>
// ************************
let id = req.params.id;//specially get id from the request parameter

let sql = "SELECT * FROM DRUGS WHERE ID = ?" // the "?" is what the user type in
// WHAT GOES IN THE BRACKETS
sql = mysql.format(sql, [id])

pool.query(sql, (err, rows) => {
if (err) return handleSQLError(res, err)
return res.json(rows);
})
}

const createDrug = (req, res) => {
let body = req.body
console.log(body)

let sql = "INSERT INTO DRUGS (GENERIC, BRAND, FDA_CODE) VALUES (?,?,?);"
// WHAT GOES IN THE BRACKETS
sql = mysql.format(sql, [body.GENERIC,body.BRAND,body.FDA_CODE])

pool.query(sql, (err, results) => {
if (err) return handleSQLError(res, err)
return res.json({ newId: results.insertId });
})
}

const updateDrugByID = (req, res) => {
// UPDATE USERS AND SET FIRST AND LAST NAME WHERE ID = <REQ PARAMS ID>
let body = req.body
let id = req.params.id;//specially get id from the request parameter
// *****************************************
let sql = "UPDATE DRUGS SET GENERIC = ?, BRAND = ?, FDA_CODE =? WHERE id =?"
// WHAT GOES IN THE BRACKETS
sql = mysql.format(sql, [body.GENERIC,body.BRAND,body.FDA_CODE, id]) //id = condition

pool.query(sql, (err, results) => {
if (err) return handleSQLError(res, err)
return res.status(204).json();
})
}

const deleteDrugByID = (req, res) => {

let id = req.params.id;//specially get id from the request parameter

let sql = "DELETE FROM DRUGS WHERE id = ?"
// WHAT GOES IN THE BRACKETS
sql = mysql.format(sql, [id])

pool.query(sql, (err, results) => {
if (err) return handleSQLError(res, err)
console.log(results)

return res.json({ message: `Deleted ${results.affectedRows} user(s)` });
})
}
module.exports = {
getAllDrugs,
getDrugsByID,
createDrug,
updateDrugByID,
deleteDrugByID
}
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const express = require("express")
// require("dotenv").config()
const app =express()
const port= process.env.PORT || 4002
const drugsRoute = require("./routes/drugroutes")

app.use(express.json());

app.use(drugsRoute);

app.get('/', (req, res) => {
res.send('Welcome to our server!')
})




app.listen(port, () => {
console.log(`Web server is listening on port ${port}!`);
});
Loading