@@ -16,6 +16,7 @@ const UserController = {
1616 verified : false ,
1717 password : hashPassword ,
1818 createdAt : Date . now ( ) ,
19+ friendList : [ ] ,
1920 } ) ;
2021 await newUser . save ( ) ;
2122
@@ -39,6 +40,88 @@ const UserController = {
3940
4041 return res . status ( 200 ) . json ( { message : "User confirmed" } ) ;
4142 } ,
43+
44+ async addPending ( req , res ) {
45+ const userId = req . userId ;
46+ const newPendingId = req . body . friendId ;
47+
48+ //* add friendId to pending status
49+ await User . findOneAndUpdate (
50+ { _id : userId } ,
51+ { $push : { pending : newPendingId } }
52+ ) ;
53+
54+ //* add userId to request status in friendUser
55+ await User . findOneAndUpdate (
56+ { _id : newPendingId } ,
57+ { $push : { requests : userId } }
58+ ) ;
59+
60+ return res . status ( 200 ) . json ( { message : "Pending added" } ) ;
61+ } ,
62+
63+ async removePending ( req , res ) {
64+ const userId = req . userId ;
65+ const removePendingId = req . body . friendId ;
66+
67+ //* remove friendId from pending status
68+ await User . findOneAndUpdate (
69+ { _id : userId } ,
70+ { $pull : { pending : removePendingId } }
71+ ) ;
72+
73+ //* remove userId from request status in friendUser
74+ await User . findOneAndUpdate (
75+ { _id : removePendingId } ,
76+ { $pull : { requests : userId } }
77+ ) ;
78+
79+ return res . status ( 200 ) . json ( { message : "Pending removed" } ) ;
80+ } ,
81+
82+ async addFriend ( req , res ) {
83+ const userId = req . userId ;
84+ const newFriendId = req . body . friendId ;
85+
86+ //* add newFriend to user's friends
87+ await User . findOneAndUpdate (
88+ { _id : userId } ,
89+ {
90+ $push : { friendList : newFriendId } ,
91+ $pull : { requests : newFriendId } ,
92+ }
93+ ) ;
94+
95+ //* add user to newFriend's friends
96+ await User . findOneAndUpdate (
97+ { _id : newFriendId } ,
98+ {
99+ $push : { friendList : userId } ,
100+ $pull : { pending : userId } ,
101+ }
102+ ) ;
103+
104+ return res . status ( 200 ) . json ( { message : "Friend added" } ) ;
105+ } ,
106+
107+ async removeFriend ( req , res ) {
108+ const userId = req . userId ;
109+ const removeFriendId = req . body . friendId ;
110+
111+ //* remove friend from user's friends
112+ await User . findOneAndUpdate (
113+ { _id : userId } ,
114+ { $pull : { friendList : removeFriendId } }
115+ ) ;
116+
117+ //* remove user from friends's friends
118+ await User . findOneAndUpdate (
119+ { _id : removeFriendId } ,
120+ { $pull : { friendList : userId } }
121+ ) ;
122+
123+ return res . status ( 200 ) . json ( { message : "Friend removed" } ) ;
124+ } ,
42125} ;
43126
44127module . exports = UserController ;
0 commit comments