C#
C#

help

Root Question Message

Lary
Lary9/30/2022
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.
Message Not Public

Sign In and Join Server To See

9/30/2022
Message Not Public

Sign In and Join Server To See

9/30/2022
Message Not Public

Sign In and Join Server To See

9/30/2022
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
no no of course not. My api sends me the related vote records
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
i am sending them I guess. Every vote which belongs to that post for example.
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
i am also getting the count of votes, without bringing vote list.
Lary
Lary9/30/2022
so i don't actually need vote list
Lary
Lary9/30/2022
as i said, i am looking for alternative
Lary
Lary9/30/2022
i don't want to send them
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
i already did those
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
i just need to fill the btns with color when user refreshes for example
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
js don't have the list of votes, i will give the codes in a min
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
api:
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);
    }

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
        };

Then I am checking if the logged in user id is in post.votes and filling the post accordingly
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
true or false. It's votes table, not dislike and like
Lary
Lary9/30/2022
true means liked, false means disliked
Message Not Public

Sign In and Join Server To See

9/30/2022
Message Not Public

Sign In and Join Server To See

9/30/2022
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
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
Lary
Lary9/30/2022
liked, dislike, take the vote back etc all working pretty well, i remastered it yesterday
Message Not Public

Sign In and Join Server To See

9/30/2022
Message Not Public

Sign In and Join Server To See

9/30/2022
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
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;
               }
        }
Lary
Lary9/30/2022
there is no problem with that, i can change a couple of aspects and make it better of course
Lary
Lary9/30/2022
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 😄
Lary
Lary9/30/2022
app at least, not client
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
cshtml, it doesn't actually send them to the client.
Lary
Lary9/30/2022
my server has them
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
you may be right i didn't check
Lary
Lary9/30/2022
i give them all to the viewmodel
Lary
Lary9/30/2022
i don't know if client has access to it
Message Not Public

Sign In and Join Server To See

9/30/2022
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
i have no idea honestly. It's local host app and i have like 10-20 vote records 😄
Lary
Lary9/30/2022
but i imagine if it was 5000, for each post they will see, maybe a couple of fetch request would do way better job
Message Not Public

Sign In and Join Server To See

9/30/2022
Message Not Public

Sign In and Join Server To See

9/30/2022
Lary
Lary9/30/2022
welp, then i am sticking with this, i would only delete a couple of lines if i change my mind
Message Not Public

Sign In and Join Server To See

9/30/2022
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy