From d3246d082c1ea9aeb1f951d346aff7486d7c749d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20L=C3=A1szl=C3=B3?= Date: Fri, 10 May 2024 12:40:59 +0200 Subject: [PATCH] order posts by publishing date --- src/app/services/data/post.service.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/app/services/data/post.service.ts b/src/app/services/data/post.service.ts index 408bcea..c6dbe8b 100644 --- a/src/app/services/data/post.service.ts +++ b/src/app/services/data/post.service.ts @@ -20,7 +20,7 @@ export class PostService { let result: Array = new Array(); let currentUser = await this.usersvc.getCurrentUser(); - let posts = await firstValueFrom(this.afs.collection('post').valueChanges({ idField: 'id' })); + let posts = await firstValueFrom(this.afs.collection('post', ref => ref.orderBy('date', 'desc')).valueChanges({ idField: 'id' })); posts.forEach(async post=>{ let user = await firstValueFrom(this.afs.doc((post as any).user).valueChanges()) as User; @@ -51,7 +51,7 @@ export class PostService { // /user -> currentUser let user = await this.usersvc.getCurrentUser(); let userRef = this.afs.doc('/user/' + user.username).ref; - let posts = (await firstValueFrom(this.afs.collection('post', ref => ref.where('user', '==', userRef)).valueChanges({ idField: 'id' }))); + let posts = (await firstValueFrom(this.afs.collection('post', ref => ref.where('user', '==', userRef).orderBy('date', 'desc')).valueChanges({ idField: 'id' }))); posts.forEach(async (post) => { let likes = await firstValueFrom(this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges()); result.push({ @@ -70,7 +70,7 @@ export class PostService { let user = await this.usersvc.getUser(username); let userRef = this.afs.doc('/user/' + user.username).ref; let currentUser = await this.usersvc.getCurrentUser(); - let posts = (await firstValueFrom(this.afs.collection('post', ref => ref.where('user', '==', userRef)).valueChanges({ idField: 'id' }))); + let posts = (await firstValueFrom(this.afs.collection('post', ref => ref.where('user', '==', userRef).orderBy('date', 'desc')).valueChanges({ idField: 'id' }))); posts.forEach(async (post) => { let likes = await firstValueFrom(this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges()); result.push({ @@ -96,7 +96,8 @@ export class PostService { this.afs.collection('post').add({ user: this.afs.doc('/user/' + user.username).ref, content: content, - image: image + image: image, + date: Date.now() }) .then(() => resolve()) .catch(() => reject())