Skip to content

inital push #289

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


//| 1 | GET | /api/posts | Returns **an array of all the post objects** contained in the database

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

//| 2 | GET | /api/posts/:id | Returns **the post object with the specified id**
router.get('/: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 => {
res.status(500).json({
message:'The post information could not be retrieved',
err:err.message
})
})
})


//| 3 | POST | /api/posts | Creates a post using the information sent inside the request body and returns **the newly created post object** |
router.post('/', async(req, res) => {


await Post.insert(req.body)
.then(create => {

if(create){
res.status(201).json(create)
}else{
if(!req.body.title || !req.body.contents){
res.status(400).json({message:'Please provide title and contents for the post'})
}
}
})
.catch(err => {
res.status(500).json({
message:'There was an error while saving the post to the database',
err:err.me
})
})
})


//| 4 | PUT | /api/posts/:id | Updates the post with the specified id using data from the request body and **returns the modified document**, not the original |
router.put('/:id', (req, res) => {

})


//| 5 | DELETE | /api/posts/:id | Removes the post with the specified id and returns the **deleted post object** |
router.delete('/:id', (req, res) => {

})


//| 6 | GET | /api/posts/:id/comments | Returns an **array of all the comment objects** associated with the post with the specified id
router.get('/:id/comments', (req, res) => {

})


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 postsRouter = require('./posts/posts-router')
const server = express()

server.use(express.json())
server.use('/api/posts', postsRouter)

server.get('/',(req,res) => {
res.send(`
<h1>hello kitty</h1>
<p>im tired of this chana</p>
`)
})

module.exports = server
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
const server = require("./api/server")

// require your server and launch it here
const port = 7000

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