Ejemplo n.º 1
0
 public MovieViewModel(List <MovieDto> movies, LikeDislikeDto likeDislike)
 {
     Movies         = movies;
     LikeDislikeDto = likeDislike;
 }
Ejemplo n.º 2
0
        public LikeDislikeDto DislikeAFilm(int Id)
        {
            var filmLikesAndDislikes = Dbcontext.FilmLikesAndDislikes1;

            var userId = HttpContext.Current.User.Identity.GetUserId();
            var user   = Dbcontext.AspNetUsers.Single(u => u.Id.Equals(userId));

            var filmFromDb = Dbcontext.Films.Single(f => f.FilmID.Equals(Id));

            // in db values are stored as bytes : 1 == true, 0 == false
            var userLikedFilm = user.FilmLikesAndDislikes.SingleOrDefault(f => f.FilmID.Equals(Id) && f.UserID.Equals(userId));

            byte isLiked    = (byte)(userLikedFilm == null ? 0 : userLikedFilm.IsLiked);
            byte isDisliked = (byte)(userLikedFilm == null ? 0 : userLikedFilm.IsDisliked);

            if (isDisliked == 1)
            {
                return(null);
            }
            else if (isLiked == 1)
            {
                // user first liked, now wants to dislike this movie
                filmFromDb.FilmLikes--;
                filmFromDb.FilmDislikes++;
                userLikedFilm.IsDisliked = 1;
                userLikedFilm.IsLiked    = 0;

                Dbcontext.SaveChanges();

                var likeDislikeDto = new LikeDislikeDto
                {
                    FilmLikes    = filmFromDb.FilmLikes,
                    FilmDislikes = filmFromDb.FilmDislikes,
                    Message      = "Disliked!"
                };

                return(likeDislikeDto);
            }
            else
            {
                filmFromDb.FilmDislikes++;

                filmLikesAndDislikes.Add(new FilmLikesAndDislikes
                {
                    FilmID     = Id,
                    UserID     = userId,
                    IsLiked    = 0,
                    IsDisliked = 1
                });

                Dbcontext.SaveChanges();

                var likeDislikeDto = new LikeDislikeDto
                {
                    FilmLikes    = filmFromDb.FilmLikes,
                    FilmDislikes = filmFromDb.FilmDislikes,
                    Message      = "Disliked!"
                };

                return(likeDislikeDto);
            }
        }