Esempio n. 1
0
        public async Task <IActionResult> Delete(int id)
        {
            var result = await _repository.DeleteAsync(id);

            if (result)
            {
                return(NoContent());
            }

            return(NotFound());
        }
Esempio n. 2
0
        public async Task <IActionResult> Delete(int id)
        {
            var actor = await _actorRepository.GetAsync(id);

            if (actor == null)
            {
                return(NotFound());
            }

            await _actorRepository.DeleteAsync(actor);

            return(Ok());
        }
    public async Task <Unit> Handle(DeleteActorCommand request, CancellationToken cancellationToken)
    {
        ArgumentNullException.ThrowIfNull(request, nameof(request));

        _logger.LogInformation("Delete actor with ActorId: {ActorId}.", request.ActorId);

        var actorId = new ActorId(request.ActorId);

        var actor = await _actorRepository.GetActorAsync(actorId).ConfigureAwait(false);

        if (actor != null)
        {
            await _actorRepository.DeleteAsync(actor).ConfigureAwait(false);
        }

        return(Unit.Value);
    }
        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);
            }
        }
Esempio n. 5
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    var actorsFromWeb = await _IMDBScraperService.GetActorsAsync(stoppingToken).ConfigureAwait(false);;
                    var actorsFromDB  = await _actorRepository.GetAllAsync().ConfigureAwait(false);

                    IList <Actor> actorToInsert = new List <Actor>(actorsFromWeb.Count());
                    IList <Actor> actorToUpdate = new List <Actor>(actorsFromDB.Count());
                    IList <Actor> actorToDelete = new List <Actor>(actorsFromDB.Count());

                    foreach (var actor in actorsFromWeb)
                    {
                        if (actorsFromDB.Any(a => a.Name.Trim().ToLower() == actor.Name.Trim().ToLower()))
                        {
                            actorToUpdate.Add(actor);
                        }
                        else
                        {
                            actorToInsert.Add(actor);
                        }
                    }

                    foreach (var actor in actorsFromDB)
                    {
                        if (!actorsFromWeb.Any(a => a.Name.Trim().ToLower() == actor.Name.Trim().ToLower()))
                        {
                            actorToDelete.Add(actor);
                        }
                    }

                    //Insert Actor
                    foreach (var actor in actorToInsert)
                    {
                        try
                        {
                            await _actorRepository.InsertAsync(actor);

                            _logger.LogInformation($"Actor: '{actor.Name}'  Added");
                        }
                        catch (Exception exp)
                        {
                            _logger.LogError($"Failed to add actor: '{actor.Name}'", exp);
                        }
                    }

                    //Update Actor
                    foreach (var actor in actorToUpdate)
                    {
                        try
                        {
                            await _actorRepository.UpdateAsync(actor);

                            _logger.LogDebug($"Actor: '{actor.Name}'  Updated");
                        }
                        catch (Exception exp)
                        {
                            _logger.LogError($"Failed to update actor: '{actor.Name}'", exp);
                        }
                    }

                    //Delete Actor
                    foreach (var actor in actorToDelete)
                    {
                        try
                        {
                            await _actorRepository.DeleteAsync(actor);

                            _logger.LogInformation($"Actor: '{actor.Name}'  Deleted");
                        }
                        catch (Exception exp)
                        {
                            _logger.LogError($"Failed to delete actor: '{actor.Name}'", exp);
                        }
                    }
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception ex) { ErrorHandle(ex); }
            }

            return;
        }