public async Task <List <Show> > GetShows(IShowRepository showRepository, int pageNumber)
        {
            var shows = new List <Show>();

            _logger.LogInformation($"Stating copying data from {nameof(pageNumber)} {pageNumber}");

            var showDtos = await _tvMazeService.GetShowsByPage(pageNumber).ConfigureAwait(false);

            _logger.LogInformation($"Found {showDtos.Count} Shows from TvMaze API service");

            foreach (var showDto in showDtos)
            {
                var tvMazeShowWithCast = await _tvMazeService.GetShowDataById(showDto.Id).ConfigureAwait(false);

                _logger.LogInformation($"Found TvMaze show {tvMazeShowWithCast} with cast");

                var show = new Show {
                    Id = tvMazeShowWithCast.Id, Name = tvMazeShowWithCast.Name
                };

                show.Cast.AddRange(tvMazeShowWithCast.Cast
                                   .TakeWhile(personDto => personDto != null)
                                   .Distinct(new PersonDtoEqualityComparer())
                                   .Select(personDto => new Cast
                {
                    CastPersonId = personDto.Id,
                    ShowId       = show.Id,
                    Person       = GetPerson(showRepository, personDto)
                }));

                shows.Add(show);
            }
            return(shows);
        }
Example #2
0
        public void GetShowDataById_WhenApiServiceReturns401_ShouldRecieveUnauthorizedFlurlHttpException()
        {
            using (var httpTest = new HttpTest())
            {
                httpTest.RespondWithJson(null, 401);

                var tvMazeService = new TvMazeService(_jsonJsonApiDataReader, GetLogger());

                var showData = tvMazeService.GetShowDataById(1);

                try
                {
                    showData.Wait();
                }
                catch (AggregateException aggregateException)
                {
                    aggregateException.Handle(exception =>
                    {
                        Assert.IsTrue(exception is FlurlHttpException flurlHttpException &&
                                      flurlHttpException.Call.HttpStatus == HttpStatusCode.Unauthorized);
                        return(true);
                    });
                }
            }
        }
Example #3
0
        public void GetShowDataById_WhereShowIdEqualsARandomId_ExpectsReturnDataToMatchId()
        {
            var randomId = new Random().Next(1, 10000);

            using (var httpTest = new HttpTest())
            {
                httpTest.RespondWithJson(new JObject {
                    { "id", randomId }
                });

                var sut = new TvMazeService(_jsonJsonApiDataReader, GetLogger());

                var showData = sut.GetShowDataById(randomId);

                showData.Wait();

                Assert.IsTrue(showData.Result.Id == randomId);
            }
        }