Skip to content

Commit

Permalink
Merge pull request #8 from alexHoggett/bugfix/post-feed-integration
Browse files Browse the repository at this point in the history
fixed issues with adding posts
  • Loading branch information
alexHoggett authored Feb 13, 2023
2 parents 33e3f03 + 89d6b92 commit 867250b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
8 changes: 7 additions & 1 deletion api/controllers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ const PostsController = {
});
},
Create: (req, res) => {
const post = new Post(req.body);
console.log(req.user_id);
const post = new Post({
content: req.body.content,
likes: 0,
user_id: req.user_id,
date_created: new Date()
});
post.save(async (err) => {
if (err) {
throw err;
Expand Down
3 changes: 2 additions & 1 deletion api/models/post.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { ObjectId } = require("mongodb");
const mongoose = require("mongoose");

const PostSchema = new mongoose.Schema({
content: { type: String, required: true },
date_created: { type: Date, required: true },
user_id: { type: Number, required: true },
user_id: { type: ObjectId, required: true },
likes: { type: Number, required: true }
});

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/post/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Post = ({post}) => {
<div>{console.log("post was deleted :)")}</div>
) : (
<div className="post">
<article data-cy="post" key={ post._id }>{ post.message }</article>
<article data-cy="post" key={ post._id }>{ post.content }</article>
<DeleteButtonPost post={post} id={post._id} setIsDeleted={setIsDeleted} />
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/post/PostForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const PostForm = ({post}) => {
const[newPost, setNewPost] = useState("")

const handleSubmit = async (event) => {
// event.preventDefault();
event.preventDefault();
const token = window.localStorage.getItem("token") //every event that will be handled by LOGGED IN user has to have this bit, its about JWT
let response = await fetch( '/posts', {
method: 'post',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}` // This is the token header
},
body: JSON.stringify({ message: newPost })
body: JSON.stringify({ content: newPost })
})

if(response.status !== 201) {
Expand Down

0 comments on commit 867250b

Please sign in to comment.