In this episode, we dive into figuring out if a user has already liked something, which is essential before allowing them to remove their like later on.
We start by creating a handy method (likedBy
) that checks whether a given user has liked a particular item (like a comment) using Redis’s SISMEMBER
method. This lets us see if the user’s ID exists in the set of IDs that have liked something.
Then, we update our UI so, for example, it can show a message like "You liked this" next to the relevant comments. This makes the experience more interactive and personal.
Next up, we tidy the code by centralizing the likes key logic into a dedicated getLikesKey
method. This avoids repeating ourselves, making the code easier to maintain and update later—like when we want to apply this logic to other models using a trait.
With that in place, we can accurately see who’s liked each comment and prep for the next episode, where we’ll tackle removing likes!