Skip to content

Commit a7f6187

Browse files
committed
partials are populating with data
1 parent 3f2d7a2 commit a7f6187

13 files changed

+151
-51
lines changed

controllers/api/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const router = require("express").Router();
44
const userRoutes = require("./userRoutes");
5+
const postRoutes = require("./postRoutes");
56

67
router.use("/users", userRoutes);
7-
8+
router.use("/post", postRoutes);
89
module.exports = router;

controllers/api/postRoutes.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const router = require("express").Router();
2+
const { Post, User } = require("../../models");
3+
4+
const withAuth = require("../../utils/auth");
5+
6+
// * Create a new post
7+
router.post("/", async (req, res) => {
8+
try {
9+
const newPost = req.body;
10+
const postData = await Post.create({
11+
...newPost,
12+
user_id: req.session.user_id,
13+
});
14+
res.status(200).json(postData);
15+
} catch (err) {
16+
res.status(400).json(err);
17+
}
18+
});
19+
20+
// * Front-End Route for one post's details
21+
router.get("/:id", async (req, res) => {
22+
console.info(`${req.method} request received for post details`);
23+
try {
24+
const thisPost = await Post.findAll({
25+
attributes: ["id", "title", "text", "createdAt"],
26+
});
27+
28+
const post = thisPost.map((post) => post.get({ plain: true }));
29+
30+
res.render("post-details", {
31+
post,
32+
logged_in: req.session.logged_in,
33+
email: req.session.email,
34+
username: req.session.username,
35+
title: "Home",
36+
});
37+
} catch (err) {
38+
res.status(500).json(err);
39+
}
40+
});
41+
42+
module.exports = router;

