forked from bradtraversy/storybooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brad Traversy
authored and
Brad Traversy
committed
Jun 19, 2020
1 parent
b5a2c68
commit 1b3ad25
Showing
6 changed files
with
116 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const moment = require('moment') | ||
|
||
module.exports = { | ||
formatDate: function (date, format) { | ||
return moment(date).utc().format(format) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = { | ||
ensureAuth: function (req, res, next) { | ||
if (req.isAuthenticated()) { | ||
return next() | ||
} else { | ||
res.redirect('/') | ||
} | ||
}, | ||
ensureGuest: function (req, res, next) { | ||
if (req.isAuthenticated()) { | ||
res.redirect('/dashboard') | ||
} else { | ||
return next() | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const mongoose = require('mongoose') | ||
|
||
const StorySchema = new mongoose.Schema({ | ||
title: { | ||
type: String, | ||
required: true, | ||
trim: true, | ||
}, | ||
body: { | ||
type: String, | ||
required: true, | ||
}, | ||
status: { | ||
type: String, | ||
default: 'public', | ||
enum: ['public', 'private'], | ||
}, | ||
user: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User', | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now, | ||
}, | ||
}) | ||
|
||
module.exports = mongoose.model('Story', StorySchema) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,30 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
const { ensureAuth, ensureGuest } = require('../middleware/auth') | ||
|
||
const Story = require('../models/Story') | ||
|
||
// @desc Login/Landing page | ||
// @route GET / | ||
router.get('/', (req, res) => { | ||
router.get('/', ensureGuest, (req, res) => { | ||
res.render('login', { | ||
layout: 'login', | ||
}) | ||
}) | ||
|
||
// @desc Dashboard | ||
// @route GET /dashboard | ||
router.get('/dashboard', (req, res) => { | ||
res.render('dashboard') | ||
router.get('/dashboard', ensureAuth, async (req, res) => { | ||
try { | ||
const stories = await Story.find({ user: req.user.id }).lean() | ||
res.render('dashboard', { | ||
name: req.user.firstName, | ||
stories, | ||
}) | ||
} catch (err) { | ||
console.error(err) | ||
res.render('error/500') | ||
} | ||
}) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,38 @@ | ||
<h1>Dashboard</h1> | ||
<h6>Dashboard</h6> | ||
<h3>Welcome {{name}}</h3> | ||
<p>Here are your stories</p> | ||
{{#if stories}} | ||
<table class="striped"> | ||
<thead> | ||
<tr> | ||
<th>Title</th> | ||
<th>Date</th> | ||
<th>Status</th> | ||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{{#each stories}} | ||
<tr> | ||
<td><a href="/stories/{{_id}}">{{title}}</a></td> | ||
<td>{{formatDate createdAt 'MMMM Do YYYY, h:mm:ss a'}}</td> | ||
<td><span class="dash-status">{{status}}</span></td> | ||
<td> | ||
<a href="/stories/edit/{{_id}}" class="btn btn-float"> | ||
<i class="fas fa-edit"></i> | ||
</a> | ||
|
||
<form action="/stories/{{_id}}" method="POST" id="delete-form"> | ||
<input type="hidden" name="_method" value="DELETE"> | ||
<button type="submit" class="btn red"> | ||
<i class="fas fa-trash"></i> | ||
</button> | ||
</form> | ||
</td> | ||
</tr> | ||
{{/each}} | ||
</tbody> | ||
</table> | ||
{{else}} | ||
<p>You have not created any stories</p> | ||
{{/if}} |