示例#1
0
        public async Task UnLinkActorAndMovie(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} not linked");
            }


            //movie must have at least one actor
            var anotherActorMovie = await _actorMovieRepository.FindByAsync(e => e.ActorId != actorId && e.MovieId == movieId);

            if (anotherActorMovie == null)
            {
                throw new ArgumentException($"Actor {actorId} and Movie {movieId} not linked");
            }

            await _actorMovieRepository.DeleteAsync(actorMovie);
        }
示例#2
0
        public async Task DeleteByIdAsync(Guid id)
        {
            var existedEntity = await _movieRepository.GetByIdAsync(id);

            if (existedEntity == null)
            {
                throw new ArgumentException($"Movie with id : {id} not found");
            }

            //we need to remove film from actors
            foreach (var actorMovie in existedEntity.ActorMovie)
            {
                await _actorMovieRepository.DeleteAsync(actorMovie);
            }

            await _movieRepository.DeleteAsync(existedEntity);
        }
        public async Task DeleteByIdAsync(Guid id)
        {
            var existedEntity = await _actorRepository.GetByIdAsync(id);

            if (existedEntity == null)
            {
                throw new ArgumentException($"Actor with id : {id} not found");
            }

            // Due to film can't be without any actors we should check that all related film will have at least one actor after we remove this one
            foreach (var actorMovie in existedEntity.ActorMovie)
            {
                var movie = await _movieRepository.GetByIdAsync(actorMovie.MovieId);

                if (movie.ActorMovie.Count <= 1)
                {
                    throw new InvalidOperationException("Can't remove actor due to it is last film actor");
                }
            }

            //remove actorMovie
            foreach (var actorMovie in existedEntity.ActorMovie)
            {
                var actorMovieDb = await _actorMovieRepository.FindByAsync(e => e.ActorMovieId == actorMovie.ActorMovieId);

                await _actorMovieRepository.DeleteAsync(actorMovieDb);
            }

            //fetch again to avoid concurrent issues
            existedEntity = await _actorRepository.GetByIdAsync(id);

            if (existedEntity != null)
            {
                await _actorRepository.DeleteAsync(existedEntity);
            }
        }