public async Task LikeMovie_WithAlreadyLikedMovie_ShouldThrowInvalidOperationException() { var userRepo = new Mock <IRepository <ApplicationUser> >(); var likedMovieRepo = new Mock <IRepository <LikedMovie> >(); userRepo.Setup(x => x.All()).Returns(new List <ApplicationUser>() { new ApplicationUser() { Id = "1", Activity = new List <UserActivity>() } }.AsQueryable); likedMovieRepo.Setup(x => x.All()).Returns(this.GetLikedMovieTestData().AsQueryable); this._movieService = new MovieService(null, likedMovieRepo.Object, userRepo.Object, null); var movieDto = new AddToLikesMovieDTO() { PosterPath = "123123123123123", ReleaseDate = "2020-03-10", Title = "The last air bender", UserId = "1", VoteAverage = "1.1", VoteCount = "1233" }; await Assert.ThrowsAsync <InvalidOperationException>(() => this._movieService.LikeMovie(movieDto)); }
public void LikeMovie_WithCorrectData_ShouldLikeMovieProperly() { string errorMessagePrefix = "MovieService LikeMovie() method does not work properly."; var repo = new Mock <IRepository <LikedMovie> >(); var activityRepo = new Mock <IRepository <UserActivity> >(); var userRepo = new Mock <IRepository <ApplicationUser> >(); repo.Setup(r => r.All()).Returns(this.GetLikedMovieTestData().AsQueryable()); userRepo.Setup(x => x.All()).Returns(new List <ApplicationUser>() { new ApplicationUser() { Id = "1", Activity = new List <UserActivity>() } }.AsQueryable); this._movieService = new MovieService(null, repo.Object, userRepo.Object, activityRepo.Object); var movieDto = new AddToLikesMovieDTO() { PosterPath = "poster path", ReleaseDate = "2020-03-10", Title = "The last asd", UserId = "1", VoteAverage = "1.1", VoteCount = "1233" }; var isCompleted = this._movieService.LikeMovie(movieDto).IsCompletedSuccessfully; Assert.True(isCompleted, errorMessagePrefix); }
public async Task LikeMovie(AddToLikesMovieDTO movieDTO) { if (string.IsNullOrEmpty(movieDTO.Title) || string.IsNullOrEmpty(movieDTO.PosterPath) || string.IsNullOrEmpty(movieDTO.ReleaseDate) || string.IsNullOrEmpty(movieDTO.VoteAverage) || string.IsNullOrEmpty(movieDTO.VoteCount)) { throw new InvalidOperationException("Invalid movie to like."); } var user = this._userRepository .All() .Single(u => u.Id == movieDTO.UserId); var movieToLike = new LikedMovie { PosterPath = movieDTO.PosterPath, ReleaseDate = DateTime.ParseExact(movieDTO.ReleaseDate, "yyyy-MM-dd", CultureInfo.CurrentCulture), Title = movieDTO.Title, UserId = movieDTO.UserId, VoteAverage = double.Parse(movieDTO.VoteAverage), VoteCount = int.Parse(movieDTO.VoteCount) }; var likedMovies = this._likedMovieRepository .All() .Where(x => x.UserId == movieDTO.UserId) .ToList(); if (likedMovies == null || likedMovies.Any(x => x.Title == movieToLike.Title && x.PosterPath == movieToLike.PosterPath)) { throw new InvalidOperationException("Movie is already liked."); } var activity = new UserActivity { Action = $"Like movie with title '{movieToLike.Title}'", RegisteredOn = DateTime.Now, UserId = movieDTO.UserId, }; await this._likedMovieRepository.AddAsync(movieToLike); await this._userActivityRepository.AddAsync(activity); user?.LikedMovies.Add(movieToLike); user?.Activity.Add(activity); await this._userRepository.SaveChangesAsync(); }
public async Task LikeMovie_WithIncorrectData_ShouldThrowInvalidOperationException(string posterPath, string releaseDate, string title, string voteAverage, string voteCount) { this._movieService = new MovieService(null, null, null, null); var movieDto = new AddToLikesMovieDTO() { PosterPath = posterPath, ReleaseDate = releaseDate, Title = title, UserId = "1", VoteAverage = voteAverage, VoteCount = voteCount }; await Assert.ThrowsAsync <InvalidOperationException>(() => this._movieService.LikeMovie(movieDto)); }
public async Task <ActionResult <AddToLikesMovieDTO> > LikeMovie([FromForm] AddToLikesMovieDTO movie) { try { _logger.LogInfo($"Adding movie with title {movie.Title} to liked..."); await this._movieService.LikeMovie(movie); _logger.LogInfo($"Successfully liked movie with title {movie.Title}..."); return(this.Ok()); } catch (Exception) { return(BadRequest()); } }