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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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 are appropriately cast to JSON.

#### 2. Routes

Expand Down
51 changes: 51 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const users = require("../data/index")
const sampleUser = require('../data/sampleUser.js')


// * Should retrieve the entire array from _data/index_
const listUsers = (req, res) => {
return res.json(users)
}

// * Should retrieve just the user that matches the passed-in id
const showUser = (req, res) => {
let targetId = req.params.id -1
if (users[targetId]){
return res.json(users[targetId])
} else {
res.status(404).send('That user does not exist')
}
}

// * Should add a user to the array
const createUser = (req, res) => {
let counter = users.length
let newUser = sampleUser
newUser.id = counter + 1
users.push(newUser)
return res.json(users[counter])
}

// * Should update one user in the array based on its id
const updateUser = (req, res) => {
let targetId = req.params.id -1
if (users[targetId]){
users[targetId].name = sampleUser.name
return res.json(users[targetId])
} else {
res.status(400).send('That user does not exist')
}
}

// * Should delete one user from the array based on its id
const deleteUser = (req, res) => {
let targetId = req.params.id -1
if (users[targetId]){
users[targetId].isActive = false
res.send("Deleted")
} else {
res.status(400).send('That user does not exist')
}
}

module.exports = {listUsers, showUser, createUser, updateUser, deleteUser}
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = process.env.PORT || 4000
const users = require('./routes/users')

app.get('/', (req, res) => res.send('default route'))
app.use(bodyParser.json())
app.use(users)


app.listen(port, () => {
console.log('app is listening on:', port)
Expand Down
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.

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

router.get('/users', userController.listUsers)
router.get('/users/:id', userController.showUser)
router.post('/users', userController.createUser)
router.put('/users/:id', userController.updateUser)
router.delete('/users/:id', userController.deleteUser)

module.exports = router