public async Task <bool> UpdateMovieAsync(UpdateMovieInputModel input) { if (!await dbContext.Movies.AnyAsync(movie => movie.Id == input.Id)) { return(false); } if (!await dbContext.Genres.AnyAsync(genre => genre.Name == input.Genre)) { return(false); } if (!await dbContext.Artists.AnyAsync(artist => artist.FullName == input.Director)) { return(false); } var genreFromDb = await dbContext.Genres.SingleOrDefaultAsync(g => g.Name == input.Genre); var directorFromDb = await dbContext.Artists.SingleOrDefaultAsync(a => a.FullName == input.Director); var movieFromDb = await dbContext.Movies.FindAsync(input.Id); movieFromDb.Name = input.Name; movieFromDb.Genre = genreFromDb; movieFromDb.ReleaseDate = input.ReleaseDate; movieFromDb.Length = input.Length; movieFromDb.Description = input.Description; movieFromDb.CoverImageLink = input.CoverImageLink; movieFromDb.TrailerLink = input.TrailerLink; movieFromDb.Director = directorFromDb; dbContext.Update(movieFromDb); await dbContext.SaveChangesAsync(); return(true); }
public async Task UpdateMovieShouldReturnFalseIfProvidedIdIsInvalid() { var genre1 = new Genre { Name = "genre1" }; var artist1 = new Artist { FullName = "name1", Biography = "biography1", BirthDate = DateTime.Parse("25 July 2019"), PhotoLink = "photo1", }; var movie1 = new Movie { Name = "movie1", ReleaseDate = DateTime.Parse("24 July 2019"), Description = "description1", Length = 160, Genre = genre1, Director = artist1, CoverImageLink = "cover1", TrailerLink = "trailer1", }; await dbContext.Genres.AddAsync(genre1); await dbContext.Artists.AddAsync(artist1); await dbContext.Movies.AddAsync(movie1); await dbContext.SaveChangesAsync(); var movieService = new MovieService(dbContext, reviewService.Object, watchlistService.Object, mapper); var input = new UpdateMovieInputModel { Id = "invalid id", Name = "new name", ReleaseDate = DateTime.Parse("24 July 2003"), Description = "new desc", Length = 120, Genre = "genre1", Director = "name1", CoverImageLink = "new cover", TrailerLink = "new trailer", }; var actualResult = await movieService.UpdateMovieAsync(input); Assert.False(actualResult); }
public async Task <IActionResult> Update(UpdateMovieInputModel input) { if (!ModelState.IsValid) { return(View(input)); } if (!await movieService.UpdateMovieAsync(input)) { return(View(input)); } return(Redirect(redirectMovieDetails + input.Id)); }
public async Task UpdateMovieShouldUpdateProperly() { var genre1 = new Genre { Name = "genre1" }; var genre2 = new Genre { Name = "genre2" }; var artist1 = new Artist { FullName = "name1", Biography = "biography1", BirthDate = DateTime.Parse("25 July 2019"), PhotoLink = "photo1", }; var artist2 = new Artist { FullName = "name2", Biography = "biography2", BirthDate = DateTime.Parse("26 July 2019"), PhotoLink = "photo2", }; var movie1 = new Movie { Name = "movie1", ReleaseDate = DateTime.Parse("24 July 2019"), Description = "description1", Length = 160, Genre = genre1, Director = artist1, CoverImageLink = "cover1", TrailerLink = "trailer1", }; await dbContext.Genres.AddAsync(genre1); await dbContext.Genres.AddAsync(genre2); await dbContext.Artists.AddAsync(artist1); await dbContext.Artists.AddAsync(artist2); await dbContext.Movies.AddAsync(movie1); await dbContext.SaveChangesAsync(); var movieService = new MovieService(dbContext, reviewService.Object, watchlistService.Object, mapper); var input = new UpdateMovieInputModel { Id = movie1.Id, Name = "new name", ReleaseDate = DateTime.Parse("24 July 2003"), Description = "new desc", Length = 120, Genre = "genre2", Director = "name2", CoverImageLink = "new cover", TrailerLink = "new trailer", }; var actualResult = await movieService.UpdateMovieAsync(input); Assert.True(actualResult); Assert.True(dbContext.Movies.Count() == 1); Assert.Equal(120, dbContext.Movies.First().Length); Assert.Equal("new name", dbContext.Movies.First().Name); Assert.Equal("new desc", dbContext.Movies.First().Description); Assert.Equal(DateTime.Parse("24 July 2003"), dbContext.Movies.First().ReleaseDate); Assert.Equal(genre2, dbContext.Movies.First().Genre); Assert.Equal(artist2, dbContext.Movies.First().Director); Assert.Equal("new cover", dbContext.Movies.First().CoverImageLink); Assert.Equal("new trailer", dbContext.Movies.First().TrailerLink); }