public void AddMovie(MovieDTO movieDTO) { int movieId = _movieRepository.AddMovie(movieDTO); if (movieDTO.Actors == null || !movieDTO.Actors.Any()) { return; } List <int> actorsIds = new List <int>(); foreach (string actor in movieDTO.Actors) { ActorDTO actorFromDb = _actorRepository.GetActorByName(actor); if (actorFromDb == null) { actorsIds.Add(_actorRepository.AddActor(new ActorDTO { ActorName = actor })); } else { actorsIds.Add(actorFromDb.ActorId); } } foreach (int actorId in actorsIds) { _actorRepository.AddActorByMovieId(actorId, movieId); } }