Check if user already voted this post or comment [Answered]
I want to fill the like/dislike buttons with color if they are already voted by the user. Which one would be faster and more economic;
-Fetching the votes of the parent post/comment from the start, then checking if this user's id is there or...
-Another request to the api to check if user has a record already with that post's id in votes table.
31 Replies
I suspect it would be faster to do the first, but that's subject to your performance measurements. And you may even find that it doesn't matter one way or the other.
Also, I don't think you need make another request to the API with #2. The first request should be able to provide that information. Of course this may not be the case if you're using something that constrains how much information you can put in a response.
If #2 does require a second request, definitely #1.
Also... I hope you aren't sending a list of everyone who voted to the client. The client (the user) has no business knowing who voted.
no no of course not. My api sends me the related vote records
The server can figure out how many voted and which posts this user voted on, so the client doesn't know any more than that.
i am sending them I guess. Every vote which belongs to that post for example.
If you care about speed (or privacy) those details should not touch the client.
i am also getting the count of votes, without bringing vote list.
so i don't actually need vote list
as i said, i am looking for alternative
i don't want to send them
Right, so if each post in DOM has an ID then you can send a list of IDs of posts the user voted for.
i already did those
And a number of votes for each post regardless of whether the user voted for them.
i just need to fill the btns with color when user refreshes for example
Those two details are all you need, I think, and JS doesn't need the list of votes.
js don't have the list of votes, i will give the codes in a min
K.
api:
and constructor in api has this
Then I get those with controller from my app (temporary, i will use fetch later)
Then I am checking if the logged in user id is in post.votes and filling the post accordingly
d => !d.Body
What does the bool Body
signify?true or false. It's votes table, not dislike and like
true means liked, false means disliked
Got it.
Right, so the thing it's not doing yet is determining whether this user voted for a post.
You could have three colors, for voting the post up, not voting, and voting the post down.
well, i am getting the votes with account id and body, so i can check if the logged in user has his vote there. But I only applied it to one page
liked, dislike, take the vote back etc all working pretty well, i remastered it yesterday
Votes.SingleOrDefault(l => l.AccountId == me)?.Body
That should give you a bool?
for the user's opinion of a post.
true
, false
, or null
there is no problem with that, i can change a couple of aspects and make it better of course
but it's not really a problem right now. My problem is the speed. I am getting all the damn votes and giving it to the client 😄
app at least, not client
So that code there is running on the client?
cshtml, it doesn't actually send them to the client.
my server has them
So you mean it's happening in Razor. That's not really a problem.
you may be right i didn't check
i give them all to the viewmodel
i don't know if client has access to it
Unless you're using Blazor, I don't think any C# logic should end up in JS.
Are you having problems with speed, though? You think you need to optimize?
i have no idea honestly. It's local host app and i have like 10-20 vote records 😄
but i imagine if it was 5000, for each post they will see, maybe a couple of fetch request would do way better job
In general, if you haven't run into a performance problem your time is probably wasted in optimizing.
And your code can get more complex that way too, which can make it worse than useless to optimize.
welp, then i am sticking with this, i would only delete a couple of lines if i change my mind
If you have run into a performance problem, measure it before trying to solve it.
The major exception is if you spot some code that is just unreasonably inefficient. I sometimes unravel that without checking whether it matters.
thanks!
✅ This post has been marked as answered!