Exemplo n.º 1
0
        public async Task <Unit> Handle(AddNewShowsCommand request, CancellationToken cancellationToken)
        {
            var lastAddedShowId = await _repository.GetLastAddedShowIdAsync(cancellationToken);

            var page = lastAddedShowId / ShowsPerPage;

            while (true)
            {
                var showIds = await _tvMazeApiClient.GetShowIdsAsync(page, cancellationToken);

                foreach (var nextShowId in showIds.Where(x => x > lastAddedShowId))
                {
                    var show = await _tvMazeApiClient.GetShowAsync(nextShowId, cancellationToken);

                    await _repository.AddAsync(new Show
                    {
                        Id   = show.Id,
                        Name = show.Name,
                        Cast = show.Embedded?.Cast?.OrderByDescending(x => x.Person.Birthday).Select(c => new Person
                        {
                            Id       = c.Person.Id,
                            Name     = c.Person.Name,
                            Birthday = c.Person.Birthday?.ToString("yyyy-MM-dd")
                        })
                    }, cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(Unit.Value);
                    }
                }

                if (cancellationToken.IsCancellationRequested || !showIds.Any())
                {
                    return(Unit.Value);
                }

                page++;
            }
        }