Skip to content

Commit 1fd052b

Browse files
committed
posts are now working
1 parent af4694e commit 1fd052b

12 files changed

+111
-84
lines changed

controllers/api/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const router = require("express").Router();
22
const userRoutes = require("./userRoutes");
33
const postRoutes = require("./postRoutes");
44
const commentRoutes = require("./commentRoutes");
5+
56
router.use("/users", userRoutes);
67
router.use("/posts", postRoutes);
78
router.use("/comments", commentRoutes);

controllers/api/postRoutes.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ router.get('/', (req, res) => {
88
Post.findAll({
99
attributes: ['id',
1010
'title',
11-
'content',
11+
'post_content',
1212
'created_at'
1313
],
1414
order: [
@@ -41,7 +41,7 @@ router.get('/:id', (req, res) => {
4141
id: req.params.id
4242
},
4343
attributes: ['id',
44-
'content',
44+
'post_content',
4545
'title',
4646
'created_at'
4747
],
@@ -80,7 +80,7 @@ router.get('/:id', (req, res) => {
8080
router.post('/', withAuth, (req, res) => {
8181
Post.create({
8282
title: req.body.title,
83-
content: req.body.content,
83+
post_content: req.body.post_content,
8484
user_id: req.session.user_id
8585
})
8686
.then(postData => res.json(postData))
@@ -93,7 +93,7 @@ router.post('/', withAuth, (req, res) => {
9393
router.put('/:id', withAuth, (req, res) => {
9494
Post.update({
9595
title: req.body.title,
96-
content: req.body.content
96+
post_content: req.body.post_content
9797
}, {
9898
where: {
9999
id: req.params.id

controllers/api/userRoutes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ router.get("/:id", (req, res) => {
2121
include: [
2222
{
2323
model: Post,
24-
attributes: ["id", "title", "content", "created_at"],
24+
attributes: ["id", "title", "post_content", "created_at"],
2525
},
2626

2727
{

controllers/dashboardRoutes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ router.get("/", withAuth, (req, res) => {
77
where: {
88
user_id: req.session.user_id,
99
},
10-
attributes: ["id", "title", "content", "created_at"],
10+
attributes: ["id", "title", "post_content", "created_at"],
1111
include: [
1212
{
1313
model: Comment,
@@ -50,7 +50,7 @@ router.get("/edit/:id", withAuth, (req, res) => {
5050
id: req.params.id,
5151
},
5252

53-
attributes: ["id", "title", "content", "created_at"],
53+
attributes: ["id", "title", "post_content", "created_at"],
5454
include: [
5555
{
5656
model: User,

controllers/homeRoutes.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { Post, User, Comment } = require("../models");
33
const router = require("express").Router();
44
router.get("/", (req, res) => {
55
Post.findAll({
6-
attributes: ["id", "title", "content", "created_at"],
6+
attributes: ["id", "title", "post_content", "created_at"],
77
include: [
88
{
99
model: Comment,
@@ -64,7 +64,7 @@ router.get("/post/:id", (req, res) => {
6464
where: {
6565
id: req.params.id,
6666
},
67-
attributes: ["id", "content", "title", "created_at"],
67+
attributes: ["id", "post_content", "title", "created_at"],
6868
include: [
6969
{
7070
model: Comment,
@@ -110,7 +110,7 @@ router.get("/posts-comments", (req, res) => {
110110
where: {
111111
id: req.params.id,
112112
},
113-
attributes: ["id", "content", "title", "created_at"],
113+
attributes: ["id", "post_content", "title", "created_at"],
114114
include: [
115115
{
116116
model: Comment,

controllers/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const router = require("express").Router();
22
const apiRoutes = require("./api");
33
const homeRoutes = require("./homeRoutes");
44
const dashboardRoutes = require("./dashboardRoutes");
5+
56
router.use("/api", apiRoutes);
67
router.use("/", homeRoutes);
78
router.use("/dashboard", dashboardRoutes);

models/Post.js

+31-31
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,39 @@ const { Model, DataTypes } = require('sequelize');
33
const sequelize = require('../config/connection');
44

55

6-
class Post extends Model {}
6+
class Post extends Model { }
77

88

99
Post.init(
10-
{
11-
id: {
12-
type: DataTypes.INTEGER,
13-
allowNull: false,
14-
primaryKey: true,
15-
autoIncrement: true
16-
},
17-
title: {
18-
type: DataTypes.STRING,
19-
allowNull: false
20-
},
21-
content: {
22-
type: DataTypes.TEXT,
23-
allowNull: false
24-
},
25-
user_id: {
26-
type: DataTypes.INTEGER,
27-
references: {
28-
model: 'user',
29-
key: 'id'
30-
}
31-
}
10+
{
11+
id: {
12+
type: DataTypes.INTEGER,
13+
allowNull: false,
14+
primaryKey: true,
15+
autoIncrement: true
16+
},
17+
title: {
18+
type: DataTypes.STRING,
19+
allowNull: false
20+
},
21+
post_content: {
22+
type: DataTypes.TEXT,
23+
allowNull: false
3224
},
33-
{
34-
sequelize,
35-
freezeTableName: true,
36-
underscored: true,
37-
modelName: 'post'
25+
user_id: {
26+
type: DataTypes.INTEGER,
27+
references: {
28+
model: 'user',
29+
key: 'id'
30+
}
3831
}
39-
);
40-
41-
module.exports = Post;
32+
},
33+
{
34+
sequelize,
35+
freezeTableName: true,
36+
underscored: true,
37+
modelName: 'post'
38+
}
39+
);
40+
41+
module.exports = Post;

public/js/add-post.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ async function newFormHandler(event) {
22
event.preventDefault();
33

44
const title = document.querySelector('input[name="post-title"]').value;
5-
const content = document.querySelector('input[name="content"]').value;
5+
const post_content = document.querySelector('input[name="post_content"]').value;
66

77
const response = await fetch(`/api/posts`, {
88
method: "POST",
99
body: JSON.stringify({
1010
title,
11-
content,
11+
post_content,
1212
}),
1313
headers: {
14-
"Content-Type": "application/json",
14+
"post_content-Type": "application/json",
1515
},
1616
});
1717

seeds/postData.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22
{
33
"id": 1,
44
"title": "Title of Post Number One",
5-
"content": "Content of post 1.",
5+
"post_content": "Content of post 1.",
66
"user_id": 1
77
},
88
{
99
"id": 2,
1010
"title": "Post Number Two",
11-
"content": "Content of post 1.",
11+
"post_content": "Content of post 1.",
1212
"user_id": 2
1313
},
1414
{
1515
"id": 3,
1616
"title": "Post Three",
17-
"content": "Content of post 3.",
17+
"post_content": "Content of post 3.",
1818
"user_id": 1
1919
},
2020
{
2121
"id": 4,
2222
"title": "Post Four",
23-
"content": "Content of post 4.",
23+
"post_content": "Content of post 4.",
2424
"user_id": 3
2525
}
2626
]

views/add-post.handlebars

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
<div class="card text-center">
2-
<h3 class="card-header">Create New Post</h3>
3-
<form class="card-body new-post-form" id="new-post-form">
4-
<div class="form-group">
5-
<input type="text" id="post-title" name="post-title" class="form-input" placeholder="Title" />
6-
</div>
7-
<div class="form-group">
8-
<input id="content" name="content" class="form-input" placeholder="Make a post..." />
9-
</div>
10-
<button type="submit" class="btn btn-sm btn-primary">Post</button>
11-
</form>
2+
<h3 class="card-header">Create New Post</h3>
3+
<form class="card-body new-post-form" id="new-post-form">
4+
<div class="form-group">
5+
<input
6+
type="text"
7+
id="post-title"
8+
name="post-title"
9+
class="form-input"
10+
placeholder="Title"
11+
/>
12+
</div>
13+
<div class="form-group">
14+
<input
15+
id="post_content"
16+
name="post_content"
17+
class="form-input"
18+
placeholder="Make a post..."
19+
/>
20+
</div>
21+
<button type="submit" class="btn btn-sm btn-primary">Post</button>
22+
</form>
1223
</div>
1324

1425
<script src="/js/add-post.js"></script>

views/edit-post.handlebars

+28-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
<div class="card">
2-
<h3 class="card-header">Edit Post</h3>
3-
<div class="row text-center">
4-
<form class="card-body edit-post-form">
2+
<h3 class="card-header">Edit Post</h3>
3+
<div class="row text-center">
4+
<form class="card-body edit-post-form">
55

6-
<div class="form-group col-11">
6+
<div class="form-group col-11">
77

8-
<input name="post-title" type="text" value="{{post.title}}" class="form-input" />
9-
</div>
10-
<div class="form-group col-11">
11-
<input name="content" type="text" value="{{post.content}}" class="form-input" />
12-
</div>
13-
<div class="form-group col-11">
14-
<button type="submit" class="btn btn-sm btn-primary">Update Post</button>
15-
<button type="button" class="delete-post-btn btn btn-sm btn-primary">Delete Post</button>
16-
</div>
17-
</form>
18-
</div>
8+
<input
9+
name="post-title"
10+
type="text"
11+
value="{{post.title}}"
12+
class="form-input"
13+
/>
14+
</div>
15+
<div class="form-group col-11">
16+
<input
17+
name="post_content"
18+
type="text"
19+
value="{{post.post_content}}"
20+
class="form-input"
21+
/>
22+
</div>
23+
<div class="form-group col-11">
24+
<button type="submit" class="btn btn-sm btn-primary">Update Post</button>
25+
<button
26+
type="button"
27+
class="delete-post-btn btn btn-sm btn-primary"
28+
>Delete Post</button>
29+
</div>
30+
</form>
31+
</div>
1932
</div>
2033

21-
22-
2334
<script src="/js/edit-post.js"></script>
2435
<script src="/js/delete-post.js"></script>

views/partials/post-info.handlebars

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<div class="card mt-3">
2-
<div class="card-header">
3-
<p class="card-text date"> {{format_date created_at}}</p>
4-
<h6 class=" text-dark card-subtitle">
5-
<i class="fas fa-user mr-2"></i>
6-
{{user.username}}
7-
</h6>
8-
<h4 class="mt-2"><a href="/post/{{id}}" class="post-link">{{title}}</a></h4>
9-
</div>
10-
<p class="card-body">{{content}}</p>
2+
<div class="card-header">
3+
<p class="card-text date"> {{format_date created_at}}</p>
4+
<h6 class="text-dark card-subtitle">
5+
<i class="fas fa-user mr-2"></i>
6+
{{user.username}}
7+
</h6>
8+
<h4 class="mt-2"><a
9+
href="/post/{{id}}"
10+
class="post-link"
11+
>{{title}}</a></h4>
12+
</div>
13+
<p class="card-body">{{post_content}}</p>
1114
</div>

0 commit comments

Comments
 (0)