Skip to content

Commit

Permalink
Update story
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Traversy authored and Brad Traversy committed Jun 19, 2020
1 parent 16b778e commit b3e0f8d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
22 changes: 21 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const mongoose = require('mongoose')
const dotenv = require('dotenv')
const morgan = require('morgan')
const exphbs = require('express-handlebars')
const methodOverride = require('method-override')
const passport = require('passport')
const session = require('express-session')
const MongoStore = require('connect-mongo')(session)
Expand All @@ -23,13 +24,31 @@ const app = express()
app.use(express.urlencoded({ extended: false }))
app.use(express.json())

// Method override
app.use(
methodOverride(function (req, res) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
let method = req.body._method
delete req.body._method
return method
}
})
)

// Logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}

// Handlebars Helpers
const { formatDate, stripTags, truncate, editIcon } = require('./helpers/hbs')
const {
formatDate,
stripTags,
truncate,
editIcon,
select,
} = require('./helpers/hbs')

// Handlebars
app.engine(
Expand All @@ -40,6 +59,7 @@ app.engine(
stripTags,
truncate,
editIcon,
select,
},
defaultLayout: 'main',
extname: '.hbs',
Expand Down
12 changes: 12 additions & 0 deletions helpers/hbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@ module.exports = {
return ''
}
},
select: function (selected, options) {
return options
.fn(this)
.replace(
new RegExp(' value="' + selected + '"'),
'$& selected="selected"'
)
.replace(
new RegExp('>' + selected + '</option>'),
' selected="selected"$&'
)
},
}
51 changes: 51 additions & 0 deletions routes/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,55 @@ router.get('/', ensureAuth, async (req, res) => {
}
})

// @desc Show edit page
// @route GET /stories/edit/:id
router.get('/edit/:id', ensureAuth, async (req, res) => {
try {
const story = await Story.findOne({
_id: req.params.id,
}).lean()

if (!story) {
return res.render('error/404')
}

if (story.user != req.user.id) {
res.redirect('/stories')
} else {
res.render('stories/edit', {
story,
})
}
} catch (err) {
console.error(err)
return res.render('error/500')
}
})

// @desc Update story
// @route PUT /stories/:id
router.put('/:id', ensureAuth, async (req, res) => {
try {
let story = await Story.findById(req.params.id).lean()

if (!story) {
return res.render('error/404')
}

if (story.user != req.user.id) {
res.redirect('/stories')
} else {
story = await Story.findOneAndUpdate({ _id: req.params.id }, req.body, {
new: true,
runValidators: true,
})

res.redirect('/dashboard')
}
} catch (error) {
console.error(err)
return res.render('error/500')
}
})

module.exports = router
36 changes: 36 additions & 0 deletions views/stories/edit.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<h3>Edit Story</h3>
<div class="row">
<form action="/stories/{{story._id}}" method="POST" class="col s12">
<input type="hidden" name="_method" value="PUT">
<div class="row">
<div class="input-field">
<input type="text" id="title" name="title" value="{{story.title}}">
<label for="title">Title</label>
</div>
</div>

<div class="row">
<div class="input-field">
<select id="status" name="status">
{{#select story.status}}
<option value="public" selected>Public</option>
<option value="private">Private</option>
{{/select}}
</select>
<label for="status">Status</label>
</div>
</div>

<div class="row">
<div class="input-field">
<h5>Tell Us Your Story:</h5>
<textarea id="body" name="body">{{story.body}}</textarea>
</div>
</div>

<div class="row">
<input type="submit" value="Save" class="btn">
<a href="/dashboard" class="btn orange">Cancel</a>
</div>
</form>
</div>

0 comments on commit b3e0f8d

Please sign in to comment.