Add Comment and get Comments done
continuous-integration/drone/push Build is passing Details

MANAGE_COMMENTS_LDE
Emre KARTAL 2 years ago
parent 0eb850e830
commit c276e7ae1e

@ -1,14 +1,18 @@
import 'User.dart';
class Comment {
final int _id;
final String _id;
User _user;
String _text;
DateTime _date;
List<Comment> 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;

@ -0,0 +1,17 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import '../../main.dart';
import '../Comment.dart';
import '../User.dart';
class CommentMapper {
static Future<Comment> toModel(DocumentSnapshot<Map<String, dynamic>> snapshot) async {
final data = snapshot.data();
User? user = await MyApp.userViewModel.getUser(data?['user_id']);
return Comment(
snapshot.id,
user!,
data?["text"],
data?["date"]);
}
}

@ -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 = <String, dynamic>{
"user_id": id,
"text": text,
"date": DateTime.now(),
"post_id": idPost
};
await MyApp.db.collection("comments").add(comment);
}
Future<List<QueryDocumentSnapshot<Map<String, dynamic>>>> getCommentsByPostId(
String id) async {
var response = await FirebaseFirestore.instance
.collection("comments")
.where("post_id", isEqualTo: id)
.get();
return response.docs;
}
}

@ -1,8 +1,37 @@
import 'package:justmusic/model/mapper/CommentMapper.dart';
import '../model/Comment.dart';
import '../services/CommentService.dart';
class CommentViewModel {
List<Comment> _comments = [];
final CommentService _commentService = CommentService();
// Constructor
CommentViewModel();
// Methods
List<Comment> getCommentsPost(int idPost) {
throw new Error();
addComment(String text, String idPost) async {
try {
await _commentService.createComment(text,idPost);
} catch(e) {
print(e);
rethrow;
}
}
Future<List<Comment>> 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 [];
}
}
}

@ -224,10 +224,12 @@ class MusicViewModel {
List<Music> 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);
}
}
}

@ -58,9 +58,9 @@ class PostViewModel {
try {
var responseData = await _postService.getPopularPosts();
List<String> 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<Music> musics = await MyApp.musicViewModel.getMusicsWithIds(ids);

Loading…
Cancel
Save