From c276e7ae1ed06608cfdcaae848ebf54eee681102 Mon Sep 17 00:00:00 2001 From: Emre Date: Wed, 2 Aug 2023 16:01:59 +0200 Subject: [PATCH] Add Comment and get Comments done :white_check_mark: --- Sources/justMUSIC/lib/model/Comment.dart | 12 ++++--- .../lib/model/mapper/CommentMapper.dart | 17 ++++++++++ .../lib/services/CommentService.dart | 27 +++++++++++++++ .../lib/view_model/CommentViewModel.dart | 33 +++++++++++++++++-- .../lib/view_model/MusicViewModel.dart | 6 ++-- .../lib/view_model/PostViewModel.dart | 4 +-- 6 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 Sources/justMUSIC/lib/model/mapper/CommentMapper.dart create mode 100644 Sources/justMUSIC/lib/services/CommentService.dart diff --git a/Sources/justMUSIC/lib/model/Comment.dart b/Sources/justMUSIC/lib/model/Comment.dart index 4c8d269..b95c57b 100644 --- a/Sources/justMUSIC/lib/model/Comment.dart +++ b/Sources/justMUSIC/lib/model/Comment.dart @@ -1,14 +1,18 @@ +import 'User.dart'; + class Comment { - final int _id; + final String _id; + User _user; String _text; DateTime _date; - List comments = []; // Constructor - Comment(this._id, this._text, this._date); + Comment(this._id, this._user, this._text, this._date); // Getters and setters - int get id => _id; + String get id => _id; + + User get user => _user; String get text => _text; diff --git a/Sources/justMUSIC/lib/model/mapper/CommentMapper.dart b/Sources/justMUSIC/lib/model/mapper/CommentMapper.dart new file mode 100644 index 0000000..cbd52e6 --- /dev/null +++ b/Sources/justMUSIC/lib/model/mapper/CommentMapper.dart @@ -0,0 +1,17 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; + +import '../../main.dart'; +import '../Comment.dart'; +import '../User.dart'; + +class CommentMapper { + static Future toModel(DocumentSnapshot> snapshot) async { + final data = snapshot.data(); + User? user = await MyApp.userViewModel.getUser(data?['user_id']); + return Comment( + snapshot.id, + user!, + data?["text"], + data?["date"]); + } +} diff --git a/Sources/justMUSIC/lib/services/CommentService.dart b/Sources/justMUSIC/lib/services/CommentService.dart new file mode 100644 index 0000000..633ab78 --- /dev/null +++ b/Sources/justMUSIC/lib/services/CommentService.dart @@ -0,0 +1,27 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; + +import '../main.dart'; + +class CommentService { + createComment(String text, String idPost) async { + var id = MyApp.userViewModel.userCurrent.id; + final comment = { + "user_id": id, + "text": text, + "date": DateTime.now(), + "post_id": idPost + }; + + await MyApp.db.collection("comments").add(comment); + } + + Future>>> getCommentsByPostId( + String id) async { + var response = await FirebaseFirestore.instance + .collection("comments") + .where("post_id", isEqualTo: id) + .get(); + + return response.docs; + } +} diff --git a/Sources/justMUSIC/lib/view_model/CommentViewModel.dart b/Sources/justMUSIC/lib/view_model/CommentViewModel.dart index f4684e1..7c2be7f 100644 --- a/Sources/justMUSIC/lib/view_model/CommentViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/CommentViewModel.dart @@ -1,8 +1,37 @@ +import 'package:justmusic/model/mapper/CommentMapper.dart'; + import '../model/Comment.dart'; +import '../services/CommentService.dart'; class CommentViewModel { + List _comments = []; + final CommentService _commentService = CommentService(); + + // Constructor + CommentViewModel(); + // Methods - List getCommentsPost(int idPost) { - throw new Error(); + addComment(String text, String idPost) async { + try { + await _commentService.createComment(text,idPost); + } catch(e) { + print(e); + rethrow; + } + } + + Future> getCommentsByPostId(String id) async { + try { + var responseData = await _commentService.getCommentsByPostId(id); + var commentsFutures = responseData.map((value) async { + return await CommentMapper.toModel(value); + }).toList(); + _comments = await Future.wait(commentsFutures); + return _comments; + } catch(e) { + print(e); + _comments = []; + return []; + } } } diff --git a/Sources/justMUSIC/lib/view_model/MusicViewModel.dart b/Sources/justMUSIC/lib/view_model/MusicViewModel.dart index 981b08a..c5afd40 100644 --- a/Sources/justMUSIC/lib/view_model/MusicViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/MusicViewModel.dart @@ -224,10 +224,12 @@ class MusicViewModel { List musics = []; Artist artist = await getArtistWithName(name, market: market); musics.addAll(await getTopMusicsWithArtistId(artist.id)); - musics.addAll(await getMusicsWithName(name,limit: limit,offset: offset,market: market)); + musics.addAll(await getMusicsWithName(name, + limit: limit, offset: offset, market: market)); return musics; } catch (e) { - return await getMusicsWithName(name,limit: limit,offset: offset,market: market); + return await getMusicsWithName(name, + limit: limit, offset: offset, market: market); } } } diff --git a/Sources/justMUSIC/lib/view_model/PostViewModel.dart b/Sources/justMUSIC/lib/view_model/PostViewModel.dart index d60a4ce..a89abb2 100644 --- a/Sources/justMUSIC/lib/view_model/PostViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/PostViewModel.dart @@ -58,9 +58,9 @@ class PostViewModel { try { var responseData = await _postService.getPopularPosts(); List ids = []; - var postsFutures = responseData.map((value) { + var postsFutures = responseData.map((value) async { ids.add(value.data()["song_id"]); - return PostMapper.toModel(value); + return await PostMapper.toModel(value); }).toList(); var posts = await Future.wait(postsFutures); List musics = await MyApp.musicViewModel.getMusicsWithIds(ids);