Exemple #1
0
        /// <summary>
        /// Trigger the build and output of Posts to the STDOUT stream
        /// </summary>
        public void Handle(Options options, IConsoleStream consoleStream)
        {
            // Validate that the number of Posts does not exceed 100
            if (options.Posts < 1 || options.Posts > 100)
            {
                consoleStream.WriteErrorLine("Number of posts must be a positive integer not exceeding 100");
                return;
            }

            var postSerializer = new JsonSerializer();

            consoleStream.JsonWriter.Formatting = Formatting.Indented;
            int postCounter = 1;

            // Write the start of the Json Array to the STDOUT stream
            consoleStream.JsonWriter.WriteStartArray();

            // Write each post to the Array in the STDOUT stream
            // This continues to use Linq Lazy Evaluation to write one valid post to the STDOUT stream
            // at a time
            var posts = service.GetTopPosts(consoleStream)
                        .Where(PostValidator.Validate)
                        .Where(p => SerializePost(postSerializer, consoleStream, p, postCounter++))
                        .Take(options.Posts)
                        .ToList();

            // Write the end of the Json Array to the STDOUT stream
            consoleStream.JsonWriter.WriteEndArray();
        }