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

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

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

router.post('/api/:posts',(req, res) => {
console.log(req.params.posts)
Post.insert(req.params.posts)
.then(posts => {
res.status(201).json(posts)
})
.catch(err => {
console.log(err)
res.status(500).json({ message: "There was an error while saving the post to the database" })
});
});

router.put('/api/posts/:id',(req, res) => {
Post.update(req.params.id, req.params.posts)
.then(posts => {
if (posts) {
res.status(201).json(posts)
} else {
res.status(400).json({ message: "Please provide title and contents for the post"})
}
})
.catch(err => {
console.log(err)
res.status(500).json({ message: "The post information could not be modified"})
});
});

router.delete('/api/posts/:id', (req, res) => {
Post.remove(req.params.id)
.then(posts => {
if (posts) {
res.status(200).json(posts)
} else {
res.status(404).json({ message: "The post with the specified ID does not exist"
})
}
})
.catch(err => {
console.log(err)
res.status(500).json({ message: "The post could not be removed"})
});
});

router.get('/api/posts/:id/comments', (req, res) => {
Post.findPostComments(req.params.id)
.then(posts => {
if (posts) {
res.status(200).json(posts)
} else {
res.status(404).json({ message: "The post with the specified ID does not exist"})
}
})
.catch(error => {
res.status(500).json({
message: "The comments information could not be retrieved"
});
});
});

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

const server = express();

const postsRouter = require('./posts/posts-router')

server.use(express.json());

server.use(postsRouter);

server.listen(4000, () => {
console.log('The server is running on port 4000');
});

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 express = require('express');

const server = require('./api/server');


Loading