Skip to content

code review #283

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

Open
wants to merge 5 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
144 changes: 143 additions & 1 deletion api/posts/posts-router.js
Original file line number Diff line number Diff line change
@@ -1 +1,143 @@
// implement your posts router here
const express = require('express')
const Post = require('./posts-model')
const router = express.Router()

router.get('/', (req, res) => {
Post.find()
.then(post => {
res.json(post)
})
.catch(err => {
res.status(500).json({
message: "The posts information could not be retrieved",
err: err.message,
stack: err.stack,
})
})
})

router.get('/:id', async (req, res) => {
const { id } = req.params
try {
const post = await Post.findById(id)
if (!post) {
res.status(404).json({
message: "The post with the specified ID does not exist",
})
} else {
res.status(200).json(post)
}
} catch (err) {
res.status(500).json({
message: "The posts information could not be retrieved",
err: err.message,
stack: err.stack,
})
}

})

router.post('/', (req, res) => {
const { title, contents} = req.body
if (!title || !contents) {
res.status(400).json({
message: "Please provide title and contents for the post",
})
} else {
Post.insert({ title, contents})
.then(({id}) => { //destruct the id so that you can grab not just the id but the title etc.
return Post.findById(id)
})
.then(post => {
res.status(201).json(post)
})
.catch(err => {
res.status(500).json({
message: "There was an error while saving the post to the database",
err: err.message,
stack: err.stack,
})
})
}
})

router.put('/:id', (req, res) => {
const { title, contents} = req.body
if (!title || !contents) {
res.status(400).json({
message: "Please provide title and contents for the post",
})
} else {
Post.findById(req.params.id)
.then(post => {
if(!post) {
res.status(404).json({
message: "The post with the specified ID does not exist"
})
} else {
return Post.update(req.params.id, req.body)
}
})
.then(data => {
if (data)
return Post.findById(req.params.id)
})
.then(post => {
if (post) {
res.status(201).json(post)
}
})
.catch(err => {
res.status(500).json({
message: "There was an error while saving the post to the database",
err: err.message,
stack: err.stack,
})
})
}
})

router.delete('/:id', async (req, res) => {
const { id } = req.params
try {
const post = await Post.findById(id)
if (!post) {
res.status(404).json({
message: "The post with the specified ID does not exist"
})
} else {
await Post.remove(id)
res.status(200).json(post)
}
} catch (err) {
res.status(500).json({
message: "The posts information could not be retrieved",
err: err.message,
stack: err.stack,
})
}
})

router.get('/:id/comments', async (req, res) => {
const { id } = req.params
try {
const post = await Post.findById(id)
if (!post) {
res.status(404).json({
message: "The post with the specified ID does not exist"
})
} else {
const messages = await Post.findPostComments(req.params.id)
res.status(200).json(messages)
}
}catch (err) {
res.status(500).json({
message: "The posts information could not be retrieved",
err: err.message,
stack: err.stack,
})
}

})

module.exports = router;
13 changes: 13 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
// implement your server here
const express = require('express')
const postsRouter = require('./posts/posts-router.js')
const server = express()
server.use(express.json())

server.use('/api/posts', postsRouter)
// require your posts router and connect it here

server.use('*', (req, res) => {
res.status(404).json({
message: 'not found'
})
})
module.exports = server;
Binary file modified data/database.db3
Binary file not shown.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
// require your server and launch it here
const server = require('./api/server');

const port = 5000;

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