Skip to content

Commit 09c0213

Browse files
committed
chore: functionality for user related posts and hobbies are deleted when user is deleted
1 parent 5511038 commit 09c0213

File tree

2 files changed

+98
-11
lines changed

2 files changed

+98
-11
lines changed

client/lib/views/users_page.dart

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class _UsersPageState extends State<UsersPage> {
4141
List hobbiesIDsToDelete = [];
4242
List postsIDsToDelete = [];
4343

44+
bool _isRemoveHobbies = false;
45+
bool _isRemovePosts = false;
46+
4447
@override
4548
Widget build(BuildContext context) {
4649
return Query(
@@ -133,21 +136,57 @@ class _UsersPageState extends State<UsersPage> {
133136
for (var i = 0; i < user["posts"].length; i++){
134137
postsIDsToDelete.add(user["hobbies"][i]["id"]);
135138
}
136-
debugPrint("+++${user["name"]} Hobbies to delete ${hobbiesIDsToDelete.toString()}");
137-
debugPrint("+++${user["name"]} Posts to delete ${postsIDsToDelete.toString()}");
139+
// debugPrint("+++${user["name"]} Hobbies to delete ${hobbiesIDsToDelete.toString()}");
140+
// debugPrint("+++${user["name"]} Posts to delete ${postsIDsToDelete.toString()}");
141+
142+
setState((){
143+
_isRemoveHobbies = true;
144+
_isRemovePosts = true;
145+
});
138146

139-
// runMutation({"id": user["id"]});
140-
// Navigator.pushAndRemoveUntil(
141-
// context,
142-
// MaterialPageRoute(builder: (context){
143-
// return const HomeScreen();
144-
// },
145-
// ), (route) => false,);
147+
runMutation({"id": user["id"]});
148+
Navigator.pushAndRemoveUntil(
149+
context,
150+
MaterialPageRoute(builder: (context){
151+
return const HomeScreen();
152+
},
153+
), (route) => false,);
146154
},
147-
);
148-
}
155+
);
156+
},
149157
),
150158
),
159+
_isRemoveHobbies
160+
? Mutation(
161+
options: MutationOptions(
162+
document: gql(removeHobbies()),
163+
onCompleted: (data) {},
164+
),
165+
builder: (runMutation, result) {
166+
if(hobbiesIDsToDelete.isNotEmpty){
167+
debugPrint("Calling deleteHobbies");
168+
runMutation({
169+
'ids': hobbiesIDsToDelete
170+
});
171+
}
172+
return Container();
173+
},
174+
) : Container(),
175+
_isRemovePosts
176+
? Mutation(
177+
options: MutationOptions(
178+
document: gql(removePosts()),
179+
onCompleted: (data){},
180+
),
181+
builder: (runMutation, result){
182+
if(postsIDsToDelete.isNotEmpty){
183+
runMutation({
184+
"ids": postsIDsToDelete
185+
});
186+
}
187+
return Container();
188+
},
189+
) : Container(),
151190
],
152191
)
153192
],
@@ -197,4 +236,24 @@ class _UsersPageState extends State<UsersPage> {
197236
}
198237
""";
199238
}
239+
240+
String removeHobbies() {
241+
return """
242+
mutation RemoveHobbies(\$ids: [String]){
243+
RemoveHobbies(ids: \$ids){
244+
245+
}
246+
}
247+
""";
248+
}
249+
250+
String removePosts() {
251+
return """
252+
mutation RemovePosts(\$ids: [String]){
253+
RemovePosts(ids: \$ids){
254+
255+
}
256+
}
257+
""";
258+
}
200259
}

server/schema/schema.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,20 @@ const Mutation = new GraphQLObjectType({
252252
}
253253
},
254254

255+
RemovePosts:{
256+
type: PostType,
257+
args: {
258+
ids: {type: GraphQLList(GraphQLString)},
259+
},
260+
resolve(parent, args){
261+
let removedPosts = Post.deleteMany({_id: args.ids}).exec();
262+
if(!removedPosts){
263+
throw new "Error" ()
264+
}
265+
return removedPosts
266+
}
267+
},
268+
255269
createHobby: {
256270
type: HobbyType,
257271
args: {
@@ -305,6 +319,20 @@ const Mutation = new GraphQLObjectType({
305319
}
306320
},
307321

322+
RemoveHobbies:{
323+
type: HobbyType,
324+
args: {
325+
ids: {type: GraphQLList(GraphQLString)},
326+
},
327+
resolve(parent, args){
328+
let removedHobbies = Hobby.deleteMany({_id: args.ids}).exec();
329+
if(!removedHobbies){
330+
throw new "Error" ()
331+
}
332+
return removedHobbies
333+
}
334+
},
335+
308336
}
309337
});
310338

0 commit comments

Comments
 (0)