Skip to content

Commit 9d9348c

Browse files
Merge pull request #51 from ARIHANTJAIN2006/pr
Like and Comment Routes
2 parents d8cf54b + 5e28a25 commit 9d9348c

File tree

9 files changed

+266
-185
lines changed

9 files changed

+266
-185
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Request, Response } from "express";
2+
import { Post } from "../models/post";
3+
import { ActionModel } from "../models/action_collection";
4+
export const handleLike = async (
5+
req: Request & { userId?: string },
6+
res: Response
7+
) => {
8+
try {
9+
const userId = req.userId;
10+
if (!userId)
11+
return res
12+
.status(401)
13+
.json({ message: "Unauthorized. Sign in first" });
14+
15+
const { postId } = req.params;
16+
if (!postId)
17+
return res.status(400).json({ message: "Post id is required" });
18+
19+
const existingLike = await ActionModel.findOne({
20+
userid: userId,
21+
targetId: postId,
22+
action_type: "like",
23+
});
24+
25+
if (existingLike) {
26+
await ActionModel.deleteOne({ _id: existingLike._id });
27+
} else {
28+
await ActionModel.create({
29+
userid: userId,
30+
targetId: postId,
31+
action_type: "like",
32+
value: null,
33+
});
34+
}
35+
36+
const likesCount = await ActionModel.countDocuments({
37+
targetId: postId,
38+
action_type: "like",
39+
});
40+
41+
const updatedPost = await Post.findByIdAndUpdate(
42+
postId,
43+
{ $set: { likesCount } },
44+
{ new: true } // 👈 IMPORTANT
45+
);
46+
47+
return res.status(200).json({
48+
message: existingLike
49+
? "Post unliked successfully"
50+
: "Post liked successfully",
51+
liked: !existingLike,
52+
post: updatedPost, // 👈 sending the post
53+
});
54+
} catch (error) {
55+
console.log(error);
56+
return res.status(500).json({ message: "Internal server error" });
57+
}
58+
};
59+
60+
61+
export const handleComment = async (req:Request & {userId?:string},res:Response) =>{
62+
try{
63+
const userId = req.userId
64+
if(!userId)
65+
return res.status(401).json({message:"Unauthorized to comment.Sign in first"})
66+
const {postId} = req.params
67+
const {comment} = req.body
68+
if(!postId || !comment){
69+
return res.status(400).json({message:"Post id and comment are required"})
70+
}
71+
const createdcomment = await ActionModel.create({
72+
userid:userId,
73+
targetId:postId,
74+
action_type:"comment",
75+
value:comment
76+
})
77+
const commentsCount = await ActionModel.countDocuments({targetId:postId,action_type:"comment"})
78+
await Post.findByIdAndUpdate(postId,{$set:{commentsCount:commentsCount}})
79+
return res.status(200).json({
80+
message:"Comment added successfully",
81+
comment:createdcomment
82+
})
83+
}catch(error){
84+
console.log(error)
85+
return res.status(500).json({message:"Internal server error",
86+
87+
})
88+
}
89+
}

backend/src/controllers/editavatarcontroller.ts

Lines changed: 0 additions & 59 deletions
This file was deleted.

backend/src/controllers/editbioController.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import cloudinary from '../config/cloudinary'
2+
import UserModel from '../models/user';
3+
import { Request, Response } from 'express';
4+
5+
import fs from 'fs'
6+
export const updateAvatar = async (req: Request & { userId?: string }, res: Response) => {
7+
try {
8+
if (!req.file) {
9+
return res.status(400).json({
10+
success: false,
11+
message: "Avatar file is required",
12+
});
13+
}
14+
15+
const userId = req.userId;
16+
if (!userId) {
17+
return res.status(401).json({
18+
success: false,
19+
message: "Unauthorized request",
20+
});
21+
}
22+
23+
const user = await UserModel.findOne({ _id:userId});
24+
if (!user) {
25+
return res.status(404).json({
26+
success: false,
27+
message: "User not found",
28+
});
29+
}
30+
const uploadResult = await cloudinary.uploader.upload(req.file.path,{
31+
folder:"avatars",
32+
resource_type:"image"
33+
})
34+
if(!uploadResult){
35+
return res.status(500).json({
36+
success: false,
37+
message: "Failed to upload avatar",
38+
});
39+
}
40+
user.avatarUrl = uploadResult.secure_url
41+
await user.save()
42+
fs.unlink(req.file.path, (err) => {
43+
if (err) console.error("Failed to delete temp file:", err);
44+
});
45+
46+
return res.status(200).json({
47+
success:true,
48+
message:"Avatar updated successfully",
49+
avatar_url:uploadResult.secure_url
50+
})
51+
52+
} catch (error: any) {
53+
console.log("Error in editavatar route:", error);
54+
return res.status(500).json({
55+
success: false,
56+
message: "Internal server error",
57+
});
58+
}
59+
}
60+
61+
export const updateBio = async (req: Request & { userId?: string }, res: Response) => {
62+
try {
63+
if (!req.file) {
64+
return res.status(400).json({
65+
success: false,
66+
message: "Avatar file is required",
67+
});
68+
}
69+
70+
const userId = req.userId;
71+
if (!userId) {
72+
return res.status(401).json({
73+
success: false,
74+
message: "Unauthorized request",
75+
});
76+
}
77+
78+
const user = await UserModel.findOne({ _id:userId});
79+
if (!user) {
80+
return res.status(404).json({
81+
success: false,
82+
message: "User not found",
83+
});
84+
}
85+
const newbio = req.body.Bio
86+
user.bio = newbio
87+
await user.save()
88+
89+
return res.status(200).json({
90+
success:true,
91+
message:"Bio updated successfully",
92+
})
93+
94+
} catch (error: any) {
95+
console.log("Error in editbio route:", error);
96+
return res.status(500).json({
97+
success: false,
98+
message: "Internal server error",
99+
});
100+
}
101+
}
102+
103+
export const updateUsername = async (req: Request & { userId?: string }, res: Response) => {
104+
try {
105+
const userId = req.userId;
106+
if (!userId) {
107+
return res.status(401).json({
108+
success: false,
109+
message: "Unauthorized request",
110+
});
111+
}
112+
113+
const { username } = req.body;
114+
115+
if (!username || username.trim().length < 3) {
116+
return res.status(400).json({
117+
success: false,
118+
message: "Username must be at least 3 characters long",
119+
});
120+
}
121+
122+
// check if username already exists
123+
const existingUser = await UserModel.findOne({ username });
124+
if (existingUser) {
125+
return res.status(409).json({
126+
success: false,
127+
message: "Username already taken",
128+
});
129+
}
130+
131+
const user = await UserModel.findById(userId);
132+
if (!user) {
133+
return res.status(404).json({
134+
success: false,
135+
message: "User not found",
136+
});
137+
}
138+
139+
user.username = username;
140+
await user.save();
141+
142+
return res.status(200).json({
143+
success: true,
144+
message: "Username updated successfully",
145+
});
146+
147+
} catch (error) {
148+
console.error("Error in editUsername:", error);
149+
return res.status(500).json({
150+
success: false,
151+
message: "Internal server error",
152+
});
153+
}
154+
};

0 commit comments

Comments
 (0)