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
111 changes: 111 additions & 0 deletions api/posts/posts-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,112 @@
// implement your posts router here
const express = require("express");

const server = express();
server.use(express.json());
const model = require("./posts-model");

server.get("/", (req, res) => {
model
.find()
.then((posts) => res.send(posts))
.catch(() => {
res
.status(500)
.send({ message: "The posts information could not be retrieved" });
});
});

server.get(`/:id`, (req, res) => {
const id = req.params.id;
model
.findById(id)
.then((post) => {
return post === undefined || post === null
? res
.status(400)
.send({ message: "The post with the specified ID does not exist" })
: res.status(200).send(post);
})
.catch(() => {
res
.status(500)
.send({ message: "The post information could not be retrieved" });
});
});

server.post("/", (req, res) => {
const post = req.body;
if (!post.title || !post.contents) {
res
.status(400)
.send({ message: "Please provide title and contents for the post" });
} else {
model
.insert(post)
.then(() => res.status(201).send(post))
.catch(() => {
res.status(500).send({
message: "There was an error while saving the post to the database",
});
});
}
});

server.put(`/:id`, (req, res) => {
const id = req.params.id;
const post = req.body;
if (!post.title || !post.contents) {
res
.status(400)
.send({ message: "Please provide title and contents for the post" });
} else {
model
.update(id, post)
.then((updatedPost) => {
return updatedPost === undefined ||
updatedPost === null ||
updatedPost === 0
? res.status(400).send({
message: "The post with the specified ID does not exist",
})
: res.status(200).send(post);
})
.catch(() => {
res
.sendStatus(500)
.send({ message: "The post information could not be modified" });
});
}
});

server.delete(`/:id`, (req, res) => {
const id = req.params.id;
model
.remove(id)
.then((removedPost) => {
return removedPost === undefined ||
removedPost === null ||
removedPost === 0
? res
.status(400)
.send({ message: "The post with the specified ID does not exist" })
: res.status(200).send("Post removed");
})
.catch(() => {
res.status(500).send({ message: "The post could not be removed" });
});
});

server.get(`/:id/comments`, (req, res) => {
const id = req.params.id;
model
.findPostComments(id)
.then((data) => res.status(200).send(data))
.catch(() => {
res
.status(500)
.send({ message: "The comments information could not be retrieved" });
});
});

module.exports = server;
8 changes: 8 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
// implement your server here
// require your posts router and connect it here
const express = require("express");
const postRouter = require("./posts/posts-router");

const server = express();

server.use("/api/posts", postRouter);

module.exports = server;
Binary file modified data/database.db3
Binary file not shown.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
// require your server and launch it here
const server = require("./api/server");

server.listen(5000, () => {
console.log("server listening on port 5000");
});
Loading