public Person CreateOrGet(Person newPerson)
        {
            var person = DbContext.Persons.AsNoTracking().Where(x => x.PersonId.Equals(newPerson.PersonId)).FirstOrDefault();

            if (person == null)
            {
                person = newPerson;
                DbContext.Persons.Add(newPerson);
                DbContext.SaveChanges();
            }
            return(person);
        }
        public void Handle_TooBigLimit_Should_Throw_ArgumentException()
        {
            var options = new DbContextOptionsBuilder <TvMazeDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;

            using (var context = new TvMazeDbContext(options))
            {
                // Arrange
                context.Shows.Add(new Show(1, "1"));
                context.SaveChanges();

                // Act Assert
                var queryHandler = new PaginatedShowsQueryHandler(context);
                Assert.ThrowsAsync <ArgumentException>(async() => await queryHandler.Handle(new PaginatedShowsQuery(Int32.MaxValue, 0), CancellationToken.None));
            }
        }
        public async Task Handle_EmptyCast_Should_Return_EmptyCast()
        {
            var options = new DbContextOptionsBuilder <TvMazeDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;

            using (var context = new TvMazeDbContext(options))
            {
                // Arrange
                context.Shows.Add(new Show(1, "1"));
                context.SaveChanges();

                // Act
                var queryHandler = new PaginatedShowsQueryHandler(context);
                var result       = await queryHandler.Handle(new PaginatedShowsQuery(1, 0), CancellationToken.None);

                // Assert
                Assert.AreEqual(0, result.First().Cast.Count);
            }
        }
        public async Task Handle_Cast_Should_Distinct_DupplicatedRows()
        {
            var options = new DbContextOptionsBuilder <TvMazeDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;

            using (var context = new TvMazeDbContext(options))
            {
                // Arrange
                var show1   = context.Shows.Add(new Show(1, "1"));
                var person1 = context.Persons.Add(new Person(1, "1", null));
                context.Casts.Add(new ShowCast(show1.Entity, person1.Entity));
                context.Casts.Add(new ShowCast(show1.Entity, person1.Entity));
                context.Casts.Add(new ShowCast(show1.Entity, person1.Entity));

                context.SaveChanges();

                // Act
                var queryHandler = new PaginatedShowsQueryHandler(context);
                var result       = await queryHandler.Handle(new PaginatedShowsQuery(1, 0), CancellationToken.None);

                // Assert
                Assert.AreEqual(1, result.First().Cast.Count);
            }
        }
 public void SaveChanges()
 {
     dbContext.SaveChanges();
 }
Exemple #6
0
        public virtual async Task ScrapeData()
        {
            Console.WriteLine("Started TvMazeScrape job");

            var latestShowId = ShowsRepository.GetLatestAddedShowId();
            int nextPage     = 0;

            if (latestShowId > 0)
            {
                decimal pageNr = latestShowId / 250;
                nextPage = (int)Math.Floor(pageNr);
            }

            var shows = await TvMazeApi.GetShows(nextPage);

            Console.WriteLine("Loaded 250 shows");

            foreach (var show in shows)
            {
                var existingShow = DbContext.Shows.Where(s => s.ShowId.Equals(show.Id)).FirstOrDefault();
                if (existingShow != null)
                {
                    continue;
                }

                var castResponse = await TvMazeApi.GetCastMembers((int)show.Id);

                Console.WriteLine("Received cast for show");

                var newShow = new Show()
                {
                    ShowId = (int)show.Id,
                    Name   = show.Name
                };

                await ShowsRepository.AddShow(newShow);

                HashSet <long> AddedPersons = new HashSet <long>();

                foreach (var castMember in castResponse)
                {
                    // Some shows contain dubplicates
                    if (AddedPersons.Contains(castMember.Person.Id))
                    {
                        continue;
                    }

                    var person = PersonRepository.CreateOrGet(new Person()
                    {
                        PersonId = (int)castMember.Person.Id,
                        Name     = castMember.Person.Name,
                        Birthday = castMember.Person.Birthday?.DateTime ?? new DateTime(1970, 1, 1)
                    });

                    var showPerson = new ShowPerson()
                    {
                        PersonId = person.PersonId,
                        ShowId   = newShow.ShowId
                    };
                    Console.WriteLine($"ShowId {newShow.ShowId}, PersonId {person.PersonId}");
                    DbContext.ShowPersons.Add(showPerson);
                    AddedPersons.Add(castMember.Person.Id);
                }
                DbContext.SaveChanges();

                Console.WriteLine($"Added show {newShow.Name}");

                System.Threading.Thread.Sleep(1000);
            }
        }