Пример #1
0
        public async void Given_IncorrectIdParameter_When_RequestIsMade_Then_EmptyJsonShouldBeReturned()
        {
            var data = await HackerNewsAPI.GetHackerNewsStoriesById(-1);

            String expected = "";

            Assert.Equal(expected, data.ToString());
        }
Пример #2
0
        public ActionResult Index()
        {
            var url     = ConfigurationManager.AppSettings["HackerNewsAPI"];
            var request = new HackerNewsAPI(new Uri(url));
            var data    = request.GetAllBestStoriesInfo();

            return(View(data));
        }
Пример #3
0
        public IActionResult Index()
        {
            HackerNewsAPI api     = new HackerNewsAPI();
            List <int>    ids     = api.GetTopTwentyIds();
            List <News>   Stories = new List <News>();

            for (int i = 0; i < 20; i++)
            {
                Stories.Add(api.GetStoryById(ids[i]));
            }
            return(View(Stories));
        }
Пример #4
0
        public async void Given_RequestForStoryItem_When_RequestIsMade_Then_StoryItemJsonReturned()
        {
            var data = await HackerNewsAPI.GetHackerNewsStoriesById(8863);

            String expected = "";

            using (StreamReader sr = new StreamReader(@"..\..\..\..\HackerNewsConsoleTests/Resources/id8863.txt"))
            {
                // Read the stream to a string, and write the string to the console.
                expected = sr.ReadToEnd();
            }

            Assert.Equal(expected, data.ToString());
        }
Пример #5
0
        static long Clone(long startID, bool shouldUpdate)
        {
            List <long> itemsArray = new List <long>();

            if (!shouldUpdate)
            {
                using (HackerNewsDB hackerNewsDB = new HackerNewsDB())
                {
                    itemsArray = hackerNewsDB.Items.Where(X => X.ID >= startID).Select(X => X.ID).ToList();
                    SetConsoleColor(ConsoleColor.White);
                    Console.WriteLine($"INFO: Retrieved {itemsArray.Count} IDs");
                }
            }
            else
            {
                SetConsoleColor(ConsoleColor.White);
                Console.WriteLine($"INFO: Entries Will Be Updated");
            }

            Console.WriteLine($"INFO: Starting At {startID} - Press Enter To Continue");
            Console.ReadKey(true);

            long maxID = HackerNewsAPI.GetMaxID();

            ParallelOptions parallelOptions = new ParallelOptions()
            {
                MaxDegreeOfParallelism = 12
            };

            Parallel.For(startID, maxID + 1, parallelOptions, itemID =>
            {
                bool itemRemoved = false;

                if (!itemsArray.Contains(itemID))
                {
                    using (HackerNewsDB hackerNewsDB = new HackerNewsDB())
                    {
                        try
                        {
                            if (shouldUpdate)
                            {
                                Item existingItem = hackerNewsDB.Items.FirstOrDefault(X => X.ID == itemID);
                                if (existingItem != null)
                                {
                                    hackerNewsDB.Items.Remove(existingItem);
                                    hackerNewsDB.SaveChanges();

                                    Item newItem = HackerNewsAPI.GetItem(itemID);
                                    hackerNewsDB.Items.Add(newItem);
                                    hackerNewsDB.SaveChanges();

                                    SetConsoleColor(ConsoleColor.Blue);
                                    Console.WriteLine($"DEBUG: Updated Item {itemID}");

                                    itemRemoved = true;
                                }
                            }

                            if (!itemRemoved)
                            {
                                Item newItem = HackerNewsAPI.GetItem(itemID);
                                hackerNewsDB.Items.Add(newItem);
                                hackerNewsDB.SaveChanges();

                                SetConsoleColor(ConsoleColor.Green);
                                Console.WriteLine($"DEBUG: Added Item {itemID}");
                            }
                        }
                        catch
                        {
                            SetConsoleColor(ConsoleColor.Red);
                            Console.WriteLine($"ERROR: Failed Item {itemID}");
                        }
                    }
                }
                else
                {
                    SetConsoleColor(ConsoleColor.Yellow);
                    Console.WriteLine($"DEBUG: Skipped Item {itemID}");
                }
            });

            return(maxID);
        }
Пример #6
0
        public HackerNewsUnitTest()
        {
            _HttpClient = new HttpClient();

            _hackerNewsAPI = new HackerNewsAPI(_HttpClient, new MemoryCache(new MemoryCacheOptions()));
        }
Пример #7
0
        public async void Given_RequestForTopHackerNewsStories_When_RequestIsMade_Then_JsonArrayOfTopStoriesReturned()
        {
            var data = await HackerNewsAPI.GetTopHackerNewsStoryIds();

            Assert.NotEmpty(data);
        }