-
-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GSoC - Adding / Removing || Adding comment (#903)
* fixed generated plugin error * Added like functionality * Added Removing like feature * Added comment feature without single source truth * Added comment features and Updated test. Co-authored-by: rutvik11062000 <[email protected]>
- Loading branch information
1 parent
760ebc5
commit b111996
Showing
12 changed files
with
365 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'package:talawa/locator.dart'; | ||
import 'package:talawa/services/database_mutation_functions.dart'; | ||
import 'package:talawa/utils/comment_queries.dart'; | ||
|
||
class CommentService { | ||
CommentService() { | ||
_dbFunctions = locator<DataBaseMutationFunctions>(); | ||
} | ||
late DataBaseMutationFunctions _dbFunctions; | ||
|
||
Future<void> createComments(String postId, String text) async { | ||
print("comment service called"); | ||
final String _createCommentQuery = CommentQueries().createComment(); | ||
final result = | ||
await _dbFunctions.gqlAuthMutation(_createCommentQuery, variables: { | ||
'postId': postId, //Add your variables here | ||
'text': text | ||
}); | ||
print("comment added"); | ||
print(result); | ||
return result; | ||
} | ||
|
||
Future getCommentsForPost(String postId) async { | ||
final String _getCommmentQuery = CommentQueries().getPostsComments(postId); | ||
final result = await _dbFunctions.gqlAuthMutation(_getCommmentQuery); | ||
if (result.data != null) { | ||
return result.data["commentsByPost"] as List; | ||
} | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class CommentQueries { | ||
String createComment() { | ||
return """ | ||
mutation createComment(\$postId: ID!, \$text: String!) { | ||
createComment(postId: \$postId, | ||
data:{ | ||
text: \$text, | ||
} | ||
){ | ||
_id | ||
} | ||
} | ||
"""; | ||
} | ||
|
||
String getPostsComments(String postId) { | ||
return """ | ||
query{ | ||
commentsByPost(id: "$postId"){ | ||
_id | ||
text | ||
createdAt | ||
creator{ | ||
firstName | ||
lastName | ||
} | ||
} | ||
} | ||
"""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
lib/view_model/widgets_view_models/comments_view_model.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'package:talawa/enums/enums.dart'; | ||
import 'package:talawa/locator.dart'; | ||
import 'package:talawa/models/comment/comment_model.dart'; | ||
import 'package:talawa/services/comment_service.dart'; | ||
import 'package:talawa/services/post_service.dart'; | ||
import 'package:talawa/services/user_config.dart'; | ||
import 'package:talawa/view_model/base_view_model.dart'; | ||
|
||
class CommentsViewModel extends BaseModel { | ||
late CommentService _commentService; | ||
late PostService _postService; | ||
late String _postID; | ||
late List<Comment> _commentlist; | ||
late UserConfig _userConfig; | ||
|
||
// Getters | ||
List<Comment> get commentList => _commentlist; | ||
|
||
void initialise(String postID) { | ||
_commentlist = []; | ||
_postID = postID; | ||
_commentService = locator<CommentService>(); | ||
_userConfig = locator<UserConfig>(); | ||
_postService = locator<PostService>(); | ||
notifyListeners(); | ||
getComments(); | ||
} | ||
|
||
Future getComments() async { | ||
setState(ViewState.busy); | ||
final List _commentsJSON = | ||
await _commentService.getCommentsForPost(_postID) as List; | ||
print(_commentsJSON); | ||
_commentsJSON.forEach((commentJson) { | ||
_commentlist.add(Comment.fromJson(commentJson as Map<String, dynamic>)); | ||
}); | ||
setState(ViewState.idle); | ||
} | ||
|
||
Future createComment(String msg) async { | ||
print("comment viewModel called"); | ||
await _commentService.createComments(_postID, msg); | ||
addCommentLocally(msg); | ||
} | ||
|
||
void addCommentLocally(String msg) { | ||
_postService.addCommentLocally(_postID); | ||
final _creator = _userConfig.currentUser; | ||
final Comment _localComment = Comment( | ||
text: msg, createdAt: DateTime.now().toString(), creator: _creator); | ||
_commentlist.insert(0, _localComment); | ||
notifyListeners(); | ||
} | ||
} |
Oops, something went wrong.