public async Task LinkActorAndMovie(Guid actorId, Guid movieId) { var actor = await _actorRepository.FindByAsync(e => e.Id == actorId); if (actor == null) { throw new ArgumentException($"Actor with id: {actorId} doesn't exist"); } var movie = await _movieRepository.FindByAsync(e => e.Id == movieId); if (movie == null) { throw new ArgumentException($"Movie with id: {movieId} doesn't exist"); } var actorMovie = await _actorMovieRepository.FindByAsync(e => e.ActorId == actorId && e.MovieId == movieId); if (actorMovie != null) { throw new ArgumentException($"Actor {actorId} and Movie {movieId} already linked"); } actorMovie = new ActorMovie() { MovieId = movieId, ActorId = actorId, ActorMovieId = Guid.NewGuid() }; await _actorMovieRepository.AddAsync(actorMovie); }
public async Task CreateAsync(MovieCreate movieUi) { var actorIds = new List <Guid>(); if (movieUi.GenreId == Guid.Empty) { throw new ArgumentException($"Genre can't be null"); } if (movieUi.FirstActor == Guid.Empty) { throw new ArgumentException($"Movie should has at least one actor"); } var existedActor = await _actorRepository.FindByAsync(e => e.Id == movieUi.FirstActor); if (existedActor == null) { throw new ArgumentException($"Can't create movie with not existed actor id : {movieUi.FirstActor}"); } var movie = new Movie() { Genre = movieUi.Genre, GenreId = movieUi.GenreId, Id = Guid.NewGuid(), Title = movieUi.Title, Year = movieUi.Year, }; await _movieRepository.AddAsync(movie); //Link movie to actors var actorMovie = new ActorMovie() { ActorId = movieUi.FirstActor, MovieId = movie.Id, ActorMovieId = Guid.NewGuid() }; await _actorMovieRepository.AddAsync(actorMovie); }