Back

Like counter

  • 0
  • Flutter
  • Cloud
Dritz
8 Feb, 2024, 03:19

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 profile
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more