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

Steve rivera #454

Open
wants to merge 4 commits into
base: main
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
88 changes: 87 additions & 1 deletion api/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,89 @@
// BUILD YOUR SERVER HERE

module.exports = {}; // EXPORT YOUR SERVER instead of {}
const express = require("express")
const userServer = require("./users/model")

const server = express()

server.use(express.json())

//[POST] /api/users
server.post("/api/users", (req, res) => {
const newUser = req.body
userServer.insert(newUser)
.then(user => {
res.status(201).json(user)
})
.catch(err => {
res.status(500).json({message: err.message})
})
})

//[GET] /api/users
server.get("/api/users", (req, res) => {
userServer.find()
.then(users => {
res.status(200).json(users)
})
.catch(err => {
res.status(500).json({message: err.message})
})
})

//[GET] /api/users/:id
server.get("/api/users/:id", (req, res) => {
const userId = req.params.id
userServer.findById(userId)
.then(user => {
if (!user) {
res.status(400).json(`user with id ${userId} does not exist`)
} else {
res.status(200).json(user)
}
})
.catch(err => {
res.status(500).json({message: err.message})
})
})

//[DELETE] /api/users/:id
server.delete("/api/users/:id", async (req, res) => {
try {
const { id } = req.params
const deletedUser = await userServer.remove(id)
if (!deletedUser) {
res.status(404).json("User does not exist")
} else {
res.status(201).json(deletedUser)
}
}
catch (err) {
res.status(500).json({message: err.message})
}
})

//[PUT] /api/users/:id
server.put("/api/users/:id", async (req, res) => {
const { id } = req.params
const changes = req.body
try {
if (!changes.name || !changes.bio) {
res.status(422).json({message: "name and bio required"})
} else {
const updateUser = await userServer.update(id, changes)
if (!updateUser) {
res.status(500).json({message: "user doesn't exist"})
} else {
res.status(200).json(updateUser)
}
}
}catch (err) {
res.status(500).json({message: err.message})
}
})

//[GET] / makes sure server works
server.use("*", (req, res) => {
res.status(404).json({message: "server is live"})
})
module.exports = server; // EXPORT YOUR SERVER instead of {}
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ const server = require('./api/server');

const port = 9000;

server.listen(port, () => {
console.log("server is running on port 9000")
})
// START YOUR SERVER HERE
Loading