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
@@ -1,7 +1,7 @@
# Node API 1 Project Starter Code

## Introduction

xxx
- Building a RESTful API.
- Performing CRUD operations.
- Writing API endpoints.
Expand Down
114 changes: 112 additions & 2 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,113 @@
// BUILD YOUR SERVER HERE
const express = require("express");
const User = require("./users/model");

module.exports = {}; // EXPORT YOUR SERVER instead of {}
const server = express();

server.use(express.json());

//CREATE NEW USER
server.post("/api/users", async (req, res) => {
try {
const { name, bio } = req.body;
if (!name || !bio) {
res
.status(400)
.json({ message: "Please provide name and bio for the user" });
} else {
const user = await User.insert({ name, bio });
res.status(201).json(user);
}
} catch (error) {
res
.status(500)
.json({
message: "There was an error while saving the user to the database",
});
}
});

// GET ALL USERS
server.get("/api/users", (req, res) => {
User.find()
.then((users) => {
res.json(users);
})
.catch((err) => {
res.status(500).json({
message: "The users information could not be retrieved",
error: err.message,
});
});
});

//GET USER BY ID

server.get("/api/users/:id", async (req, res) => {
User.findById();
try {
const { id } = req.params;
const user = await User.findById(id);
if (!user) {
res
.status(404)
.json({ message: "The user with the specified ID does not exist" });
} else {
res.status(200).json(user);
}
} catch (err) {
res.status(500).json({
message: "The user information could not be retrieved",
error: err.message,
});
}
});

//DELETE USER BY ID
server.delete("/api/users/:id", async (req, res) => {
const { id } = req.params;
User.remove(id)
.then((deletedUser) => {
if (!deletedUser) {
res.status(404).json({
message: "The user with the specified ID does not exist",
});
} else {
res.json(deletedUser);
}
})
.catch((err) => {
res.status(500).json({
message: "The user could not be removed",
error: err.message,
});
});
});

//UPDATE USER BY ID
server.put("/api/users/:id", async (req, res) => {
try {
const { id } = req.params;
const { name, bio } = req.body;

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

module.exports = server;
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ const server = require('./api/server');
const port = 9000;

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