public async Task UpdateMovie_ShouldAddCompanyAndMovieCompany_WhenCompanyNotExists() { using var context = DbContextScopeFactory.Create(); var movieUnderTest = TestMovieSeededWithoutRelatedInfo; var relationUnderTest = new MovieCompany { Movie = movieUnderTest, MovieId = movieUnderTest.Id, Company = TestCompanyNotSeeded, CompanyId = TestCompanyNotSeeded.Id }; movieUnderTest.CompanyList.Add(relationUnderTest); await MovieDetailsDbAccess.UpdateMovie(Mapper.Map <UpdateMovieDetailsDto>(movieUnderTest)); context.SaveChanges(); var existingMovie = MoviesContext.Movies.Find(movieUnderTest.Id); var existingRelation = MoviesContext.MovieCompanies.Find(movieUnderTest.Id, TestCompanyNotSeeded.Id); var existingCompany = MoviesContext.Companies.Find(TestCompanyNotSeeded.Id); existingMovie.Should().Be(movieUnderTest); existingRelation.Should().BeEquivalentTo(relationUnderTest); existingCompany.Should().Be(TestCompanyNotSeeded); }
public async Task UpdateMovie_ShoulAddSecondMovieCompany_WhenOtherExistsPreviously() { var movieUnderTest = TestMovieSeededWithRelatedInfo; var secondRelationUnderTest = new MovieCompany { Movie = movieUnderTest, MovieId = movieUnderTest.Id, Company = TestCompanyNotSeeded, CompanyId = TestCompanyNotSeeded.Id }; using var context = DbContextScopeFactory.Create(); movieUnderTest.CompanyList.Add(secondRelationUnderTest); await MovieDetailsDbAccess.UpdateMovie(Mapper.Map <UpdateMovieDetailsDto>(movieUnderTest)); context.SaveChanges(); var existingFirstRelation = MoviesContext.MovieCompanies.Include(x => x.Company).Single(x => x.MovieId == movieUnderTest.Id && x.CompanyId == TestCompanySeeded.Id); var existingSecondRelation = MoviesContext.MovieCompanies.Find(movieUnderTest.Id, TestCompanyNotSeeded.Id); var existingSecondCompany = MoviesContext.Companies.Find(TestCompanyNotSeeded.Id); var existingMovie = MoviesContext.Movies.Find(movieUnderTest.Id); existingFirstRelation.Should().BeEquivalentTo(TestMovieCompanySeeded); existingSecondRelation.Should().BeEquivalentTo(secondRelationUnderTest); existingSecondCompany.Should().Be(TestCompanyNotSeeded); existingMovie.Should().Be(movieUnderTest); }
public MovieCompany Delete(MovieCompany MovieCompany) { var result = context.Remove(MovieCompany); context.SaveChanges(); return(result.Entity); }
public MovieCompany Create(MovieCompany MovieCompany) { var result = context.Add <MovieCompany>(MovieCompany); context.SaveChanges(); return(result.Entity); }
public MovieCompany Post(MovieCompanyDTO value) { MovieCompany model = new MovieCompany() { MovieId = value.MovieId, CompanyId = value.CompanyId, }; return(IMovieCompanyRepository.Create(model)); }
private void CreateTestManyToManyData() { TestMovieDirectorSeeded = new MovieDirector { Person = TestDirectorSeeded, PersonId = TestDirectorSeeded.Id, Movie = TestMovieSeededWithRelatedInfo, MovieId = TestMovieSeededWithRelatedInfo.Id }; TestMovieActorSeeded = new MovieActor { Person = TestActorSeeded, PersonId = TestActorSeeded.Id, Movie = TestMovieSeededWithRelatedInfo, MovieId = TestMovieSeededWithRelatedInfo.Id, AsCharacter = "Test" }; TestMovieWriterSeeded = new MovieWriter { Person = TestWriterSeeded, PersonId = TestWriterSeeded.Id, Movie = TestMovieSeededWithRelatedInfo, MovieId = TestMovieSeededWithRelatedInfo.Id, }; TestMovieCompanySeeded = new MovieCompany { Company = TestCompanySeeded, CompanyId = TestCompanySeeded.Id, Movie = TestMovieSeededWithRelatedInfo, MovieId = TestMovieSeededWithRelatedInfo.Id, }; TestMovieImageSeeded = fixture.Build <MovieImageData>() .With(x => x.Movie, TestMovieSeededWithRelatedInfo) .With(x => x.MovieId, TestMovieSeededWithRelatedInfo.Id) .Create(); TestMovieImageNotSeeded = fixture.Build <MovieImageData>() .With(x => x.Movie, TestMovieNotSeeded) .With(x => x.MovieId, TestMovieNotSeeded.Id) .Create(); TestMovieSimilarSeeded = new MovieSimilar { Similar = TestMovieSeededWithoutRelatedInfo, SimilarId = TestMovieSeededWithoutRelatedInfo.Id, Movie = TestMovieSeededWithRelatedInfo, MovieId = TestMovieSeededWithRelatedInfo.Id, }; TestMovieSeededWithRelatedInfo.DirectorList.Add(TestMovieDirectorSeeded); TestMovieSeededWithRelatedInfo.ActorList.Add(TestMovieActorSeeded); TestMovieSeededWithRelatedInfo.WriterList.Add(TestMovieWriterSeeded); TestMovieSeededWithRelatedInfo.CompanyList.Add(TestMovieCompanySeeded); TestMovieSeededWithRelatedInfo.Images.Add(TestMovieImageSeeded); TestMovieSeededWithRelatedInfo.Similars.Add(TestMovieSimilarSeeded); }
public MovieCompany Put(int id, MovieCompanyDTO value) { MovieCompany model = IMovieCompanyRepository.Get(id); if (value.MovieId != 0) { model.MovieId = value.MovieId; } if (value.CompanyId != 0) { model.CompanyId = value.CompanyId; } return(IMovieCompanyRepository.Update(model)); }
//----------------------------------------------------------------// public async Task <List <MovieCompany> > SaveMovieCompanies(Movie movie, IEnumerable <ProductionCompany> companies) { List <MovieCompany> movieCompanies = new List <MovieCompany>(); foreach (ProductionCompany company in companies) { MovieCompany movieCompany = new MovieCompany(movie, company); movie.ProductionCompanies.Add(company); company.Movies.Add(movie); if (await _movieCompanyCommand.SaveIfNotExist(_movieCompanyQuery, movieCompany)) { movieCompanies.Add(movieCompany); } } return(movieCompanies); }
public void Post(MovieDTO value) { Movie model = new Movie() { Title = value.Title, ReleaseDate = value.ReleaseDate, RunTime = value.RunTime, Vote = value.Vote }; IMovieRepository.Create(model); for (int i = 0; i < value.ActorId.Count; i++) { MovieActor MovieActor = new MovieActor() { MovieId = model.Id, ActorId = value.ActorId[i] }; IMovieActorRepository.Create(MovieActor); } for (int i = 0; i < value.CompanyId.Count; i++) { MovieCompany MovieCompany = new MovieCompany() { MovieId = model.Id, CompanyId = value.CompanyId[i] }; IMovieCompanyRepository.Create(MovieCompany); } for (int i = 0; i < value.GenreId.Count; i++) { MovieGenre MovieGenre = new MovieGenre() { MovieId = model.Id, GenreId = value.GenreId[i] }; IMovieGenreRepository.Create(MovieGenre); } }
public MovieCompany Delete(int id) { MovieCompany model = IMovieCompanyRepository.Get(id); return(IMovieCompanyRepository.Delete(model)); }
private void UpdateRelatedInfo(UpdateMovieDetailsDto movie) { foreach (var image in movie.Images) { var existingLink = DbContext.MovieImageDatas.Find(new object[] { image.MovieId, image.Id }); if (existingLink != null) { continue; } var existingMovie = DbContext.Movies.Find(new object[] { image.MovieId }); if (existingMovie == null) { throw new InvalidOperationException("Se esperaba encontrar pelicula para agregar personas"); } existingMovie.Images.Add(new MovieImageData { Id = image.Id, Movie = existingMovie, MovieId = existingMovie.Id, Image = image.Image, Title = image.Title }); } //TODO: refactor this foreach (var moviePerson in movie.ActorList) { var existingLink = DbContext.MovieActors.Find(new object[] { moviePerson.MovieId, moviePerson.PersonId }); if (existingLink != null) { continue; } var existingPerson = DbContext.People.Find(new object[] { moviePerson.PersonId }); if (existingPerson == null) { DbContext.People.Add(moviePerson.Person); existingPerson = DbContext.People.Find(new object[] { moviePerson.PersonId }); } var existingMovie = DbContext.Movies.Find(new object[] { moviePerson.MovieId }); if (existingMovie == null) { throw new InvalidOperationException("Se esperaba encontrar pelicula para agregar personas"); } var newLink = new MovieActor { Movie = existingMovie, MovieId = existingMovie.Id, Person = existingPerson, PersonId = existingPerson.Id, AsCharacter = moviePerson.AsCharacter, IsStar = moviePerson.IsStar }; DbContext.MovieActors.Add(newLink); } foreach (var moviePerson in movie.WriterList) { var existingLink = DbContext.MovieWriters.Find(new object[] { moviePerson.MovieId, moviePerson.PersonId }); if (existingLink != null) { continue; } var existingPerson = DbContext.People.Find(new object[] { moviePerson.PersonId }); if (existingPerson == null) { DbContext.People.Add(moviePerson.Person); existingPerson = DbContext.People.Find(new object[] { moviePerson.PersonId }); } var existingMovie = DbContext.Movies.Find(new object[] { moviePerson.MovieId }); if (existingMovie == null) { throw new InvalidOperationException("Se esperaba encontrar pelicula para agregar personas"); } var newLink = new MovieWriter { Movie = existingMovie, MovieId = existingMovie.Id, Person = existingPerson, PersonId = existingPerson.Id, }; DbContext.MovieWriters.Add(newLink); } foreach (var moviePerson in movie.DirectorList) { var existingLink = DbContext.MovieDirectors.Find(new object[] { moviePerson.MovieId, moviePerson.PersonId }); if (existingLink != null) { continue; } var existingPerson = DbContext.People.Find(new object[] { moviePerson.PersonId }); if (existingPerson == null) { DbContext.People.Add(moviePerson.Person); existingPerson = DbContext.People.Find(new object[] { moviePerson.PersonId }); } var existingMovie = DbContext.Movies.Find(new object[] { moviePerson.MovieId }); if (existingMovie == null) { throw new InvalidOperationException("Se esperaba encontrar pelicula para agregar personas"); } var newLink = new MovieDirector { Movie = existingMovie, MovieId = existingMovie.Id, Person = existingPerson, PersonId = existingPerson.Id, }; DbContext.MovieDirectors.Add(newLink); } foreach (var movieSimilar in movie.Similars) { var existingLink = DbContext.MovieSimilars.Find(new object[] { movieSimilar.MovieId, movieSimilar.SimilarId }); if (existingLink != null) { continue; } var existingSimilar = DbContext.Movies.Find(new object[] { movieSimilar.SimilarId }); if (existingSimilar == null) { DbContext.Movies.Add(movieSimilar.Similar); existingSimilar = DbContext.Movies.Find(new object[] { movieSimilar.SimilarId }); } var existingMovie = DbContext.Movies.Find(new object[] { movieSimilar.MovieId }); if (existingMovie == null) { throw new InvalidOperationException("Se esperaba encontrar pelicula para agregar personas"); } var newLink = new MovieSimilar { Movie = existingMovie, MovieId = existingMovie.Id, Similar = existingSimilar, SimilarId = existingSimilar.Id, }; DbContext.MovieSimilars.Add(newLink); } foreach (var movieCompany in movie.CompanyList) { var existingLink = DbContext.MovieCompanies.Find(new object[] { movieCompany.MovieId, movieCompany.CompanyId }); if (existingLink != null) { continue; } var existingCompany = DbContext.Companies.Find(new object[] { movieCompany.CompanyId }); if (existingCompany == null) { DbContext.Companies.Add(movieCompany.Company); existingCompany = DbContext.Companies.Find(new object[] { movieCompany.CompanyId }); } var existingMovie = DbContext.Movies.Find(new object[] { movieCompany.MovieId }); if (existingMovie == null) { throw new InvalidOperationException("Se esperaba encontrar pelicula para agregar personas"); } var newLink = new MovieCompany { Movie = existingMovie, MovieId = existingMovie.Id, Company = existingCompany, CompanyId = existingCompany.Id, }; DbContext.MovieCompanies.Add(newLink); } }
public void Put(int id, MovieDTO value) { Movie model = IMovieRepository.Get(id); if (value.Title != null) { model.Title = value.Title; } if (value.ReleaseDate != null) { model.ReleaseDate = value.ReleaseDate; } if (value.RunTime != 0) { model.RunTime = value.RunTime; } if (value.Vote != 0) { model.Vote = value.Vote; } IMovieRepository.Update(model); if (value.ActorId != null) { IEnumerable <MovieActor> MyMovieActors = IMovieActorRepository.GetAll().Where(x => x.MovieId == id); foreach (MovieActor MyMovieActor in MyMovieActors) { IMovieActorRepository.Delete(MyMovieActor); } for (int i = 0; i < value.ActorId.Count; i++) { MovieActor MovieActor = new MovieActor() { MovieId = model.Id, ActorId = value.ActorId[i] }; IMovieActorRepository.Create(MovieActor); } } if (value.CompanyId != null) { IEnumerable <MovieCompany> MyMovieCompanies = IMovieCompanyRepository.GetAll().Where(x => x.MovieId == id); foreach (MovieCompany MyMovieCompany in MyMovieCompanies) { IMovieCompanyRepository.Delete(MyMovieCompany); } for (int i = 0; i < value.CompanyId.Count; i++) { MovieCompany MovieCompany = new MovieCompany() { MovieId = model.Id, CompanyId = value.CompanyId[i] }; IMovieCompanyRepository.Create(MovieCompany); } } if (value.GenreId != null) { IEnumerable <MovieGenre> MyMovieGenres = IMovieGenreRepository.GetAll().Where(x => x.MovieId == id); foreach (MovieGenre MyMovieGenre in MyMovieGenres) { IMovieGenreRepository.Delete(MyMovieGenre); } for (int i = 0; i < value.GenreId.Count; i++) { MovieGenre MovieGenre = new MovieGenre() { MovieId = model.Id, GenreId = value.GenreId[i] }; IMovieGenreRepository.Create(MovieGenre); } } }
public MovieCompany Update(MovieCompany MovieCompany) { context.Entry(MovieCompany).State = Microsoft.EntityFrameworkCore.EntityState.Modified; context.SaveChanges(); return(MovieCompany); }