controllers/api/userRoutes.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ router.post("/login", async (req, res) => {
6262
req.session.save(() => {
6363
req.session.page_visited = userData.page_visited;
6464
req.session.user_id = userData.id;
65-
req.session.email = userData.email;
66-
req.session.isAdmin = userData.isAdmin;
6765
req.session.username = userData.username;
66+
req.session.email = userData.email;
6867
// set logged in to true
6968
req.session.logged_in = true;
7069
res.json({ user: userData, message: "You are now logged in!" });

controllers/homeRoutes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ router.get("/", async (req, res) => {
66
try {
77
// gets all posts to display on the page
88
const postData = await Post.findAll({
9-
attributes: ["title", "text", "createdAt"],
9+
attributes: ["id", "title", "text", "createdAt"],
1010
});
1111

1212
const posts = postData.map((post) => post.get({ plain: true }));

public/css/style.css

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.date {
2+
font-size: small;
3+
justify-content: start;
4+
}
5+
6+
.post-link {
7+
text-decoration: none;
8+
}
9+
10+
.post-link:hover {
11+
text-decoration: none;
12+
}

seeds/postData.json

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
[
22
{
33
"id": 1,
4-
"title": "Sample Title",
4+
"title": "Post Title",
55
"text": "This is a sample post",
66
"user_id": 1
7+
},
8+
{
9+
"id": 3,
10+
"title": "Post #3",
11+
"text": "Lorem ipsum dolor sit, amet consectetur adipisicing elit. Autem eveniet ex fugiat laboriosam, minima temporibus ullam recusandae odio exercitationem voluptas magni nesciunt repudiandae doloribus tempore? Officia aliquid illum molestias quaerat?",
12+
"user_id": 1
13+
},
14+
{
15+
"id": 4,
16+
"title": "Post Four",
17+
"text": "Lorem ipsum dolor sit, amet consectetur adipisicing elit. Autem eveniet ex fugiat laboriosam, minima temporibus ullam recusandae odio exercitationem voluptas magni nesciunt repudiandae doloribus tempore? Officia aliquid illum molestias quaerat?",
18+
"user_id": 2
719
}
820
]

seeds/userData.json

+5
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33
"username": "Andrew",
44
"email": "[email protected]",
55
"password": "password"
6+
},
7+
{
8+
"username": "L Figgy",
9+
"email": "[email protected]",
10+
"password": "password"
611
}
712
]

views/home.handlebars

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
homepage
1+
<div class="container-fluid">
2+
<div class="row justify-content-center">
3+
<div class="col-6 mt-3">
4+
{{>posts}}
5+
</div>
26

7+
</div>
8+
</div>
39

4-
{{>posts}}

views/layouts/main.handlebars

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{{!-- BOOTSTRAP 4.6 --}}
1212
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
1313
integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
14-
14+
<link rel="stylesheet" href="/css/style.css">
1515
<title>{{title}} | Tech Blog</title>
1616
</head>
1717

views/login.handlebars

+31-27
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
<div class="container-fluid">
2-
<div class="row">
3-
<div class="col-12">
4-
<form class="form login-form">
5-
<div class="form-group">
6-
<input
7-
class="form-input"
8-
type="text"
9-
id="email-login"
10-
placeholder="Email"
11-
/>
12-
</div>
13-
<div class="form-group">
14-
{{! <label for="password-login">Password:</label> }}
15-
<input
16-
class="form-input"
17-
type="password"
18-
id="password-login"
19-
placeholder="Password"
20-
/>
21-
</div>
22-
<div class="form-group">
23-
<button class="btn btn-primary" type="submit">Login</button>
24-
</div>
25-
</form>
26-
</div>
27-
<p>Not yet a Member?</p>
28-
<a href="/register"><button class="btn btn-primary">Signup</button></a>
2+
<div class="row justify-content-center">
3+
<h1>Login</h1>
4+
</div>
5+
<div class="row justify-content-center">
296

7+
<form class="form login-form">
8+
<div class="form-group">
9+
<input
10+
class="form-input"
11+
type="text"
12+
id="email-login"
13+
placeholder="Email"
14+
/>
15+
</div>
16+
<div class="form-group">
17+
{{! <label for="password-login">Password:</label> }}
18+
<input
19+
class="form-input"
20+
type="password"
21+
id="password-login"
22+
placeholder="Password"
23+
/>
24+
</div>
25+
<div class="form-group">
26+
<button class="btn btn-sm btn-primary" type="submit">Login</button>
27+
</div>
28+
</form>
3029
</div>
30+
<div class="col-12 d-flex justify-content-center">
31+
<p>Not yet a Member?</p>
32+
<a href="/register"><button class="btn btn-sm btn-primary ml-2">Sign Up</button></a>
33+
</div>
34+
3135
</div>
3236

3337
<script src="/js/login.js"></script>

views/partials/main-menu.handlebars

+2-8
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,21 @@
1414
<div class="collapse navbar-collapse" id="navbarText">
1515
<ul class="navbar-nav mr-auto">
1616
<li class="nav-item">
17-
<a class="nav-link" href="/">Home</a>
17+
<a class="nav-link" href="/">Homepage</a>
1818
</li>
1919
<li class="nav-item">
2020
<a class="nav-link" href="/dashboard">Dashboard</a>
2121
</li>
22-
2322
</ul>
23+
2424
{{#if logged_in}}
2525
<span class="navbar-text">
2626
Welcome,
2727
{{username}}
2828
</span>
29-
{{/if}}
30-
{{#if logged_in}}
31-
3229
<a class="nav-link" href="" id="logout">Logout</a>
33-
3430
{{else}}
35-
3631
<a class="nav-link" href="/login">Login</a>
37-
3832
{{/if}}
3933

4034
</div>

views/partials/posts.handlebars

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
<div>
2-
<h3>Posts</h3>
3-
</div>
4-
51
{{#each posts}}
6-
<div class="card">
7-
<h3>{{title}}</h3>
8-
<p>{{text}}</p>
9-
</div>
2+
3+
<a href="/api/post/{{id}}" class="post-link">
4+
<div class="card">
5+
<h5 class="card-header">
6+
{{title}}
7+
</h5>
8+
<div class="card-body">
9+
<p class="card-text date">
10+
{{createdAt}}
11+
</p>
12+
</div>
13+
</div>
14+
</a>
1015
{{/each}}

views/post-details.handlebars

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{{#each post}}
2+
3+
<section class="card">
4+
<a href="/painting/{{id}}">
5+
<h5 class="card-header">
6+
<p>Posted By:</p>
7+
{{req.session.username}}
8+
</h5>
9+
<div class="card-body">
10+
<h5 class="card-title">{{title}}</h5>
11+
12+
<p class="card-text"> {{text}}</p>
13+
<p class="card-text date">
14+
{{createdAt}}
15+
16+
</p>
17+
</div>
18+
</a>
19+
</section>
20+
21+
{{/each}}

0 commit comments

Comments
 (0)