Skip to content

Commit f236591

Browse files
committed
individual post get is working
1 parent a7f6187 commit f236591

File tree

8 files changed

+85
-36
lines changed

8 files changed

+85
-36
lines changed

controllers/api/postRoutes.js

+20-21
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,25 @@ router.post("/", async (req, res) => {
1717
}
1818
});
1919

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-
});
20+
// router.get("/:id", withAuth, async (req, res) => {
21+
// console.info(`${req.method} request received for post details`);
22+
// try {
23+
// const thisPost = await Post.findAll({
24+
// attributes: ["id", "title", "text", "createdAt"],
25+
// });
26+
//
27+
// const post = thisPost.map((post) => post.get({ plain: true }));
28+
//
29+
// res.render("post-details", {
30+
// post,
31+
// logged_in: req.session.logged_in,
32+
// email: req.session.email,
33+
// username: req.session.username,
34+
// title: "Home",
35+
// });
36+
// } catch (err) {
37+
// res.status(500).json(err);
38+
// }
39+
// });
4140

4241
module.exports = router;

controllers/homeRoutes.js

+22
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ router.get("/register", (req, res) => {
3838
title: "Register",
3939
});
4040
});
41+
// ! FRONT END ROUTES
4142

4243
// * Route for /dash
4344
router.get("/dashboard", async (req, res) => {
@@ -63,5 +64,26 @@ router.get("/dashboard", async (req, res) => {
6364
});
6465
}
6566
});
67+
// * Front-End Route for one post's details
68+
router.get("/post/:id", async (req, res) => {
69+
const postData = await Post.findByPk(req.params.id, {
70+
attributes: ["id", "title", "text", "createdAt"],
71+
include: [
72+
{
73+
model: User,
74+
attributes: ["username", "email", "id"],
75+
},
76+
],
77+
});
78+
79+
const post = postData.get({ plain: true });
80+
81+
res.render("post-details", {
82+
post,
83+
username: req.session.username,
84+
email: req.session.email,
85+
logged_in: req.session.logged_in,
86+
});
87+
});
6688

6789
module.exports = router;

models/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
const User = require("./User");
22
const Post = require("./Post");
3+
// relationships here
4+
User.hasMany(Post, {
5+
foreignKey: "user_id",
6+
onDelete: "CASCADE",
7+
});
38

9+
Post.belongsTo(User, {
10+
foreignKey: "user_id",
11+
});
412
module.exports = {
513
User,
614
Post,

public/css/style.css

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,16 @@
99

1010
.post-link:hover {
1111
text-decoration: none;
12-
}
12+
}
13+
14+
a {
15+
text-decoration: none;
16+
}
17+
18+
a:hover {
19+
text-decoration: none;
20+
}
21+
22+
/* .card {
23+
24+
} */

views/home.handlebars

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="container-fluid">
22
<div class="row justify-content-center">
3-
<div class="col-6 mt-3">
3+
<div class="col-12 col-md-8 col-xl-6 mt-3">
44
{{>posts}}
55
</div>
66

views/layouts/main.handlebars

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
</head>
1717

1818
<body>
19-
2019
{{>main-menu}}
21-
22-
{{{body}}}
20+
21+
<div class="container-fluid">
22+
<div class="row justify-content-center">
23+
{{{body}}}
24+
</div>
25+
</div>
2326

2427
{{#if logged_in}}
2528
<script src="/js/logout.js"></script>

views/partials/posts.handlebars

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{{#each posts}}
22

3-
<a href="/api/post/{{id}}" class="post-link">
3+
<a href="/post/{{id}}" class="post-link">
44
<div class="card">
55
<h5 class="card-header">
66
{{title}}

views/post-details.handlebars

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
{{#each post}}
1+
22

33
<section class="card">
4-
<a href="/painting/{{id}}">
5-
<h5 class="card-header">
6-
<p>Posted By:</p>
7-
{{req.session.username}}
8-
</h5>
4+
<a href="/post/{{post.id}}">
5+
<div class="card-header ">
6+
7+
{{#if logged_in}}
8+
<p style="display: inline;">Posted By:</p>
9+
<h5 style="display: inline;">
10+
11+
{{username}}
12+
</h5>
13+
</div>
14+
{{/if}}
915
<div class="card-body">
10-
<h5 class="card-title">{{title}}</h5>
16+
<h5 class="card-title">{{post.title}}</h5>
1117

12-
<p class="card-text"> {{text}}</p>
18+
<p class="card-text"> {{post.text}}</p>
1319
<p class="card-text date">
1420
{{createdAt}}
1521

@@ -18,4 +24,3 @@
1824
</a>
1925
</section>
2026

21-
{{/each}}

0 commit comments

Comments
 (0)