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 })); }
public IActionResult DislikeJoke(Joke joke) { var repo = new JokesRepository(_connectionString); var authDb = new Authentication(_connectionString); var user = authDb.GetByEmail(User.Identity.Name); if (repo.JokeInDb(joke.WebsiteId)) { joke.Id = repo.GetJokeId(joke.WebsiteId); repo.LikeDislikeJoke(joke.Id, user.Id, false); } else { repo.AddJoke(joke); joke.Id = repo.GetJokeId(joke.WebsiteId); repo.LikeDislikeJoke(joke.Id, user.Id, false); } return(Json("")); }