Conta
Conta

Reputation: 176

Java Android Firestore Add like to a post

I'm developing a post like system. For posts I am using the following structure:

  root --- posts (collection)
             |
             --- uid (documents)
                  |
                  --- userPosts (collection)
                        |
                        --- postId (documents)
                        |     |
                        |     --- title: "Post Title"
                        |     |
                        |     --- date: September 03, 2018 at 6:16:58 PM UTC+3
                        |
                        --- postId (documents)
                              |
                              --- title: "Post Title"
                              |
                              --- date: 08/06/2021
                              |
                              --- description: "this is the description of the post"
                              [...etc]

I am using RecyclerView to fetch fields from each post. For the likes I had imagined the following structure (which was inserted within each single post as soon as the first like would have been placed) The structure I would like:

                   root---like
                             |
                             --- postId
                                      |
                                      --- postLikes
                                                  |
                                                  --uid
                                                  |
                                                  --uid

So when a user double-taps an image from a post, a like is added to the post. I put this in the onBindViewHolder of the Recyclerview:

 Map<String, Object> addLike = new HashMap<>();
                            addLike.put("like", FirebaseAuth.getInstance().getCurrentUser().getUid());


                            DocumentSnapshot snapshot = getSnapshots().getSnapshot(holder.getAdapterPosition());
                            final String postId = snapshot.getId();

                            FirebaseFirestore.getInstance().collection("likes").document(postId).collection("postLikes").add(addLike).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                                @Override
                                public void onComplete(@NonNull Task<DocumentReference> task) {

                                }
                            });

The problem is that by doing so, more collections are created that have a different name (generated randomly by the firestore) and within them there is the user id of the person who has put the like:

is there a way to simulate the structure i showed initially and then have all user ids inside a parent?

Upvotes: 0

Views: 124

Answers (2)

Conta
Conta

Reputation: 176

I finally solved it by doing this:

FirebaseFirestore.getInstance().collection("likes").document(postId).collection("postLikes").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).set(addLike).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {

                                }
                            });

I am creating a collection for each user who likes it, so the collection name and the collection value will be equal to the user is who liked

Upvotes: 0

androidLearner
androidLearner

Reputation: 1702

If you want to add all uid under "postLike" collection that is impossible .because You can't store field(uid) in collection(postLike). you can only store field under document.but if you want you can store all uid under "postId" document.

so when user double tap on like, add document "addLike" as you mentioned in your question. when another user double tap on like update document under "postLikes".so that you can keep all uid under one document.

//before update document fetch already existing liked user id under  document.
    Map<String, Object> addLike = new HashMap<>();
addLike.put("alreadyLikedUserId",alreadyLikedUserId );
                                addLike.put("like", FirebaseAuth.getInstance().getCurrentUser().getUid());
    
    
                                DocumentSnapshot snapshot = getSnapshots().getSnapshot(holder.getAdapterPosition());
                                final String postId = snapshot.getId();
  
                                FirebaseFirestore.getInstance().collection("likes").document(postId).collection("postLikes").doucment("documentIdUnderPostLike").update(addLike).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                                    @Override
                                    public void onComplete(@NonNull Task<DocumentReference> task) {
    
                                    }
                                });

Upvotes: 1

Related Questions