
Each User profile has a like counter that accumulates from all posts.
A user has 10 posts. For each like executed , it should update the post and the total likes on the user’s profile. They are in separate COLLECTIONS
Would I use relationships? This is my execution and is probably not effective.
TypeScript
executeLike(int postLikes, String profileId) async {
int newLikes = postLikes + 1;
//update post
var status = await _databases.updateDocument(
databaseId: '...',
collectionId: 'posts',
documentId: '...',
data: {'likes': newLikes});
//get userprofile
var profileData = await _databases.getDocument(
databaseId: '...', collectionId: 'public_profile', documentId: profileId);
int totalLikes = profileData.data['total_likes'];
int newTotalLikes = totalLikes + 1;
// update poster's total likes
await _databases.updateDocument(
databaseId: '...',
collectionId: 'public_profile',
documentId: profileId,
data: {'total_likes': newTotalLikes});
}
TL;DR
The developer wants to update the like counter for a user's profile and their individual posts. They are currently executing this by updating the post's like count and then updating the user's total likes separately. They are unsure if this is the most effective method and asking for advice.
A more efficient solution would be to use relationships between the user profile and the posts. By creating a reference in the post document to the user's profile, you can easily update both the post's like count and the user's total likes in a single update operation.
The updated code would look something like this:
```dart
executeLike(int postLikes, String profileRecommended threads
- Hi, can anyone give me some free credits...
Hi, can anyone give me some free credits to upgrade to pro for a few months?
- The current user is not authorized to pe...
Hello, I am attempting to receive fetch data from databases.listDocuments with each of its env parameter data for it to be shown on a NextJS project. However I ...
- Native Sign in with Google/Apple Flutter
Is native sign in with google / apple in Flutter possible with Appwrite?
