Update getPosts 🔨
continuous-integration/drone/push Build is passing Details

pull/59/head
Emre KARTAL 2 years ago
parent 45b0f68323
commit 923fc02d49

@ -7,7 +7,8 @@ import 'package:firebase_storage/firebase_storage.dart';
import '../main.dart'; import '../main.dart';
class PostService { class PostService {
createPost(String? description, String idMusic, File? image, Tuple2<String, String>? location) async { createPost(String? description, String idMusic, File? image,
Tuple2<String, String>? location) async {
var id = MyApp.userViewModel.userCurrent.id; var id = MyApp.userViewModel.userCurrent.id;
final post = <String, dynamic>{ final post = <String, dynamic>{
"user_id": id, "user_id": id,
@ -41,13 +42,13 @@ class PostService {
deletePost() {} deletePost() {}
Future<List<QueryDocumentSnapshot<Map<String, dynamic>>>> getPopularPosts({int limit = 10, int offset = 0}) async { Future<List<QueryDocumentSnapshot<Map<String, dynamic>>>> getPopularPosts(
DateTime twentyFourHoursAgo = DateTime.now().subtract(Duration(hours: 24)); {int limit = 10,
Timestamp twentyFourHoursAgoTimestamp = Timestamp.fromDate(twentyFourHoursAgo); QueryDocumentSnapshot<Map<String, dynamic>>? offset}) async {
QuerySnapshot<Map<String, dynamic>> response;
QuerySnapshot<Map<String, dynamic>> response = await FirebaseFirestore.instance response = await FirebaseFirestore.instance
.collection("posts") .collection("posts")
.where("date", isGreaterThan: twentyFourHoursAgoTimestamp) .orderBy("date", descending: true)
.limit(limit) .limit(limit)
.get(); .get();
@ -58,18 +59,15 @@ class PostService {
return filteredPosts; return filteredPosts;
} }
Timestamp _getTwentyFourHoursAgoTimestamp() { Future<List<QueryDocumentSnapshot<Map<String, dynamic>>>> getPostsFriends(
DateTime twentyFourHoursAgo = DateTime.now().subtract(Duration(hours: 24)); {int limit = 10, int offset = 0}) async {
return Timestamp.fromDate(twentyFourHoursAgo);
}
Future<List<QueryDocumentSnapshot<Map<String, dynamic>>>> getPostsFriends({int limit = 10, int offset = 0}) async {
var timestamp = _getTwentyFourHoursAgoTimestamp();
var response = await FirebaseFirestore.instance var response = await FirebaseFirestore.instance
.collection("posts") .collection("posts")
.where("user_id", whereIn: MyApp.userViewModel.userCurrent.followed) .where("user_id", whereIn: [
.where("date", isGreaterThan: timestamp) MyApp.userViewModel.userCurrent.id,
.orderBy("date") ...MyApp.userViewModel.userCurrent.followed
])
.orderBy("date", descending: true)
.limit(limit) .limit(limit)
.get(); .get();
@ -79,12 +77,17 @@ class PostService {
Future<bool> getAvailable(String idUser) async { Future<bool> getAvailable(String idUser) async {
DateTime today = DateTime.now(); DateTime today = DateTime.now();
QuerySnapshot<Map<String, dynamic>> response = QuerySnapshot<Map<String, dynamic>> response = await FirebaseFirestore
await FirebaseFirestore.instance.collection("posts").where("user_id", isEqualTo: idUser).get(); .instance
.collection("posts")
.where("user_id", isEqualTo: idUser)
.get();
bool isTodayAvailable = response.docs.any((doc) { bool isTodayAvailable = response.docs.any((doc) {
DateTime date = doc["date"].toDate(); // Assuming the field name is "date" DateTime date = doc["date"].toDate();
return date.day == today.day && date.month == today.month && date.year == today.year; return date.day == today.day &&
date.month == today.month &&
date.year == today.year;
}); });
return !isTodayAvailable; return !isTodayAvailable;
@ -95,11 +98,15 @@ class PostService {
DateTime sevenDaysAgo = DateTime.now().subtract(Duration(days: 6)); DateTime sevenDaysAgo = DateTime.now().subtract(Duration(days: 6));
QuerySnapshot<Map<String, dynamic>> response = QuerySnapshot<Map<String, dynamic>> response = await FirebaseFirestore
await FirebaseFirestore.instance.collection("posts").where("user_id", isEqualTo: id).get(); .instance
.collection("posts")
.where("user_id", isEqualTo: id)
.get();
List<Map<String, dynamic>?> postList = List<Map<String, dynamic>?> postList = response.docs
response.docs.map((DocumentSnapshot<Map<String, dynamic>> doc) => doc.data()).toList(); .map((DocumentSnapshot<Map<String, dynamic>> doc) => doc.data())
.toList();
for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) {
DateTime date = sevenDaysAgo.add(Duration(days: i)); DateTime date = sevenDaysAgo.add(Duration(days: i));
@ -116,7 +123,8 @@ class PostService {
} }
Future<List<String>> getLikesByPostId(String id) async { Future<List<String>> 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) { if (response.exists) {
var musicFavorite = response.get("likes"); var musicFavorite = response.get("likes");
return List.from(musicFavorite); return List.from(musicFavorite);

@ -205,10 +205,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: collection name: collection
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687 sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.17.2" version: "1.17.1"
convert: convert:
dependency: transitive dependency: transitive
description: description:
@ -804,18 +804,18 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: matcher name: matcher
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.12.16" version: "0.12.15"
material_color_utilities: material_color_utilities:
dependency: transitive dependency: transitive
description: description:
name: material_color_utilities name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.5.0" version: "0.2.0"
meta: meta:
dependency: transitive dependency: transitive
description: description:
@ -993,10 +993,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: source_span name: source_span
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.10.0" version: "1.9.1"
sqflite: sqflite:
dependency: transitive dependency: transitive
description: description:
@ -1057,10 +1057,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: test_api name: test_api
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.6.0" version: "0.5.1"
text_scroll: text_scroll:
dependency: "direct main" dependency: "direct main"
description: description:
@ -1141,14 +1141,6 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.1.4" version: "2.1.4"
web:
dependency: transitive
description:
name: web
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
url: "https://pub.dev"
source: hosted
version: "0.1.4-beta"
win32: win32:
dependency: transitive dependency: transitive
description: description:
@ -1182,5 +1174,5 @@ packages:
source: hosted source: hosted
version: "1.1.0" version: "1.1.0"
sdks: sdks:
dart: ">=3.1.0-185.0.dev <4.0.0" dart: ">=3.0.0 <4.0.0"
flutter: ">=3.10.0" flutter: ">=3.10.0"

Loading…
Cancel
Save