From d59c475b6b212f3e19762dbf111a98f7f6058156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20L=C3=A1szl=C3=B3?= Date: Fri, 10 May 2024 12:41:21 +0200 Subject: [PATCH] order posts by publishing date --- .../java/com/dowerx/quack/service/PostService.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/dowerx/quack/service/PostService.java b/app/src/main/java/com/dowerx/quack/service/PostService.java index 88fd5dc..7c1010f 100644 --- a/app/src/main/java/com/dowerx/quack/service/PostService.java +++ b/app/src/main/java/com/dowerx/quack/service/PostService.java @@ -19,9 +19,11 @@ import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.DocumentSnapshot; import com.google.firebase.firestore.Filter; import com.google.firebase.firestore.FirebaseFirestore; +import com.google.firebase.firestore.Query; import com.google.firebase.firestore.QueryDocumentSnapshot; import com.google.firebase.firestore.QuerySnapshot; +import java.util.Date; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -42,7 +44,7 @@ public class PostService { FirebaseFirestore db = FirebaseFirestore.getInstance(); try { - for (QueryDocumentSnapshot post : Tasks.await(db.collection("/post").get())) { + for (QueryDocumentSnapshot post : Tasks.await(db.collection("post").orderBy("date", Query.Direction.DESCENDING).get())) { DocumentSnapshot user = Tasks.await(post.getDocumentReference("user").get()); String username = user.get("username", String.class); String email = user.get("email", String.class); @@ -96,8 +98,10 @@ public class PostService { FirebaseFirestore db = FirebaseFirestore.getInstance(); try { - for (QueryDocumentSnapshot post : Tasks.await(db.collection("/post") - .where(Filter.equalTo("user", db.document("/user/"+username))).get())) { + for (QueryDocumentSnapshot post : Tasks.await(db.collection("post") + .where(Filter.equalTo("user", db.document("/user/"+username))) + .orderBy("date", Query.Direction.DESCENDING) + .get())) { DocumentSnapshot user = Tasks.await(post.getDocumentReference("user").get()); String username = user.get("username", String.class); String email = user.get("email", String.class); @@ -157,6 +161,7 @@ public class PostService { post.put("content", strings[0]); post.put("image", ""); post.put("user", user.getReference()); + post.put("date", new Date().getTime()); Tasks.await(db.collection("post").add(post)); Log.d(this.getClass().getName(), "after adding post");