N
N
Ness2020-03-24 21:59:25
.NET
Ness, 2020-03-24 21:59:25

How to implement the count of dislikes in this case?

There is such a model:

[Table("PostsData")]
  public class PostData
  {
    [PrimaryKey,AutoIncrement]
        public int Id { get; private set; }
    public string PostUrl { get; set; }
    public long? PostId { get; set; }
    public int MessageId { get; set; }
    public int Likes { get; set; }
    public int DisLikes { get; set; }

        public virtual List<VoteData> VoteDatas { get; set; }
    }


  [Table("VotesData")]
  public class VoteData
  {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public int PostDataId { get; set; }
    public int? VoterId { get; set; }
    public bool LikedPost { get; set; }

        public virtual PostData Post { get; set; }
    }


I catch a callback on a button from a telegram and, depending on the callback, I do some manipulations, there is such a code for counting likes:
int messageId = callbackQuery.Message.MessageId;
                    var userId = callbackQuery.From.Id;
                    var postData = context.Posts
                        .Include("VoteDatas")
                        .FirstOrDefault(p => p.MessageId == messageId);

                    if (postData == null)
                    {
                        return;
                    }

                    var voteData = postData.VoteDatas.ToList();

                        var inLikes = postData.VoteDatas
                            .FirstOrDefault(v =>
                            v.PostDataId == postData.Id && v.VoterId == userId);

                        var inDislikes = voteData
                        .FirstOrDefault(v =>
                        v.PostDataId == postData.Id && v.VoterId == userId && !v.LikedPost);

                    try
                    {
                        if (inLikes != null)
                        {
                            context.Votes.Remove(inLikes);
                        } else
                        {
                            context.Votes.Add(new VoteData() {
                                VoterId = userId, LikedPost = true, PostDataId = postData.Id
                            });
                        }

                        if (inDislikes != null)
                        {
                            context.Votes.Remove(inDislikes);
                        }

                        context.SaveChanges();

                        var likesCount = postData.VoteDatas.Count();


But how to calculate the normal dislikes, I xs. Tell me please

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question