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

completed route, get employee by id #103

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions controller/employees.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const mysql = require('mysql')
const pool = require('../mysql/connection') //update this folder location when its made



const getEmployees = (req, res) => {
pool.query("SELECT * FROM employees LIMIT 50", (err, rows) => {
// if (err) return handleSQLError(res, err)
return res.json(rows);
})
}

const getEmployeeById = (req, res) => {
let sql = "SELECT * FROM employees WHERE emp_no = ?"
let id = req.params.id
sql = mysql.format(sql, [id])

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

const getEmployeeById2 = (req, res) => {
res.send("getting employees")
}





module.exports = { getEmployees, getEmployeeById, getEmployeeById2 }
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const express = require("express");
const bodyParser = require("body-parser");

const pool = require('./mysql/connection')

const employeesRouter = require('./routes/employees')


const app = express();
const port = process.env.PORT || 4001;

app.use(bodyParser.json())
app.use('/employees', employeesRouter)


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

app.listen(port, () => {
console.log(`Web server is listening on port ${port}!`);
});
31 changes: 31 additions & 0 deletions mysql/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const mysql = require('mysql')
require('dotenv').config()

const host = process.env.HOST
const username = process.env.USERNAME
const password = process.env.PASSWORD
const database = process.env.DATABASE

class Connection {
constructor() {
if (!this.pool) {
console.log('creating connection...')
console.log(host, username, password, database)
this.pool = mysql.createPool({
connectionLimit: 100,
host: host,
user: username,
password: password,
database: database
})
console.log("success")
return this.pool
}

return this.pool
}
}

const instance = new Connection()

module.exports = instance;
Loading