Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
39963c7
-
RamonCruz-Hinojosa Jul 28, 2021
02a49bb
changed readme
Michael-Fares Jul 28, 2021
730b200
added SQL creds
kc-hrt Jul 28, 2021
8adf224
merge error
kc-hrt Jul 28, 2021
12684f6
added express
Michael-Fares Jul 28, 2021
2a162b0
created routes and controllers folders with employees.js file for each
mayra-tellez Jul 28, 2021
92335e4
project inited
RamonCruz-Hinojosa Jul 28, 2021
028a483
added export statement to controller file
mayra-tellez Jul 28, 2021
a6f6765
Merge pull request #2 from kiffykowboy/mayra/create-employees-router
TeknoFiend Jul 29, 2021
b7ae0b4
adding controller and route for employee id
chase-powell Jul 30, 2021
ab216ea
Merge branch 'master' into Michael-Fares
Michael-Fares Jul 30, 2021
1763d9f
Merge pull request #1 from kiffykowboy/Michael-Fares
TeknoFiend Jul 30, 2021
397a7e6
GET routs for employees
sammydunn Jul 30, 2021
c2e2f86
Merge branch 'master' into initProject
TeknoFiend Jul 30, 2021
7e77346
Controller function getEmployees
sammydunn Jul 30, 2021
99069d6
Merge pull request #3 from kiffykowboy/initProject
TeknoFiend Jul 30, 2021
683e3f5
indexjs fixed
RamonCruz-Hinojosa Jul 30, 2021
a915dca
Merge pull request #5 from kiffykowboy/newbranch
TeknoFiend Jul 30, 2021
de0fa63
Merge branch 'master' of https://github.com/kiffykowboy/311_wk5_both_…
sammydunn Jul 30, 2021
aa34a07
Merge pull request #4 from kiffykowboy/chase-part
TeknoFiend Jul 30, 2021
43b8743
fixed controller and route for employee by id
chase-powell Jul 30, 2021
262c28c
added controllers
slisette127 Jul 30, 2021
f40d8d5
routes for employee by first name
slisette127 Jul 30, 2021
af23db7
pull request credit
kc-hrt Jul 30, 2021
cf3711c
fixed caps
RamonCruz-Hinojosa Jul 30, 2021
e0bb06c
router caps fixed
RamonCruz-Hinojosa Jul 30, 2021
3759204
Merge branch 'master' into Sam
TeknoFiend Jul 30, 2021
14e1442
Merge pull request #6 from kiffykowboy/Sam
TeknoFiend Jul 30, 2021
2f37444
Merge pull request #7 from kiffykowboy/kasey_branch
TeknoFiend Jul 30, 2021
a69d40f
Merge branch 'master' into sandra
TeknoFiend Jul 30, 2021
f26eccb
Merge pull request #8 from kiffykowboy/sandra
TeknoFiend Jul 30, 2021
a7e085a
adding update employee by id
JankMajesty Jul 30, 2021
8bfdfb6
Merge pull request #9 from kiffykowboy/lucas
TeknoFiend Jul 30, 2021
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
123 changes: 66 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Read this ENTIRE document before beginning work.

## Collaboration

ONE person from the group will fork this repo from ACA. That person will give everyone else permissions as a "collaborator". From that point on, you will all clone THE ONE repo and your changes will be in the form of __pull requests__. For each change/ticket you will create a new branch and work from there. Remember to always pull the latest from the master branch before you begin on a new task.
ONE person from the group will fork this repo from ACA. That person will give everyone else permissions as a "collaborator". From that point on, you will all clone THE ONE repo and your changes will be in the form of **pull requests**. For each change/ticket you will create a new branch and work from there. Remember to always pull the latest from the master branch before you begin on a new task.

_Adding a collaborator_
<br />
Expand All @@ -15,14 +15,15 @@ _Creating a new branch_
`git checkout -b <BRANCH_NAME>`

_Pulling from master_
* Make sure you are on the master branch (`git checkout master`)
* Pull the latest changes (`git pull origin master` or `git pull` for short)

