├── README.md ├── .gitattributes ├── UniqueNumbersReaderFromFileOutputCvsFileWithExplanation ├── TestFiles │ ├── mixed 5.txt │ ├── all different.txt │ ├── only1 numbers.txt │ └── mixed 7.txt ├── WorkerService │ ├── AppSettings.cs │ ├── Properties │ │ └── launchSettings.json │ ├── appsettings.json │ ├── WorkerService.csproj │ ├── IReadContentService.cs │ ├── Program.cs │ └── Worker.cs └── WorkerService.sln ├── StarlingAndNumbersAnswer.docx ├── BookAPIReaderFromFileOutputCvsFile ├── TestFile │ └── ISBN_Input_File.txt ├── WorkerService │ ├── AppSettings.cs │ ├── Properties │ │ └── launchSettings.json │ ├── Models │ │ ├── DataRetrievalType.cs │ │ ├── BookContext.cs │ │ └── Book.cs │ ├── appsettings.json │ ├── Extensions │ │ └── EnumExtensionMethods.cs │ ├── WorkerService.csproj │ ├── IFileConstructionService.cs │ ├── Program.cs │ ├── IReadContentService.cs │ ├── IApiConsumerService.cs │ └── Worker.cs └── WorkerService.sln ├── LICENSE └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # NsChallenge 2 | 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /UniqueNumbersReaderFromFileOutputCvsFileWithExplanation/TestFiles/mixed 5.txt: -------------------------------------------------------------------------------- 1 | 1,4,5,1,1,5,3,2 -------------------------------------------------------------------------------- /UniqueNumbersReaderFromFileOutputCvsFileWithExplanation/TestFiles/all different.txt: -------------------------------------------------------------------------------- 1 | 1,2,3,4,5,6,7 -------------------------------------------------------------------------------- /UniqueNumbersReaderFromFileOutputCvsFileWithExplanation/TestFiles/only1 numbers.txt: -------------------------------------------------------------------------------- 1 | 1,1,1,1,1,1,1,1 -------------------------------------------------------------------------------- /UniqueNumbersReaderFromFileOutputCvsFileWithExplanation/TestFiles/mixed 7.txt: -------------------------------------------------------------------------------- 1 | 1,2,3,4,5,6,7,1,2,3,4,5,6,7 -------------------------------------------------------------------------------- /StarlingAndNumbersAnswer.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sgermosen/NsChallenge/HEAD/StarlingAndNumbersAnswer.docx -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/TestFile/ISBN_Input_File.txt: -------------------------------------------------------------------------------- 1 | 0201558025 2 | 0201558025,0984782869 3 | 0984782869,048641714X,0565095021 -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/AppSettings.cs: -------------------------------------------------------------------------------- 1 | namespace WorkerService 2 | { 3 | public class AppSettings 4 | { 5 | public string InputFolder { get; set; } 6 | public string OutputFolder { get; set; } 7 | public string ApiEndpoint { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "WorkerService": { 4 | "commandName": "Project", 5 | "dotnetRunMessages": true, 6 | "environmentVariables": { 7 | "DOTNET_ENVIRONMENT": "Development" 8 | } 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /UniqueNumbersReaderFromFileOutputCvsFileWithExplanation/WorkerService/AppSettings.cs: -------------------------------------------------------------------------------- 1 | namespace WorkerService 2 | { 3 | public class AppSettings 4 | { 5 | public string InputFolder { get; set; } 6 | public string OutputFolder { get; set; } 7 | public string ApiEndpoint { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/Models/DataRetrievalType.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel; 2 | 3 | namespace WorkerService.Models 4 | { 5 | public enum DataRetrievalType 6 | { 7 | [Description("Server")] 8 | Server = 1, 9 | [Description("Cache")] 10 | Cache = 2 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /UniqueNumbersReaderFromFileOutputCvsFileWithExplanation/WorkerService/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "WorkerService": { 4 | "commandName": "Project", 5 | "dotnetRunMessages": true, 6 | "environmentVariables": { 7 | "DOTNET_ENVIRONMENT": "Development" 8 | } 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "AppSettings": { 3 | "InputFolder": "C:\\temp", 4 | "OutputFolder": "C:\\temp2", 5 | "ApiEndpoint": "https://openlibrary.org/isbn" 6 | }, 7 | "Logging": { 8 | "LogLevel": { 9 | "Default": "Information", 10 | "Microsoft.Hosting.Lifetime": "Information" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /UniqueNumbersReaderFromFileOutputCvsFileWithExplanation/WorkerService/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "AppSettings": { 3 | "InputFolder": "C:\\temp", 4 | "OutputFolder": "C:\\temp2", 5 | "ApiEndpoint": "https://openlibrary.org/isbn" 6 | }, 7 | "Logging": { 8 | "LogLevel": { 9 | "Default": "Information", 10 | "Microsoft.Hosting.Lifetime": "Information" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/Models/BookContext.cs: -------------------------------------------------------------------------------- 1 | namespace WorkerService.Models 2 | { 3 | public class BookContext 4 | { 5 | public static readonly BookContext Instance = new BookContext(); 6 | 7 | public BookContext() 8 | { 9 | Books = new List(); 10 | } 11 | 12 | public List Books { get; set; } 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/Extensions/EnumExtensionMethods.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel; 2 | 3 | namespace WorkerService.Extensions 4 | { 5 | public static class EnumExtensionMethods 6 | { 7 | public static string GetEnumDescription(this Enum enumValue) 8 | { 9 | var fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); 10 | 11 | var descriptionAttributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); 12 | 13 | return descriptionAttributes.Length > 0 ? descriptionAttributes[0].Description : enumValue.ToString(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /UniqueNumbersReaderFromFileOutputCvsFileWithExplanation/WorkerService/WorkerService.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | dotnet-WorkerService-6D5E8DC4-6C1B-488B-AE73-72FCAEC0414A 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 sGermosenLw 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/WorkerService.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | dotnet-WorkerService-6D5E8DC4-6C1B-488B-AE73-72FCAEC0414A 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31903.59 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkerService", "WorkerService\WorkerService.csproj", "{B57D33A6-5555-4E19-8215-1F56A125CC20}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {B57D33A6-5555-4E19-8215-1F56A125CC20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {B57D33A6-5555-4E19-8215-1F56A125CC20}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {B57D33A6-5555-4E19-8215-1F56A125CC20}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {B57D33A6-5555-4E19-8215-1F56A125CC20}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {763ADAD5-7E8D-4496-BE3E-E010292DA722} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /UniqueNumbersReaderFromFileOutputCvsFileWithExplanation/WorkerService.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31903.59 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkerService", "WorkerService\WorkerService.csproj", "{B57D33A6-5555-4E19-8215-1F56A125CC20}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {B57D33A6-5555-4E19-8215-1F56A125CC20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {B57D33A6-5555-4E19-8215-1F56A125CC20}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {B57D33A6-5555-4E19-8215-1F56A125CC20}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {B57D33A6-5555-4E19-8215-1F56A125CC20}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {763ADAD5-7E8D-4496-BE3E-E010292DA722} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /UniqueNumbersReaderFromFileOutputCvsFileWithExplanation/WorkerService/IReadContentService.cs: -------------------------------------------------------------------------------- 1 | namespace WorkerService 2 | { 3 | public interface IReadContentService 4 | { 5 | void Run(string inputFile, string fileName); 6 | } 7 | 8 | public class ReadContentService : IReadContentService 9 | { 10 | private readonly ILogger _logger; 11 | 12 | public ReadContentService(ILogger logger ) 13 | { 14 | _logger = logger; 15 | } 16 | 17 | public void Run(string inputFile, string fileName) 18 | { 19 | _logger.LogInformation($"Start to Read file content: {fileName}"); 20 | StreamReader sStreamReader = new StreamReader($"{inputFile}"); 21 | string AllData = sStreamReader.ReadToEnd(); 22 | string[] strArray = AllData.Split(",".ToCharArray()); 23 | int[] rows = Array.ConvertAll(strArray, int.Parse); 24 | _logger.LogInformation("End of Read file content"); 25 | 26 | var differents = rows.Select(p => p.ToString()).Distinct(); 27 | if (differents.Count() != rows.Count() && differents.Count() == 1) 28 | Console.WriteLine($"All the values are the same, therefore, there is only one unique input value: {string.Join(",", differents)}"); 29 | else if (differents.Count() != rows.Count()) 30 | Console.WriteLine($"The only unique values are: {string.Join(",", differents)} and the output order does not matter"); 31 | else 32 | Console.WriteLine($"Entire input file is unique, therefore the output will be the same: {string.Join(",", differents)}"); 33 | 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/IFileConstructionService.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Options; 2 | using Newtonsoft.Json.Linq; 3 | using System; 4 | using WorkerService.Models; 5 | using System.ComponentModel; 6 | using WorkerService.Extensions; 7 | using System.IO; 8 | 9 | namespace WorkerService 10 | { 11 | public interface IFileConstructionService 12 | { 13 | void Run(List items, string fileName); 14 | } 15 | 16 | public class FileConstructionService : IFileConstructionService 17 | { 18 | private readonly ILogger _logger; 19 | private readonly string _outputFolder; 20 | 21 | public FileConstructionService(ILogger logger, IOptions settings) 22 | { 23 | _logger = logger; 24 | _outputFolder = settings.Value.OutputFolder; 25 | } 26 | 27 | public async void Run(List items, string fileName) 28 | { 29 | _logger.LogInformation("Start Create the File"); 30 | using (StreamWriter writer = new StreamWriter($"{_outputFolder}\\{fileName}.csv")) 31 | { 32 | writer.WriteLine($"Row Number,Data Retrieval Type,ISBN,Title,Subtitle,Author Name(s),Number of Pages,Publish Date"); 33 | int rowNum = 0; 34 | foreach (var item in items) 35 | { 36 | writer.WriteLine($"{++rowNum},{item.DataRetrievalType.GetEnumDescription()},{item.Isbn},{item.title},{item.subtitle},{string.Join("; ", item.authors.Select(u => u.key))},{item.number_of_pages},{item.publish_date}"); 37 | } 38 | } 39 | _logger.LogInformation("End File creation"); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/Models/Book.cs: -------------------------------------------------------------------------------- 1 | namespace WorkerService.Models 2 | { 3 | public class Author 4 | { 5 | public string key { get; set; } 6 | } 7 | 8 | public class Created 9 | { 10 | public string type { get; set; } 11 | public DateTime value { get; set; } 12 | } 13 | 14 | public class LastModified 15 | { 16 | public string type { get; set; } 17 | public DateTime value { get; set; } 18 | } 19 | 20 | public class Book 21 | { 22 | public Book() 23 | { 24 | } 25 | public string Isbn { get; set; } 26 | public string number_of_pages; 27 | public DataRetrievalType? DataRetrievalType { get; set; } 28 | public Type type { get; set; } 29 | public string title { get; set; } 30 | public List authors { get; set; } 31 | public string publish_date { get; set; } 32 | public List source_records { get; set; } 33 | public List publishers { get; set; } 34 | public List isbn_10 { get; set; } 35 | public List isbn_13 { get; set; } 36 | public string physical_format { get; set; } 37 | public string full_title { get; set; } 38 | public string subtitle { get; set; } 39 | public List covers { get; set; } 40 | public List works { get; set; } 41 | public string key { get; set; } 42 | public int latest_revision { get; set; } 43 | public int revision { get; set; } 44 | public Created created { get; set; } 45 | public LastModified last_modified { get; set; } 46 | } 47 | 48 | public class Type 49 | { 50 | public string key { get; set; } 51 | } 52 | 53 | public class Work 54 | { 55 | public string key { get; set; } 56 | } 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /UniqueNumbersReaderFromFileOutputCvsFileWithExplanation/WorkerService/Program.cs: -------------------------------------------------------------------------------- 1 | using Serilog; 2 | using Serilog.Enrichers; 3 | using Serilog.Events; 4 | using Serilog.Sinks.SystemConsole.Themes; 5 | using WorkerService; 6 | 7 | IHost host = Host.CreateDefaultBuilder(args) 8 | .UseWindowsService() 9 | .ConfigureAppConfiguration((context, config) => 10 | { 11 | // configure the app here. 12 | }) 13 | .ConfigureServices((hostContext, services) => 14 | { 15 | services.AddHostedService(); 16 | services.Configure(hostContext.Configuration.GetSection("AppSettings")); 17 | services.AddScoped(); 18 | }) 19 | .Build(); 20 | 21 | const string loggerTemplate = @"{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u4}]<{ThreadId}> [{SourceContext:l}] {Message:lj}{NewLine}{Exception}"; 22 | var baseDir = AppDomain.CurrentDomain.BaseDirectory; 23 | var logfile = Path.Combine(baseDir, "App_Data", "logs", "log.txt"); 24 | Log.Logger = new LoggerConfiguration() 25 | .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) 26 | .Enrich.With(new ThreadIdEnricher()) 27 | .Enrich.FromLogContext() 28 | .WriteTo.Console(LogEventLevel.Information, loggerTemplate, theme: AnsiConsoleTheme.Literate) 29 | .WriteTo.File(logfile, LogEventLevel.Information, loggerTemplate, 30 | rollingInterval: RollingInterval.Day, retainedFileCountLimit: 90) 31 | .CreateLogger(); 32 | 33 | try 34 | { 35 | Log.Information("===================================================================="); 36 | Log.Information($"Application Starts. Version: {System.Reflection.Assembly.GetEntryAssembly()?.GetName().Version}"); 37 | Log.Information($"Application Directory: {AppDomain.CurrentDomain.BaseDirectory}"); 38 | await host.RunAsync(); 39 | } 40 | catch (Exception e) 41 | { 42 | Log.Fatal(e, "Application terminated unexpectedly"); 43 | } 44 | finally 45 | { 46 | Log.Information("====================================================================\r\n"); 47 | Log.CloseAndFlush(); 48 | } 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/Program.cs: -------------------------------------------------------------------------------- 1 | using Serilog; 2 | using Serilog.Enrichers; 3 | using Serilog.Events; 4 | using Serilog.Sinks.SystemConsole.Themes; 5 | using WorkerService; 6 | 7 | IHost host = Host.CreateDefaultBuilder(args) 8 | .UseWindowsService() 9 | .ConfigureAppConfiguration((context, config) => 10 | { 11 | // configure the app here. 12 | }) 13 | .ConfigureServices((hostContext, services) => 14 | { 15 | services.AddHostedService(); 16 | services.Configure(hostContext.Configuration.GetSection("AppSettings")); 17 | services.AddScoped(); 18 | services.AddScoped(); 19 | services.AddScoped(); 20 | }) 21 | .Build(); 22 | 23 | const string loggerTemplate = @"{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level:u4}]<{ThreadId}> [{SourceContext:l}] {Message:lj}{NewLine}{Exception}"; 24 | var baseDir = AppDomain.CurrentDomain.BaseDirectory; 25 | var logfile = Path.Combine(baseDir, "App_Data", "logs", "log.txt"); 26 | Log.Logger = new LoggerConfiguration() 27 | .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) 28 | .Enrich.With(new ThreadIdEnricher()) 29 | .Enrich.FromLogContext() 30 | .WriteTo.Console(LogEventLevel.Information, loggerTemplate, theme: AnsiConsoleTheme.Literate) 31 | .WriteTo.File(logfile, LogEventLevel.Information, loggerTemplate, 32 | rollingInterval: RollingInterval.Day, retainedFileCountLimit: 90) 33 | .CreateLogger(); 34 | 35 | try 36 | { 37 | Log.Information("===================================================================="); 38 | Log.Information($"Application Starts. Version: {System.Reflection.Assembly.GetEntryAssembly()?.GetName().Version}"); 39 | Log.Information($"Application Directory: {AppDomain.CurrentDomain.BaseDirectory}"); 40 | await host.RunAsync(); 41 | } 42 | catch (Exception e) 43 | { 44 | Log.Fatal(e, "Application terminated unexpectedly"); 45 | } 46 | finally 47 | { 48 | Log.Information("====================================================================\r\n"); 49 | Log.CloseAndFlush(); 50 | } 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/IReadContentService.cs: -------------------------------------------------------------------------------- 1 | using WorkerService.Models; 2 | 3 | namespace WorkerService 4 | { 5 | public interface IReadContentService 6 | { 7 | void Run(string inputFile, string fileName); 8 | } 9 | 10 | public class ReadContentService : IReadContentService 11 | { 12 | private readonly ILogger _logger; 13 | private readonly IApiConsumerService _apiConsumerService; 14 | private readonly IFileConstructionService _fileConstructionService; 15 | 16 | public ReadContentService(ILogger logger, IApiConsumerService apiConsumerService, IFileConstructionService fileConstructionService) 17 | { 18 | _logger = logger; 19 | _apiConsumerService = apiConsumerService; 20 | _fileConstructionService = fileConstructionService; 21 | } 22 | 23 | public void Run(string inputFile, string fileName) 24 | { 25 | _logger.LogInformation($"Start to Read file content: {fileName}"); 26 | StreamReader sStreamReader = new StreamReader($"{inputFile}"); 27 | // string AllData = sStreamReader.ReadToEnd(); 28 | //string[] rows;// =; AllData.Split(",".ToCharArray()); 29 | string[] lines = File.ReadAllLines(inputFile); 30 | List rows = new List(); 31 | foreach (string line in lines) 32 | { 33 | rows.AddRange(line.Split(',')); 34 | } 35 | _logger.LogInformation("End of Read file content"); 36 | var existingBooks = BookContext.Instance.Books; 37 | var books = new List(); 38 | foreach (string row in rows) 39 | { 40 | Book book; 41 | var exist = existingBooks.Any(p => p.Isbn == row); 42 | if (exist) 43 | { 44 | book = existingBooks.FirstOrDefault(p => p.Isbn == row); 45 | book.DataRetrievalType = DataRetrievalType.Cache; 46 | } 47 | else 48 | { 49 | book = _apiConsumerService.GetFromApi(row, fileName).Result; 50 | if (book != null) 51 | { 52 | BookContext.Instance.Books.Add(book); 53 | } 54 | } 55 | if (book != null) 56 | { 57 | books.Add(book); 58 | } 59 | } 60 | // _apiConsumerService.Run(rows, fileName); 61 | _fileConstructionService.Run(books, fileName); 62 | 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/IApiConsumerService.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Options; 2 | using Newtonsoft.Json; 3 | using WorkerService.Models; 4 | 5 | namespace WorkerService 6 | { 7 | public interface IApiConsumerService 8 | { 9 | Task GetFromApi(string item, string fileName); 10 | void Run(string[] items, string fileName); 11 | 12 | } 13 | 14 | public class ApiConsumerService : IApiConsumerService 15 | { 16 | private readonly ILogger _logger; 17 | private readonly string _apiEndpoint; 18 | 19 | public ApiConsumerService(ILogger logger, IOptions settings) 20 | { 21 | _logger = logger; 22 | _apiEndpoint = settings.Value.ApiEndpoint; 23 | } 24 | public async Task GetFromApi(string item, string fileName) 25 | { 26 | _logger.LogInformation("Start call the API"); 27 | using (var httpClient = new HttpClient()) 28 | { 29 | var response = await httpClient.GetAsync($"{_apiEndpoint}/{item}.json"); 30 | if (response.IsSuccessStatusCode && response.Content.Headers.ContentLength > 0) 31 | { 32 | var element = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); 33 | element.DataRetrievalType = DataRetrievalType.Server; 34 | element.Isbn = item; 35 | return element; 36 | } 37 | else 38 | _logger.LogError($"Problem retreaving data from: {item}"); 39 | } 40 | _logger.LogInformation("End call the API"); 41 | return null; 42 | } 43 | public async void Run(string[] items, string fileName) 44 | { 45 | _logger.LogInformation("Start call the API"); 46 | var books = new List(); 47 | using (var httpClient = new HttpClient()) 48 | { 49 | foreach (var item in items) 50 | { 51 | var response = await httpClient.GetAsync($"{_apiEndpoint}/{item}.json"); 52 | if (response.IsSuccessStatusCode && response.Content.Headers.ContentLength > 0) 53 | { 54 | // httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {AccessToken}"); 55 | // httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 56 | var element = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); 57 | element.DataRetrievalType = DataRetrievalType.Server; 58 | element.Isbn = item; 59 | books.Add(element); 60 | } 61 | else 62 | _logger.LogError($"Problem retreaving data from: {item}"); 63 | } 64 | } 65 | if (books.Any()) 66 | BookContext.Instance.Books.AddRange(books); 67 | _logger.LogInformation("End call the API"); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /BookAPIReaderFromFileOutputCvsFile/WorkerService/Worker.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Options; 2 | 3 | namespace WorkerService 4 | { 5 | public class Worker : BackgroundService 6 | { 7 | private readonly ILogger _logger; 8 | private FileSystemWatcher _folderWatcher; 9 | private readonly string _inputFolder; 10 | private readonly IServiceProvider _services; 11 | 12 | public Worker(ILogger logger, IOptions settings, IServiceProvider services) 13 | { 14 | _logger = logger; 15 | _inputFolder = settings.Value.InputFolder; 16 | _services = services; 17 | } 18 | 19 | protected override async Task ExecuteAsync(CancellationToken stoppingToken) 20 | { 21 | await Task.CompletedTask; 22 | } 23 | 24 | public override Task StartAsync(CancellationToken cancellationToken) 25 | { 26 | _logger.LogInformation("Service Starting"); 27 | if (!Directory.Exists(_inputFolder)) 28 | { 29 | _logger.LogWarning($"Please make sure the InputFolder [{_inputFolder}] exists, then restart the service."); 30 | return Task.CompletedTask; 31 | } 32 | 33 | _logger.LogInformation($"Binding Events from Input Folder: {_inputFolder}"); 34 | _folderWatcher = new FileSystemWatcher(_inputFolder, "*.TXT") 35 | { 36 | NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName | 37 | NotifyFilters.DirectoryName 38 | }; 39 | _folderWatcher.Created += Input_OnChanged; 40 | _folderWatcher.EnableRaisingEvents = true; 41 | 42 | return base.StartAsync(cancellationToken); 43 | } 44 | 45 | protected void Input_OnChanged(object source, FileSystemEventArgs e) 46 | { 47 | if (e.ChangeType == WatcherChangeTypes.Created) 48 | { 49 | _logger.LogInformation($"InBound Change Event Triggered by [{e.FullPath}]"); 50 | 51 | // do some work 52 | using (var scope = _services.CreateScope()) 53 | { 54 | var readContent = scope.ServiceProvider.GetRequiredService(); 55 | readContent.Run(e.FullPath, e.Name); 56 | } 57 | _logger.LogInformation("Done with Inbound Change Event"); 58 | } 59 | } 60 | 61 | public override async Task StopAsync(CancellationToken cancellationToken) 62 | { 63 | _logger.LogInformation("Stopping Service"); 64 | _folderWatcher.EnableRaisingEvents = false; 65 | await base.StopAsync(cancellationToken); 66 | } 67 | 68 | public override void Dispose() 69 | { 70 | _logger.LogInformation("Disposing Service"); 71 | _folderWatcher.Dispose(); 72 | base.Dispose(); 73 | } 74 | } 75 | //public class Worker : BackgroundService 76 | //{ 77 | // private readonly ILogger _logger; 78 | 79 | // public Worker(ILogger logger) 80 | // { 81 | // _logger = logger; 82 | // } 83 | 84 | // protected override async Task ExecuteAsync(CancellationToken stoppingToken) 85 | // { 86 | // while (!stoppingToken.IsCancellationRequested) 87 | // { 88 | // _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); 89 | // await Task.Delay(1000, stoppingToken); 90 | // } 91 | // } 92 | //} 93 | } -------------------------------------------------------------------------------- /UniqueNumbersReaderFromFileOutputCvsFileWithExplanation/WorkerService/Worker.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Options; 2 | 3 | namespace WorkerService 4 | { 5 | public class Worker : BackgroundService 6 | { 7 | private readonly ILogger _logger; 8 | private FileSystemWatcher _folderWatcher; 9 | private readonly string _inputFolder; 10 | private readonly IServiceProvider _services; 11 | 12 | public Worker(ILogger logger, IOptions settings, IServiceProvider services) 13 | { 14 | _logger = logger; 15 | _inputFolder = settings.Value.InputFolder; 16 | _services = services; 17 | } 18 | 19 | protected override async Task ExecuteAsync(CancellationToken stoppingToken) 20 | { 21 | await Task.CompletedTask; 22 | } 23 | 24 | public override Task StartAsync(CancellationToken cancellationToken) 25 | { 26 | _logger.LogInformation("Service Starting"); 27 | if (!Directory.Exists(_inputFolder)) 28 | { 29 | _logger.LogWarning($"Please make sure the InputFolder [{_inputFolder}] exists, then restart the service."); 30 | return Task.CompletedTask; 31 | } 32 | 33 | _logger.LogInformation($"Binding Events from Input Folder: {_inputFolder}"); 34 | _folderWatcher = new FileSystemWatcher(_inputFolder, "*.TXT") 35 | { 36 | NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.FileName | 37 | NotifyFilters.DirectoryName 38 | }; 39 | _folderWatcher.Created += Input_OnChanged; 40 | _folderWatcher.EnableRaisingEvents = true; 41 | 42 | return base.StartAsync(cancellationToken); 43 | } 44 | 45 | protected void Input_OnChanged(object source, FileSystemEventArgs e) 46 | { 47 | if (e.ChangeType == WatcherChangeTypes.Created) 48 | { 49 | _logger.LogInformation($"InBound Change Event Triggered by [{e.FullPath}]"); 50 | 51 | // do some work 52 | using (var scope = _services.CreateScope()) 53 | { 54 | var readContent = scope.ServiceProvider.GetRequiredService(); 55 | readContent.Run(e.FullPath, e.Name); 56 | } 57 | _logger.LogInformation("Done with Inbound Change Event"); 58 | } 59 | } 60 | 61 | public override async Task StopAsync(CancellationToken cancellationToken) 62 | { 63 | _logger.LogInformation("Stopping Service"); 64 | _folderWatcher.EnableRaisingEvents = false; 65 | await base.StopAsync(cancellationToken); 66 | } 67 | 68 | public override void Dispose() 69 | { 70 | _logger.LogInformation("Disposing Service"); 71 | _folderWatcher.Dispose(); 72 | base.Dispose(); 73 | } 74 | } 75 | //public class Worker : BackgroundService 76 | //{ 77 | // private readonly ILogger _logger; 78 | 79 | // public Worker(ILogger logger) 80 | // { 81 | // _logger = logger; 82 | // } 83 | 84 | // protected override async Task ExecuteAsync(CancellationToken stoppingToken) 85 | // { 86 | // while (!stoppingToken.IsCancellationRequested) 87 | // { 88 | // _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); 89 | // await Task.Delay(1000, stoppingToken); 90 | // } 91 | // } 92 | //} 93 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml 399 | --------------------------------------------------------------------------------