Skip to content
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
72 changes: 39 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,67 +12,73 @@ We are going to create a CRUD app using our knowledge of _routes_ and _controlle

#### 1. Index

* In the `index.js` file, import and use the `bodyParser` in order to ensure that the body from our POST requests is appropriately cast to JSON.
- In the `index.js` file, import and use the `bodyParser` in order to ensure that the body from our POST requests is appropriately cast to JSON.

#### 2. Routes

We will create five routes. The data for these routes is provided in the `data` folder. It is up to you to make use of this data correctly. For the POST route you may import and use the `sampleUser` file instead of creating a user manually.

* Create a `routes` folder to hold our routes. Underneath make a file called `users.js` to represent the users router
- Create a `routes` folder to hold our routes. Underneath make a file called `users.js` to represent the users router

* Create the following routes inside `users.js`
- Create the following routes inside `users.js`

* GET /users
* Return all users
- GET /users

* GET /users/:id
* Return just the user that matches the path param (id)
- Return all users

* POST /users
* Create a new user (sampleUser). Find a way to increment the id so that we always insert the next available id in the list. Currently we have users 1-10 (_data/index_). The next user should be 11
- GET /users/:id

* PUT /users/:id
* Update one user matching the path param (id). You may again use the sampleUser code as your "body" for this request
- Return just the user that matches the path param (id)

* DELETE /users/:id
* Delete one user by it's id
- POST /users

- Create a new user (sampleUser). Find a way to increment the id so that we always insert the next available id in the list. Currently we have users 1-10 (_data/index_). The next user should be 11

- PUT /users/:id

- Update one user matching the path param (id). You may again use the sampleUser code as your "body" for this request

- DELETE /users/:id
- Delete one user by it's id

_You may chose to alter these routes so they appear as ('/', '/:id') in your users file and then prefix them all with '/users' when we import them into index.. but you are not required to do so_

#### 3. Controllers

* Create a `controllers` folder to hold our routes. Underneath make a file called `users.js` to represent the users controller
- Create a `controllers` folder to hold our routes. Underneath make a file called `users.js` to represent the users controller

We will create five controller functions. These will correspond to the routes above. ALL LOGIC for retrieving or updating the "data" should be done here. After that is complete we will import these controller functions into the routes. The end result of your routes should look like this: `router.get('/users', usersController.listUsers)`. Create the following controller functions:

* listUsers
* Should retrieve the entire array from _data/index_
- listUsers

* showUser
* Should retrieve just the user that matches the passed-in id
- Should retrieve the entire array from _data/index_

* createUser
* Should add a user to the array
- showUser

* updateUser
* Should update one user in the array based on its id
- Should retrieve just the user that matches the passed-in id

* deleteUser
* Should delete one user from the array based on its id
- createUser

- Should add a user to the array

- updateUser

- Should update one user in the array based on its id

- deleteUser
- Should delete one user from the array based on its id

#### 4. Error handling

Make sure that you are handling common use cases. For example, if we try to find a user by its _id_ and no id exists, we should return a 404 status code and no data. Likewise for the PUT and DELETEs, if a user doesn't exist return a 400 (bad request) status code.


## POINTS

* App starts (done for you) - 10 pts
* Routes/Controllers structured appropriately - 20 pts
* Routes/Controllers correctly imported in index.js - 10 pts
* Can GET users all at once or by id - 10 pts
* Can POST to create a new user - 10 pts
* Can PUT to update a user - 10 pts
* Can DELETE a user by its id - 10 pts
* Error handling - 20 pts
- App starts (done for you) - 10 pts
- Routes/Controllers structured appropriately - 20 pts
- Routes/Controllers correctly imported in index.js - 10 pts
- Can GET users all at once or by id - 10 pts
- Can POST to create a new user - 10 pts
- Can PUT to update a user - 10 pts
- Can DELETE a user by its id - 10 pts
- Error handling - 20 pts
52 changes: 52 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const users = require("../data/index.js");
const sampleUser = require("../data/sampleUser.js");

const listUsers = (req, res) => {
res.json(users);
};

const showUser = (req, res) => {
let id = req.params.id - 1;
if (users[id]) {
res.json(users[id]);
} else {
res.status(404).send("User not found.");
}
};

const createUser = (req, res) => {
let counter = users.length;
let newUser = sampleUser;
newUser.id = counter + 1;
users.push(newUser);
res.json(users[newUser.id - 1]);
};

const updateUser = (req, res) => {
let id = req.params.id - 1;
if (users[id]) {
users[req.params.id - 1] = sampleUser;
users[req.params.id - 1].id = req.params.id;
res.json(users[req.params.id - 1]);
} else {
res.status(400).send("User not found.");
}
};

const deleteUser = (req, res) => {
let id = req.params.id - 1;
if (users[id]) {
users.splice(req.params.id - 1, 1);
res.send(users);
} else {
res.status(400).send("User not found.");
}
};

module.exports = {
listUsers,
showUser,
createUser,
updateUser,
deleteUser,
};
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const express = require('express')
const app = express()
const port = process.env.PORT || 4000
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.json());

app.get('/', (req, res) => res.send('default route'))
const port = process.env.PORT || 4000;

app.get("/", (req, res) => res.send("default route"));

app.use(require("./routes/users.js"));

app.listen(port, () => {
console.log('app is listening on:', port)
})
console.log("app is listening on:", port);
});
41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const express = require("express");
const router = express.Router();
const usersController = require("../controllers/users.js");

router.get("/users", usersController.listUsers);

router.get("/users/:id", usersController.showUser);

router.post("/users", usersController.createUser);

router.put("/users/:id", usersController.updateUser);

router.delete("/users/:id", usersController.deleteUser);

module.exports = router;