C
C#3y ago
İrşat

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
Grault
Grault3y ago
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.
İrşat
İrşatOP3y ago
no no of course not. My api sends me the related vote records
Grault
Grault3y ago
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.
İrşat
İrşatOP3y ago
i am sending them I guess. Every vote which belongs to that post for example.
Grault
Grault3y ago
If you care about speed (or privacy) those details should not touch the client.
İrşat
İrşatOP3y ago
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
Grault
Grault3y ago
Right, so if each post in DOM has an ID then you can send a list of IDs of posts the user voted for.
İrşat
İrşatOP3y ago
i already did those
Grault
Grault3y ago
And a number of votes for each post regardless of whether the user voted for them.
İrşat
İrşatOP3y ago
i just need to fill the btns with color when user refreshes for example
Grault
Grault3y ago
Those two details are all you need, I think, and JS doesn't need the list of votes.
İrşat
İrşatOP3y ago
js don't have the list of votes, i will give the codes in a min
Grault
Grault3y ago
K.
İrşat
İrşatOP3y ago
api:
var posts = await mapper.ProjectTo<PostDtoRead_1>(db.Posts.Where(p =>
p.IsPublished == true &&
p.DeletedStatus!.Body == "Default")).ToListAsync();

return posts;
var posts = await mapper.ProjectTo<PostDtoRead_1>(db.Posts.Where(p =>
p.IsPublished == true &&
p.DeletedStatus!.Body == "Default")).ToListAsync();

return posts;
and constructor in api has this
public PostDtoRead_1(ICollection<VoteDto>? Votes)
{
if(Votes != null && Votes.Count > 0)
this.VoteResult = Votes.Count(l => l.Body) - Votes.Count(d => !d.Body);
}
public PostDtoRead_1(ICollection<VoteDto>? Votes)
{
if(Votes != null && Votes.Count > 0)
this.VoteResult = Votes.Count(l => l.Body) - Votes.Count(d => !d.Body);
}
Then I get those with controller from my app (temporary, i will use fetch later)
string postsUrl = "https://localhost:7022/posts";
string raw = await _httpClient.GetStringAsync(postsUrl);
List<PostRead1>? posts = JsonSerializer.Deserialize<List<PostRead1>>(raw);

ReadViewmodel viewModel = new ReadViewmodel{
posts = posts
};
string postsUrl = "https://localhost:7022/posts";
string raw = await _httpClient.GetStringAsync(postsUrl);
List<PostRead1>? posts = JsonSerializer.Deserialize<List<PostRead1>>(raw);

ReadViewmodel viewModel = new ReadViewmodel{
posts = posts
};
Then I am checking if the logged in user id is in post.votes and filling the post accordingly
Grault
Grault3y ago
d => !d.Body What does the bool Body signify?
İrşat
İrşatOP3y ago
true or false. It's votes table, not dislike and like true means liked, false means disliked
Grault
Grault3y ago
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.
İrşat
İrşatOP3y ago
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
Grault
Grault3y ago
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
İrşat
İrşatOP3y ago
int votedByUser = -2; // -2 means not logged in
if(userId != "-1"){
if(post.votes.SingleOrDefault(p => p.accountId == Convert.ToInt32(userId) && p.body == true) != null){
votedByUser = 1;
}
else if(post.votes.SingleOrDefault(p => p.accountId == Convert.ToInt32(userId) && p.body == false) != null){
votedByUser = 0;
}
else{
votedByUser = -1;
}
}
int votedByUser = -2; // -2 means not logged in
if(userId != "-1"){
if(post.votes.SingleOrDefault(p => p.accountId == Convert.ToInt32(userId) && p.body == true) != null){
votedByUser = 1;
}
else if(post.votes.SingleOrDefault(p => p.accountId == Convert.ToInt32(userId) && p.body == false) != null){
votedByUser = 0;
}
else{
votedByUser = -1;
}
}
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
Grault
Grault3y ago
So that code there is running on the client?
İrşat
İrşatOP3y ago
cshtml, it doesn't actually send them to the client. my server has them
Grault
Grault3y ago
So you mean it's happening in Razor. That's not really a problem.
İrşat
İrşatOP3y ago
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
Grault
Grault3y ago
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?
İrşat
İrşatOP3y ago
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
Grault
Grault3y ago
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.
İrşat
İrşatOP3y ago
welp, then i am sticking with this, i would only delete a couple of lines if i change my mind
Grault
Grault3y ago
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.
İrşat
İrşatOP3y ago
thanks!
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server