C
C#2y 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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y ago
no no of course not. My api sends me the related vote records
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y ago
i am sending them I guess. Every vote which belongs to that post for example.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y 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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y ago
i already did those
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y ago
i just need to fill the btns with color when user refreshes for example
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y ago
js don't have the list of votes, i will give the codes in a min
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y 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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y ago
true or false. It's votes table, not dislike and like true means liked, false means disliked
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y 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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y 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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y ago
cshtml, it doesn't actually send them to the client. my server has them
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y 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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y 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
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y ago
welp, then i am sticking with this, i would only delete a couple of lines if i change my mind
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
İrşat
İrşat2y ago
thanks!
Accord
Accord2y ago
✅ This post has been marked as answered!