Skip to content

mvp #293

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 1 commit into
base: main
Choose a base branch
from
Open

mvp #293

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

const router = express.Router()


router.get('/', (req, res) => {
//res.json('foo')
Post.find()
.then(found => {
//throw new Error('ka-zoingggggGGGG!')
res.json(found)
})
.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) => {
try {
//throw new Error('ka-bloinGGGGGGZ!')
const post = await Post.findById(req.params.id)
console.log('--->', post)
if(!post){
res.status(404).json({
message: "The post with the specified ID does not exist"
})
} else {
res.json(post)
}
} catch (err) {
res.status(500).json({
message: "The post with the specified ID does not exist",
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 {
console.log('success')
Post.insert({ title, contents })
.then( ({id}) => { //stuff is an id
console.log('check -->', id)
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.delete('/:id', async (req, res) => {
try {
//throw new Error('it :( did not work mate')
const post = await Post.findById(req.params.id)
if (!post) {
res.status(404).json({
message: "The post with the specified ID does not exist"
})
} else {
await Post.remove(req.params.id)
//console.log(post)
res.json(post)
}
} catch (err) {
res.status(500).json({
message: "The post could not be removed",
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( stuff => {
//console.log(stuff)
if (!stuff) {
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.json(post)
}
})
.catch(err => {
res.status(500).json({
message: "The post information could not be modified",
err: err.message,
stack: err.stack
})
})

}

})
router.get('/:id/comments', async (req, res) => { //or comments
try {
//throw new Error('ka-bloinGGGGGGZ!')
const post = await Post.findById(req.params.id)
console.log('--->', post)
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.json(messages)
}
} catch (err) {
res.status(500).json({
message: "The comments information could not be retrieved",
err: err.message,
stack: err.stack
})
}
})


module.exports = router
19 changes: 19 additions & 0 deletions api/server.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
// 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.use('*', (req, res) => {
console.log('* default catch upp')
res.status(404).json({
message: 'not found'
})
})

module.exports = server
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
// require your server and launch it here
console.log('index.js console.log')

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

const PORT = 5000

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

//git log | git reset --hard firstLettersOfHashFromLog
Loading