Beispiel #1
0
        public IActionResult GetLikesDislikes(int websiteId)
        {
            var repo   = new JokesRepository(_connectionString);
            var authDb = new Authentication(_connectionString);

            var      likes                = 0;
            var      dislikes             = 0;
            var      user                 = authDb.GetByEmail(User.Identity.Name);
            var      didUserLikeOrDislike = false;
            var      userLiked            = false;
            DateTime date                 = new DateTime();

            if (repo.JokeInDb(websiteId))
            {
                IEnumerable <UserLikedJokes> ul = repo.GetUserLikedJokes(repo.GetJokeId(websiteId));
                likes    = ul.Where(j => j.Liked).Count();
                dislikes = ul.Where(j => !j.Liked).Count();
                if (user != null)
                {
                    didUserLikeOrDislike = ul.Any(u => u.UserId == user.Id);
                    if (didUserLikeOrDislike)
                    {
                        date      = ul.FirstOrDefault(u => u.UserId == user.Id).Date;
                        userLiked = ul.Any(u => u.Liked && u.UserId == user.Id);
                    }
                }
            }

            return(Json(new { likes, dislikes, didUserLikeOrDislike, date, userLiked }));
        }