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
128 changes: 127 additions & 1 deletion api/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,129 @@
// BUILD YOUR SERVER HERE
const express = require('express')

module.exports = {}; // EXPORT YOUR SERVER instead of {}
const User = require('./users/model')

const server = express()

server.use(express.json())


server.get('/', (req, res) => {
res.send('Hi snoopy')
})



//| GET | /api/users | Returns an array users.
server.get('/api/users', (req, res) => {
User.find()
.then(users => {
res.status(200).json(users)
})
.catch(err => {
res.status(500).json({
message:'The users information could not be retrieved',
err:err.message
})
})

})

//| GET | /api/users/:id | Returns the user object with the specified `id`.
server.get('/api/users/:id', async(req, res) => {

const {id} = req.params
await User.findById(id)
.then(users =>{
if(!users){
res.status(404).json({message:'The user with the specified ID does not exist'})
}else{
res.status(200).json(users)
}

})
.catch(err => {
res.status(500).json({message:'The user information could not be retrieved'})
})

})

//| POST | /api/users | Creates a user using the information sent inside the `request body`.
server.post('/api/users', (req, res) => {
const newUser = req.body

try{
if(!newUser.name || !newUser.bio){
res.status(400).json({message:'Please provide name and bio for the user'})
}else{
User.insert(newUser)
.then(users => {
res.status(201).json(users)
})
.catch(err => {
res.status(500).json({message:'There was an error while saving the user to the database'})
})
}
}catch(err){
res.status(500).json({
message:'There was an error while saving the user to the database',
err:err.message,
stack:err.stack
})
}
})

//| PUT | /api/users/:id | Updates the user with the specified `id` using data from the `request body`. Returns the modified user |
server.put('/api/users/:id', async(req, res) => {



try{
const possibleUser = await User.findById(req.params.id)

if(!possibleUser){
res.status(404).json({
message:'The user with the specified ID does not exist'
})
}else{
if(!req.body.name || !req.body.bio){
res.status(400).json({
message:'Please provide name and bio for the user'
})
}else{
const updatedUser = await User.update(req.params.id, req.body)
res.status(200).json(updatedUser)
}
}
}
catch(err){
res.status(500).json({
message:'The user information could not be modified',
err:err.message,
stack:err.stack
})
}
})

//| DELETE | /api/users/:id | Removes the user with the specified `id` and returns the deleted user.
server.delete('/api/users/:id', async(req, res) => {

try{
const {id} = req.params
const deletedDog = await User.remove(id)
if(!deletedDog){
res.status(404).json({message:'The user with the specified ID does not exist'})
}else{
res.json(deletedDog)
}
}catch(err){
res.status(500).json({
message:'The user could not be removed',
err:err.message,
stack:err.stack
})



}})
module.exports = server; // EXPORT YOUR SERVER instead of {}
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const server = require('./api/server');

const port = 5000;
<<<<<<< HEAD
const port = 5001;

// START YOUR SERVER HERE
server.listen(port, ()=>{
console.log(`listening on port:${port}`)
=======
const port = 5002;

// START YOUR SERVER HERE
server.listen(port, () => {
console.log(`you are running in port ${port}`)
>>>>>>> 89372f2ae7f2615e1ebc2befd554b7b149926f3f
})
Loading