Question from junior Padawan to real Jedy.
I'm developing a website for a designer portfolio. Without any registration and backend. I'm using Firebase instead. I need to add a feature to collect "likes" (reactions) from anonymous users on works/posts.
Firebase RTDB structure:
artworks:
someArtWorkID1:
name: "lorem ipsum"
description: "lorem ipsum"
date: "01.01.1970"
src: "here should be url to picture"
**likes**: (?)
Idk how to implement it. Should it be a simple number or array of UIDs (anonymous user ID)?
Or another structure:
artworks...
likes:
idOfArtwork:
uid: true,
uid2: true
..........
But in that case, how can I calculate the sum of "likes" for specific post/artwork?
Also, I am new to Firebase and haven't any ideas how to write rules for it. In order for the anonymous user could add/delete only his UID in the likes array. Or should I use another solution for this task? Help me please)
CodePudding user response:
or array of UIDs (anonymous user IDs)
That's one of the best options you can have. Each time a user likes an artwork, add the UID in a node, and if the retracts the like, remove the UID from that node.
But in that case how can I calculate the sum of "likes"?
Simply reading the node and counting the number of children. Alternatively, you can also increment/decrement a separate counter if you want, for a simpler read.