Add likes functions
continuous-integration/drone/push Build is passing Details

LIKES_POST_LDE-EKA
Emre KARTAL 2 years ago
parent 6d6da9f2e1
commit ee88c97446

@ -128,4 +128,34 @@ class PostService {
return recapList; return recapList;
} }
Future<List<String>> getLikesByPostId(String id) async {
var response = await FirebaseFirestore.instance.collection("posts").doc(id).get();
if (response.exists) {
var musicFavorite = response.get("likes");
return List.from(musicFavorite);
} else {
return [];
}
}
Future<bool> addOrDeleteFavoritePost(String id) async {
final idUser = MyApp.userViewModel.userCurrent.id;
var postRef = await FirebaseFirestore.instance
.collection("posts")
.doc(id);
var response = await postRef.get();
List<String> likes = List.from(response.get("likes"));
if (!likes.contains(idUser)) {
likes.add(idUser);
await postRef.update({"likes": likes});
return false;
} else {
likes.remove(idUser);
await postRef.update({"likes": likes});
return true;
}
}
} }

@ -98,4 +98,22 @@ class PostViewModel {
rethrow; rethrow;
} }
} }
Future<List<String>> getLikesByPostId(String id) async {
try {
return await _postService.getLikesByPostId(id);
} catch (e) {
print(e);
rethrow;
}
}
Future<bool> addOrDeleteFavoritePost(String id) async {
try {
return await _postService.addOrDeleteFavoritePost(id);
} catch (e) {
print(e);
rethrow;
}
}
} }

Loading…
Cancel
Save