Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
            }
        }