Beispiel #1
0
        public Task Scrape(int pageNumber)
        {
            var client = new RestClient("http://api.tvmaze.com/");


            var request = new RestRequest("shows/" + pageNumber.ToString(), Method.GET);



            // execute the request
            IRestResponse response = client.Execute(request);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var         content = response.Content; // raw content as string
                TVShowModel show    = JsonConvert.DeserializeObject <TVShowModel>(content);

                //check if show exists in our database and add
                using (var db = new MovieContext())
                {
                    var check = db.Shows.FirstOrDefault(p => p.Name == show.name);
                    if (check == null)
                    {
                        check = new Show
                        {
                            Name       = show.name,
                            PageNumber = pageNumber
                        };
                        db.Shows.Add(check);

                        //get show cast from tv maze
                        var           castClient  = new RestClient("http://api.tvmaze.com/");
                        var           castRequest = new RestRequest("shows/" + pageNumber.ToString() + "/cast", Method.GET);
                        IRestResponse castRespnse = castClient.Execute(castRequest);
                        if (castRespnse.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            var castContent         = castRespnse.Content;
                            List <TVCastModel> cast = JsonConvert.DeserializeObject <List <TVCastModel> >(castContent);
                            foreach (var castItem in cast)
                            {
                                var tvcast = new Cast
                                {
                                    Birthday = castItem.person.birthday,
                                    Name     = castItem.person.name,
                                    ShowId   = check.Id
                                };
                                db.Casts.Add(tvcast);
                            }
                        }

                        //COMMIT DATABASE CHANGES
                        db.SaveChanges();
                    }
                }
            }

            return(Task.CompletedTask);
        }
        public void UpsertTVShow(TVShowModel tvShow)
        {
            tvShow.LastModificationDate = DateTime.Now;

            tvShowsCollection.ReplaceOne(
                new BsonDocument("_id", tvShow.Id),
                tvShow,
                new ReplaceOptions {
                IsUpsert = true
            });
        }
        public void Map_Maps_Cast_When_EmbeddedModel_Is_Null_Or_Empty()
        {
            // Arrange
            var instance = new TVShowModel
            {
                Embedded = null
            };

            var sut = CreateSut();

            // Act
            var actual = sut.Map(instance);

            // Assert
            Assert.Null(actual.Cast);
        }
        public void Map_Maps_Cast_When_CastModel_Is_Empty()
        {
            // Arrange
            var instance = new TVShowModel
            {
                Embedded = new EmbeddedModel
                {
                    Cast = new List <CastModel>().ToArray()
                }
            };

            var sut = CreateSut();

            // Act
            var actual = sut.Map(instance);

            // Assert
            Assert.Empty(actual.Cast);
        }
        public void Map_Maps_All_Properties_When_All_Needed_Properties_Arent_Null_Or_Empty()
        {
            // Arrange
            var expectedId   = 1;
            var expectedName = "expectedName";
            var expectedLastModificationDate = new DateTime(2020, 10, 30);
            var expectedCast = new Cast();

            var instance = new TVShowModel
            {
                Id   = expectedId,
                Name = expectedName,
                LastModificationDate = new DateTime(2020, 10, 30),
                Embedded             = new EmbeddedModel
                {
                    Cast = new List <CastModel> {
                        new CastModel()
                    }.ToArray()
                }
            };

            castMapperMock
            .Setup(mock => mock.Map(It.IsAny <CastModel>()))
            .Returns(expectedCast);


            var sut = CreateSut();

            // Act
            var actual = sut.Map(instance);

            // Assert
            Assert.Equal(expectedId, actual.Id);
            Assert.Equal(expectedName, actual.Name);
            Assert.NotNull(actual.Cast);
            Assert.Single(actual.Cast);
        }