From 923fc02d49e847ce44e8ba089e815a7137800183 Mon Sep 17 00:00:00 2001 From: Emre Date: Fri, 15 Sep 2023 18:27:34 +0200 Subject: [PATCH] Update getPosts :hammer: --- .../justMUSIC/lib/services/PostService.dart | 60 +++++++++++-------- Sources/justMUSIC/pubspec.lock | 30 ++++------ 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/Sources/justMUSIC/lib/services/PostService.dart b/Sources/justMUSIC/lib/services/PostService.dart index 82bd2d0..d9a9337 100644 --- a/Sources/justMUSIC/lib/services/PostService.dart +++ b/Sources/justMUSIC/lib/services/PostService.dart @@ -7,7 +7,8 @@ import 'package:firebase_storage/firebase_storage.dart'; import '../main.dart'; class PostService { - createPost(String? description, String idMusic, File? image, Tuple2? location) async { + createPost(String? description, String idMusic, File? image, + Tuple2? location) async { var id = MyApp.userViewModel.userCurrent.id; final post = { "user_id": id, @@ -41,13 +42,13 @@ class PostService { deletePost() {} - Future>>> getPopularPosts({int limit = 10, int offset = 0}) async { - DateTime twentyFourHoursAgo = DateTime.now().subtract(Duration(hours: 24)); - Timestamp twentyFourHoursAgoTimestamp = Timestamp.fromDate(twentyFourHoursAgo); - - QuerySnapshot> response = await FirebaseFirestore.instance + Future>>> getPopularPosts( + {int limit = 10, + QueryDocumentSnapshot>? offset}) async { + QuerySnapshot> response; + response = await FirebaseFirestore.instance .collection("posts") - .where("date", isGreaterThan: twentyFourHoursAgoTimestamp) + .orderBy("date", descending: true) .limit(limit) .get(); @@ -58,18 +59,15 @@ class PostService { return filteredPosts; } - Timestamp _getTwentyFourHoursAgoTimestamp() { - DateTime twentyFourHoursAgo = DateTime.now().subtract(Duration(hours: 24)); - return Timestamp.fromDate(twentyFourHoursAgo); - } - - Future>>> getPostsFriends({int limit = 10, int offset = 0}) async { - var timestamp = _getTwentyFourHoursAgoTimestamp(); + Future>>> getPostsFriends( + {int limit = 10, int offset = 0}) async { var response = await FirebaseFirestore.instance .collection("posts") - .where("user_id", whereIn: MyApp.userViewModel.userCurrent.followed) - .where("date", isGreaterThan: timestamp) - .orderBy("date") + .where("user_id", whereIn: [ + MyApp.userViewModel.userCurrent.id, + ...MyApp.userViewModel.userCurrent.followed + ]) + .orderBy("date", descending: true) .limit(limit) .get(); @@ -79,12 +77,17 @@ class PostService { Future getAvailable(String idUser) async { DateTime today = DateTime.now(); - QuerySnapshot> response = - await FirebaseFirestore.instance.collection("posts").where("user_id", isEqualTo: idUser).get(); + QuerySnapshot> response = await FirebaseFirestore + .instance + .collection("posts") + .where("user_id", isEqualTo: idUser) + .get(); bool isTodayAvailable = response.docs.any((doc) { - DateTime date = doc["date"].toDate(); // Assuming the field name is "date" - return date.day == today.day && date.month == today.month && date.year == today.year; + DateTime date = doc["date"].toDate(); + return date.day == today.day && + date.month == today.month && + date.year == today.year; }); return !isTodayAvailable; @@ -95,11 +98,15 @@ class PostService { DateTime sevenDaysAgo = DateTime.now().subtract(Duration(days: 6)); - QuerySnapshot> response = - await FirebaseFirestore.instance.collection("posts").where("user_id", isEqualTo: id).get(); + QuerySnapshot> response = await FirebaseFirestore + .instance + .collection("posts") + .where("user_id", isEqualTo: id) + .get(); - List?> postList = - response.docs.map((DocumentSnapshot> doc) => doc.data()).toList(); + List?> postList = response.docs + .map((DocumentSnapshot> doc) => doc.data()) + .toList(); for (int i = 0; i < 7; i++) { DateTime date = sevenDaysAgo.add(Duration(days: i)); @@ -116,7 +123,8 @@ class PostService { } Future> getLikesByPostId(String id) async { - var response = await FirebaseFirestore.instance.collection("posts").doc(id).get(); + var response = + await FirebaseFirestore.instance.collection("posts").doc(id).get(); if (response.exists) { var musicFavorite = response.get("likes"); return List.from(musicFavorite); diff --git a/Sources/justMUSIC/pubspec.lock b/Sources/justMUSIC/pubspec.lock index db118c9..89568be 100644 --- a/Sources/justMUSIC/pubspec.lock +++ b/Sources/justMUSIC/pubspec.lock @@ -205,10 +205,10 @@ packages: dependency: transitive description: name: collection - sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687 + sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c" url: "https://pub.dev" source: hosted - version: "1.17.2" + version: "1.17.1" convert: dependency: transitive description: @@ -804,18 +804,18 @@ packages: dependency: transitive description: name: matcher - sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb" url: "https://pub.dev" source: hosted - version: "0.12.16" + version: "0.12.15" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724 url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.2.0" meta: dependency: transitive description: @@ -993,10 +993,10 @@ packages: dependency: transitive description: name: source_span - sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" + sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250 url: "https://pub.dev" source: hosted - version: "1.10.0" + version: "1.9.1" sqflite: dependency: transitive description: @@ -1057,10 +1057,10 @@ packages: dependency: transitive description: name: test_api - sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" + sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb url: "https://pub.dev" source: hosted - version: "0.6.0" + version: "0.5.1" text_scroll: dependency: "direct main" description: @@ -1141,14 +1141,6 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" - web: - dependency: transitive - description: - name: web - sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10 - url: "https://pub.dev" - source: hosted - version: "0.1.4-beta" win32: dependency: transitive description: @@ -1182,5 +1174,5 @@ packages: source: hosted version: "1.1.0" sdks: - dart: ">=3.1.0-185.0.dev <4.0.0" + dart: ">=3.0.0 <4.0.0" flutter: ">=3.10.0"