- Make sure you are on the master branch (`git checkout master`)
- Pull the latest changes (`git pull origin master` or `git pull` for short)

While ONE person is working on this, ANOTHER person can begin work on [database setup](#database-setup)

## Instructions

You will work as a group to create APIs from the very beginning. Notice that there is currently no code. Not a single file. Follow the steps below to make this happen. Please keep in mind that not all tasks are parallizable.. that means that some things will need to happen before others and there are points where the rest of the group may need to wait for one person to complete a task in order to move on. That's fine. Give that person support and collaborate together to unblock each other.
You will work as a group to create APIs from the very beginning. Notice that there is currently no code. Not a single file. Follow the steps below to make this happen. Please keep in mind that not all tasks are parallizable.. that means that some things will need to happen before others and there are points where the rest of the group may need to wait for one person to complete a task in order to move on. That's fine. Give that person support and collaborate together to unblock each other.

_Note: Many tasks are intentionally vague. It's up to you to learn/google some of these_

Expand All @@ -34,7 +35,7 @@ _Note: Many tasks are intentionally vague. It's up to you to learn/google some o

2. Choose download file

3. Next page click "No thanks, just start my download" at the bottom
3. Next page click "No thanks, just start my download" at the bottom

4. Once downloaded, install the application

Expand All @@ -47,10 +48,11 @@ _Note: Many tasks are intentionally vague. It's up to you to learn/google some o
We will use ONE person's Google Cloud database for this assignment. The steps for connecting to the database will follow.

1. Use the following command to connect to your Google Cloud database
* `mysql -u root -h <HOST IP FROM WORKBENCH> -p`
* You will be prompted for a password
* This is very similar to connecting through MySQL Workbench
* [Additional information](https://stackoverflow.com/questions/15872543/access-mysql-remote-database-from-command-line)

- `mysql -u root -h <HOST IP FROM WORKBENCH> -p`
- You will be prompted for a password
- This is very similar to connecting through MySQL Workbench
- [Additional information](https://stackoverflow.com/questions/15872543/access-mysql-remote-database-from-command-line)

2. Type `quit` when you want to exit the process

Expand All @@ -59,12 +61,14 @@ We will use ONE person's Google Cloud database for this assignment. The steps fo
We are going to use sample data given to use by MySQL. An overview of the process exists here: https://dev.mysql.com/doc/employee/en/

1. Git clone the following repo in a new directory (NOT THIS ONE)
* https://github.com/datacharmer/test_db.git

- https://github.com/datacharmer/test_db.git

2. cd into that directory

3. Run the connect command followed by `< employees.sql` to load that database
* `mysql -u root -h <HOST IP FROM WORKBENCH> -p < employees.sql`

- `mysql -u root -h <HOST IP FROM WORKBENCH> -p < employees.sql`

4. After the operation is complete (may take a couple mins) you should have automatically been exited from the `mysql` command

Expand All @@ -76,7 +80,7 @@ We are going to use sample data given to use by MySQL. An overview of the proces

8. Open a new query and run `select * from employees;`

9. You should have retrieved 1000 employee rows
9. You should have retrieved 1000 employee rows

10. You should see 6 tables under this database. There are over 2 million records among those two tables

Expand All @@ -90,106 +94,111 @@ Let's get started...

#### 1. Initialize your project

* In this folder run `npm init`. Accept all the defaults (press enter a bunch of times)

* You should now have a `package.json` file
- In this folder run `npm init`. Accept all the defaults (press enter a bunch of times)

* Npm install the following packages: `express body-parser nodemon mysql`
- You should now have a `package.json` file

* You should now have a `package-lock.json` file and a `node_modules` folder
- Npm install the following packages: `express body-parser nodemon mysql`

* Create an `index.js` file and type `console.log('testing')` on the first line
- You should now have a `package-lock.json` file and a `node_modules` folder

* Open the package.json file and add a new `start` script. The value should be `nodemon ./index.js`
- Create an `index.js` file and type `console.log('testing')` on the first line

* Type `npm start` in the terminal. Did you see the log?
- Open the package.json file and add a new `start` script. The value should be `nodemon ./index.js`

* Change the word "testing" to "re-testing". Did it re-load with the new log?
- Type `npm start` in the terminal. Did you see the log?

* Setup is complete
- Change the word "testing" to "re-testing". Did it re-load with the new log?

* Stop and commit/push work. Everyone else pull the updated code
- Setup is complete

- Stop and commit/push work. Everyone else pull the updated code

#### 2. Basic Express

* Remove the "console.log" from the index.js file
- Remove the "console.log" from the index.js file

* On the first line, import express: `const express = require('express')`
- On the first line, import express: `const express = require('express')`

* Initialize the app on the second line: `const app = express()`
- Initialize the app on the second line: `const app = express()`

* Have the app listen on port 4001
- Have the app listen on port 4001

* Re-run the `npm start` command if necessary.. is the app running?
- Re-run the `npm start` command if necessary.. is the app running?

* Navigate to `http://localhost:4001` in the browser to check if the app is running
- Navigate to `http://localhost:4001` in the browser to check if the app is running

* Before the "app.listen", add a default GET route and `res.send` the text: "Welcome to our API"

* Congratulations, we have a server running
- Before the "app.listen", add a default GET route and `res.send` the text: "Welcome to our API"

- Congratulations, we have a server running

#### 3. Express routes - BEGINNING

* Create a new folder called `routes`
- Create a new folder called `routes`

* In the routes folder, create a new file called `employees.js`
- In the routes folder, create a new file called `employees.js`

* Create a router and make GET routes for `/, /:id, firstname/:first_name`
- Create a router and make GET routes for `/, /:id, firstname/:first_name`

* For right now, use `res.send` to send back the text "getting employees..." for each route. We will update this later
- For right now, use `res.send` to send back the text "getting employees..." for each route. We will update this later

* Export the router and import it into the `index.js` file. Use it with the prefix of "employees" so that each route above starts with "employees"
- Export the router and import it into the `index.js` file. Use it with the prefix of "employees" so that each route above starts with "employees"

#### 4. Express controllers - BEGINNING

* Create a new folder called `controllers`
- Create a new folder called `controllers`

* In the controllers folder, create a new file called `employees.js`
- In the controllers folder, create a new file called `employees.js`

* Create the following functions `getEmployees, getEmployeesById, getEmployeesByFirstName`
- Create the following functions `getEmployees, getEmployeesById, getEmployeesByFirstName`

* Move the logic (everything after the route path) from the employees router into these functions
- Move the logic (everything after the route path) from the employees router into these functions

* Export these functions: `module.exports = { getEmployees, getEmployeesById, getEmployeesByFirstName }`
- Export these functions: `module.exports = { getEmployees, getEmployeesById, getEmployeesByFirstName }`

* Import these functions back into the employees router and use them `router.get('/:id', controller.getEmployeesById)`
- Import these functions back into the employees router and use them `router.get('/:id', controller.getEmployeesById)`

* Everything should have stayed the same. If you navigate to `http://localhost:4001/employees/5` you should see the text "getting employees..."
- Everything should have stayed the same. If you navigate to `http://localhost:4001/employees/5` you should see the text "getting employees..."

#### 5. Hooking up MySQL

* Create a new folder called `mysql`
- Create a new folder called `mysql`

- In the mysql folder, create a new file called `connection.js`

- Import `mysql` at the top of the file

- Create a function (_singleton_) that creates a connection pool (if it doesn't already exist) and returns it. Export this function

- Remember to use the appropriate credentials from the ONE database we are currently sharing and make sure the "database" field in the connection pool is set to "employees"

* In the mysql folder, create a new file called `connection.js`
- Import the previous function into whichever file needs it. This will usually be the controller. You will import this connection as `pool` and use `pool.query` to query the database. Remember that the first argument to this function is a SELECT statement and the second argument is a callback with `(err, results)` parameters

* Import `mysql` at the top of the file
- If at any point (in any file) we find an `err` when using `pool.query`, return a 500 status code and the text, "something went wrong"

* Create a function (_singleton_) that creates a connection pool (if it doesn't already exist) and returns it. Export this function
* Remember to use the appropriate credentials from the ONE database we are currently sharing and make sure the "database" field in the connection pool is set to "employees"
---

* Import the previous function into whichever file needs it. This will usually be the controller. You will import this connection as `pool` and use `pool.query` to query the database. Remember that the first argument to this function is a SELECT statement and the second argument is a callback with `(err, results)` parameters
`Kasey: created db, mysql folder and connection.js, added credentials in connection.js`

* If at any point (in any file) we find an `err` when using `pool.query`, return a 500 status code and the text, "something went wrong"
---

#### 6. Employees controller - CONTINUED

* Import the `pool` function from `mysql/connection` at the top of the file (controllers/employees.js). Additionally import the general `mysql` package
- Import the `pool` function from `mysql/connection` at the top of the file (controllers/employees.js). Additionally import the general `mysql` package

* Update the `getEmployees` function so that it calls the database, __selecting all fields from the employees table but limiting the results to 50__. Use `res.json` to return the results to the user
- Update the `getEmployees` function so that it calls the database, **selecting all fields from the employees table but limiting the results to 50**. Use `res.json` to return the results to the user

* Update the `getEmployeesById` function so that it calls the database, __selecting all fields from the employees table where the emp_no matches the id query parameter__. This should return one result. Use `res.json` to return the result to the user
- Update the `getEmployeesById` function so that it calls the database, **selecting all fields from the employees table where the emp_no matches the id query parameter**. This should return one result. Use `res.json` to return the result to the user

* Update the `getEmployeesByFirstName` function so that it calls the database, __selecting all fields from the employees table where the first_name matches the first_name query parameter__. This could return multiple results. Use `res.json` to return the results to the user
- Update the `getEmployeesByFirstName` function so that it calls the database, **selecting all fields from the employees table where the first_name matches the first_name query parameter**. This could return multiple results. Use `res.json` to return the results to the user

#### Salaries and Departments

* Repeat steps 3-6 to create three more routes tying employee information to their current salaries or departments. New routes, controllers and queries will need to be created. You are free to collaboratively name these routes/controller functions anything you wish.
- Repeat steps 3-6 to create three more routes tying employee information to their current salaries or departments. New routes, controllers and queries will need to be created. You are free to collaboratively name these routes/controller functions anything you wish.

#### QA

All routes should be returning data from the database visibile through either the browser or Postman.
All routes should be returning data from the database visibile through either the browser or Postman.

## Summary

Expand Down
45 changes: 45 additions & 0 deletions controllers/employees.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const getEmployeesByFirstName = () => {

}

//Controller Function getEmployees

let getEmployees = function(req, res){;
console.log("Inside GET employee by id")
let id = req.params.id
let sql = "SELECT * FROM employees WHERE emp_no = ?"
connection.query(sql, [id], (error, rows) => {
console.log("ROWS:", rows)
if(error){
console.error("failed", error);
res.sendStatus(500);
}else if (!rows || rows.length == 0){
res.sendStatus(404);
}else {
res.send(rows[0]);
}
})

};



const getEmployeesById = res.json(
employees.find((employees) => employees.id == req.params.id)
)
let sql = "SELECT * FROM employees WHERE emp_no = ?";

const replacements = [req.params.id];

sql = mysql.format(sql, replacements);

pool.query(sql, (err, results) =>{
if (err){
return res.status(500).send("something went wrong")
} else {
return res.json(results);
}
})
};

module.exports = { getEmployees, getEmployeesById, getEmployeesByFirstName }
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const port = process.env.PORT || 4000;

app.use(express.json());

app.listen(port, () => console.log("Server started"));
24 changes: 24 additions & 0 deletions mysql/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const mysql = require('mysql')

class Connection {
constructor() {
if (!this.pool) {
console.log('creating connection...')
this.pool = mysql.createPool({
connectionLimit: 100,
host: 'fotohut.org',
user: 'fotohut_ACAPub',
password: 'lE%3o*y7mc2!wjbE86',
database: 'fotohut_ACAHackathon'
})

return this.pool
}

return this.pool
}
}

const instance = new Connection()

module.exports = instance;
Loading