Skip to content

Commit

Permalink
fix(server): fixes posting without picture
Browse files Browse the repository at this point in the history
fixing #103
  • Loading branch information
roman-ojha committed Jun 9, 2022
1 parent 70b508f commit caaf1a6
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const PostButton = () => {
console.log(resData);
if (res.status === 200 && resData.success) {
toastSuccess(resData.msg);
dispatch(userPostResponseData(resData.data));
dispatch(userPostResponseData({ ...resData.data, date: new Date() }));
dispatch(
homePageUserPostFieldDataAction({
...homePageUserPostFieldData,
Expand Down
8 changes: 5 additions & 3 deletions client/src/components/PostBox/LikeCommentShare.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ const LikeCommentShare = (props) => {
const dispatch = useDispatch();
const [likeInfo, setLikeInfo] = useState({
likeNo: props.userFeedData.likes.No,
isLikedPost: props.userFeedData.likes.by.some(
(el) => el.user === userProfileDetailStore.id
),
isLikedPost: props.userFeedData.likes.by
? props.userFeedData.likes.by.some(
(el) => el.user === userProfileDetailStore.id
)
: [],
});

const like = async () => {
Expand Down
11 changes: 7 additions & 4 deletions client/src/components/PostBox/PostBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ const PostBox = (props) => {

const [commentInfo, setCommentInfo] = useState({
commentNo: props.userFeedData.comments.No,
postCommentInfo:
props.userFeedData.comments.by[
Math.floor(Math.random() * props.userFeedData.comments.by.length)
],
postCommentInfo: props.userFeedData.comments.by
? props.userFeedData.comments.by[
Math.floor(Math.random() * props.userFeedData.comments.by.length)
]
: [],
});

console.log(props);

return (
<>
<article className="HomePage_Feed_Content_Container">
Expand Down
41 changes: 17 additions & 24 deletions controllers/storage.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ export default {
caption: caption,
likes: {
No: 0,
by: [],
},
comments: {
No: 0,
by: [],
},
};
const postSuccessFull = await rootUser.uploadPost(
Expand All @@ -42,17 +44,8 @@ export default {
);
if (postSuccessFull) {
const resData = {
useremail: rootUser.email,
username: rootUser.name,
userID: rootUser.userID,
profilePicture: rootUser.picture,
picture: "",
// id: postRes[0].id,
// caption: postRes[0].caption,
// likes: postRes[0].likes,
// comments: postRes[0].comments,
...userPostDetail,
date: new Date(),
picture: undefined,
};
return res.status(200).json(<ResponseObject>{
success: true,
Expand Down Expand Up @@ -123,23 +116,23 @@ export default {
userStoryDetail
);
if (postSuccessFull) {
const resData = {
useremail: rootUser.email,
username: rootUser.name,
userID: rootUser.userID,
profilePicture: rootUser.picture,
// id: postRes[0].id,
// caption: postRes[0].caption,
// picture: postRes[0].picture,
// likes: postRes[0].likes,
// comments: postRes[0].comments,
...userPostDetail,
date: new Date(),
};
// const resData = {
// useremail: rootUser.email,
// username: rootUser.name,
// userID: rootUser.userID,
// profilePicture: rootUser.picture,
// // id: postRes[0].id,
// // caption: postRes[0].caption,
// // picture: postRes[0].picture,
// // likes: postRes[0].likes,
// // comments: postRes[0].comments,
// ...userPostDetail,
// date: new Date(),
// };
return res.status(200).json(<ResponseObject>{
success: true,
msg: "Post upload successfully",
data: resData,
data: userPostDetail,
});
}

Expand Down
Binary file added db/Images/6ff8dd8726b1e1c08ad948a664a67693.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added db/Images/ffaa1829b324c8b9e577aec12bf519bf.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions interface/userDocument.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Document } from "mongoose";
import { Interface } from "readline";

export type UserDocumentMessages = {
lastMessageOn: Date;
Expand Down Expand Up @@ -37,6 +38,28 @@ export type UserDocumentPosts = {
date: Date;
};

export interface RootUserNewPosts {
id: string;
caption: string;
picture:
| {
name: string;
path: string;
url: string;
firebaseStorageDownloadToken: string;
bucket: string;
}
| undefined;
likes: {
No: number;
by: [];
};
comments: {
No: number;
by: [];
};
}

export type UserDocumentBirthday = {
year: string;
month: string;
Expand Down
51 changes: 46 additions & 5 deletions models/userDetail_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";
import SchemaMethodInstance from "interface/userSchemaMethods.js";
import ModelMethodInstance from "interface/userModelMethods.js";
import { UpdateResult } from "mongodb";

const userDetailSchema = new mongoose.Schema<
SchemaMethodInstance,
Expand Down Expand Up @@ -251,13 +252,53 @@ userDetailSchema.methods.uploadPost = async function (
userStoryDetail: object
) {
try {
this.posts.push(postData);
// this.posts.push(postData);
// if (userStoryDetail !== undefined) {
// this.stories = userStoryDetail;
// }
// this.postNo++;
// await this.save();
console.log(postData);
let resPost: UpdateResult;
if (userStoryDetail !== undefined) {
this.stories = userStoryDetail;
resPost = await UserDetail.updateOne(
{
id: this.id,
},
{
$push: {
posts: postData,
},
$inc: {
postNo: 1,
},

$set: {
stories: userStoryDetail,
},
}
);
} else {
console.log("Without Stories");
resPost = await UserDetail.updateOne(
{
id: this.id,
},
{
$push: {
posts: postData,
},
$inc: {
postNo: 1,
},
}
);
}
if (resPost) {
return true;
} else {
return false;
}
this.postNo++;
await this.save();
return true;
} catch (err) {
return false;
}
Expand Down

0 comments on commit caaf1a6

Please sign in to comment.