From a738ad10548a67ff5ee1a71b29ca99708343aba2 Mon Sep 17 00:00:00 2001 From: Mguy13 Date: Wed, 4 Jan 2023 18:53:48 +0100 Subject: [PATCH 1/8] skeleton done --- DemoApplication.csproj | 5 ++ OrderParser.cs | 0 Program.cs | 23 +++++- assets/input2.json | 4 +- data/dtos/order_dto.cs | 34 +++++++++ models/OrderModel.cs | 17 +++++ services/CsvWriter.cs | 22 ++++++ services/DemoApplicationConfiguration.cs | 92 ++++++++++++++++++++++++ 8 files changed, 193 insertions(+), 4 deletions(-) delete mode 100644 OrderParser.cs create mode 100644 data/dtos/order_dto.cs create mode 100644 models/OrderModel.cs create mode 100644 services/CsvWriter.cs create mode 100644 services/DemoApplicationConfiguration.cs diff --git a/DemoApplication.csproj b/DemoApplication.csproj index d439800..5e60f9c 100644 --- a/DemoApplication.csproj +++ b/DemoApplication.csproj @@ -7,4 +7,9 @@ enable + + + + + diff --git a/OrderParser.cs b/OrderParser.cs deleted file mode 100644 index e69de29..0000000 diff --git a/Program.cs b/Program.cs index a4cc54d..de760b5 100644 --- a/Program.cs +++ b/Program.cs @@ -1,10 +1,29 @@ -namespace OrderParser +using Configuration; +using DTO; +using static Configuration.DemoApplicationConfiguration; + +namespace OrderParser { class Program { static void Main(string[] args) { - Console.WriteLine(string.Join(" ", args)); + try + { + var filesInfoConfig = DemoApplicationConfiguration.InitializeFrom(args); + var csvFileWriter = new CsvFileWriter(filesInfoConfig.OutputFile); + + foreach (var jsonFile in filesInfoConfig.JsonFilesInfo) + { + var orderModel = new OrderModel(Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText(jsonFile.FullName))); + + csvFileWriter.writeOutRow(orderModel); + } + } + catch (DemoApplicationConfigurationException e) + { + Console.WriteLine(e.Message); + } } } } \ No newline at end of file diff --git a/assets/input2.json b/assets/input2.json index 2a0701f..7e51129 100644 --- a/assets/input2.json +++ b/assets/input2.json @@ -6,13 +6,13 @@ "ProductId": "PID3", "Description": "pid3 description", "Amount": 1.5, - "price": 19.5 + "price": 42.0 }, { "ProductId": "PID4", "Description": "pid4 description", "Amount": 2.5, - "price": 12.1 + "price": 169.0 } ] } \ No newline at end of file diff --git a/data/dtos/order_dto.cs b/data/dtos/order_dto.cs new file mode 100644 index 0000000..22e8c70 --- /dev/null +++ b/data/dtos/order_dto.cs @@ -0,0 +1,34 @@ +namespace DTO +{ + using Newtonsoft.Json; + + public partial class OrderDto + { + [JsonProperty("OrderId")] + public virtual long OrderId { get; set; } + + [JsonProperty("Description")] + public virtual string Description { get; set; } + + [JsonProperty("customer ID")] + public virtual long CustomerId { get; set; } + + [JsonProperty("products")] + public virtual Product[] Products { get; set; } + } + + public partial class Product + { + [JsonProperty("ProductId")] + public virtual string ProductId { get; set; } + + [JsonProperty("Description")] + public virtual string Description { get; set; } + + [JsonProperty("Amount")] + public virtual double Amount { get; set; } + + [JsonProperty("price")] + public virtual double Price { get; set; } + } +} diff --git a/models/OrderModel.cs b/models/OrderModel.cs new file mode 100644 index 0000000..62bb0ba --- /dev/null +++ b/models/OrderModel.cs @@ -0,0 +1,17 @@ +using DTO; + +public sealed class OrderModel { + public long OrderId { get; set; } + public string OrderDescription { get; set; } + + public long CustomerId { get; set; } + + public double TotalPrice { get; set; } + + public OrderModel(OrderDto orderDTO) { + OrderId = orderDTO.OrderId; + OrderDescription = orderDTO.Description; + CustomerId = orderDTO.CustomerId; + TotalPrice = orderDTO.Products.Select(product => product.Price).Sum(); + } +} \ No newline at end of file diff --git a/services/CsvWriter.cs b/services/CsvWriter.cs new file mode 100644 index 0000000..5d655db --- /dev/null +++ b/services/CsvWriter.cs @@ -0,0 +1,22 @@ +using System.Globalization; +using CsvHelper; + +class CsvFileWriter +{ + private TextWriter textWriter; + private CsvWriter csvWriter; + + public CsvFileWriter(FileInfo fileInfo) { + textWriter = new StreamWriter(fileInfo.FullName); + csvWriter = new CsvWriter(textWriter, CultureInfo.InvariantCulture); + } + + public void writeOutRow(OrderModel orderModel) { + csvWriter.WriteField(orderModel.OrderId); + csvWriter.WriteField(orderModel.OrderDescription); + csvWriter.WriteField(orderModel.CustomerId); + csvWriter.WriteField(orderModel.TotalPrice); + + csvWriter.NextRecord(); + } +} \ No newline at end of file diff --git a/services/DemoApplicationConfiguration.cs b/services/DemoApplicationConfiguration.cs new file mode 100644 index 0000000..f16473c --- /dev/null +++ b/services/DemoApplicationConfiguration.cs @@ -0,0 +1,92 @@ +namespace Configuration +{ + public sealed class DemoApplicationConfiguration + { + private FileInfo[] jsonFiles; + public FileInfo[] JsonFilesInfo + { + get { return jsonFiles; } + private set + { + jsonFiles = value.Reverse().ToArray(); + } + } + private FileInfo outputFile; + public FileInfo OutputFile{ + get {return outputFile;} + private set + { + if (!canCreateFile(value.FullName)) + throw new DemoApplicationConfigurationException($"Given output name '{value.FullName}' cannot be used"); + outputFile = value; + } + } + + public static DemoApplicationConfiguration InitializeFrom(string[] args) + { + if (args.Length < 3) + throw new DemoApplicationConfigurationException("Must have atleast: 1 flag, 1 path, 1 output filename"); + + var outputFileName = new FileInfo(args.Last()); + + // Remove last file-name arg, to get a more consistent array + args = args.SkipLast(1).ToArray(); + + + string[] givenFlags = args.Where((value, index) => index % 2 == 0).ToArray(); + string[] givenFilePaths = args.Where((value, index) => index % 2 != 0).ToArray(); + + try + { + if ( givenFlags.Length == 1 && givenFlags[0].Equals("-d") ) { + string[] filesInDirectory = Directory.GetFiles(givenFilePaths[0]); + return new DemoApplicationConfiguration { + OutputFile = outputFileName, + JsonFilesInfo = filesInDirectory.Select(name => new FileInfo(name)).ToArray(), + }; + } + else if (givenFlags.All((string flag) => flag.Equals("-f"))) { + return new DemoApplicationConfiguration { + OutputFile = outputFileName, + JsonFilesInfo = givenFilePaths.Select(name => new FileInfo(name)).ToArray(), + }; + } + else { + throw new DemoApplicationConfigurationException("Must have either 1 flag that is '-d', of all flags must be '-f"); + } + } + catch (System.Exception e) + { + Console.WriteLine("Something else went wrong"); + throw; + } + } + + + private static bool canCreateFile(string filename) { + try + { + using (File.Create(filename)) { + File.Delete(filename); + return true; + } + } + catch + { + return false; + } + } + + [Serializable] + internal sealed class DemoApplicationConfigurationException : Exception + { + public DemoApplicationConfigurationException() + { + } + + public DemoApplicationConfigurationException(string? message) : base(message) + { + } + } + } +} \ No newline at end of file From 1170c76e3faec5d9bf0ca84d044e25e1be0d464f Mon Sep 17 00:00:00 2001 From: Mguy13 Date: Wed, 4 Jan 2023 19:47:37 +0100 Subject: [PATCH 2/8] works! --- .vscode/launch.json | 4 +++- assets/input1.json | 2 +- assets/input2.json | 2 +- data/dtos/{order_dto.cs => OrderDTO.cs} | 0 models/OrderModel.cs | 2 +- services/CsvWriter.cs | 2 ++ services/DemoApplicationConfiguration.cs | 15 ++++++++------- 7 files changed, 16 insertions(+), 11 deletions(-) rename data/dtos/{order_dto.cs => OrderDTO.cs} (100%) diff --git a/.vscode/launch.json b/.vscode/launch.json index 87c338c..c646cea 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,6 +1,8 @@ { "version": "0.2.0", "configurations": [ + + { // Use IntelliSense to find out which attributes exist for C# debugging @@ -12,7 +14,7 @@ "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceFolder}/bin/Debug/net7.0/DemoApplication.dll", - "args": [], + "args": ["-f", "assets/input1.json", "-f", "assets/input2.json", "out.csv"], "cwd": "${workspaceFolder}", // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console "console": "internalConsole", diff --git a/assets/input1.json b/assets/input1.json index a373f31..b4c8b82 100644 --- a/assets/input1.json +++ b/assets/input1.json @@ -1,6 +1,6 @@ { "OrderId": 1, - "Description": " Order 123", + "Description": "Order 123", "customer ID": 123, "products": [{ "ProductId": "PID1", diff --git a/assets/input2.json b/assets/input2.json index 7e51129..c2173cf 100644 --- a/assets/input2.json +++ b/assets/input2.json @@ -1,6 +1,6 @@ { "OrderId": 3, - "Description": " Order 11234", + "Description": "Order 11234", "customer ID": 1134, "products": [{ "ProductId": "PID3", diff --git a/data/dtos/order_dto.cs b/data/dtos/OrderDTO.cs similarity index 100% rename from data/dtos/order_dto.cs rename to data/dtos/OrderDTO.cs diff --git a/models/OrderModel.cs b/models/OrderModel.cs index 62bb0ba..783a9ce 100644 --- a/models/OrderModel.cs +++ b/models/OrderModel.cs @@ -10,7 +10,7 @@ public sealed class OrderModel { public OrderModel(OrderDto orderDTO) { OrderId = orderDTO.OrderId; - OrderDescription = orderDTO.Description; + OrderDescription = orderDTO.Description.Trim(); CustomerId = orderDTO.CustomerId; TotalPrice = orderDTO.Products.Select(product => product.Price).Sum(); } diff --git a/services/CsvWriter.cs b/services/CsvWriter.cs index 5d655db..4b9ca0f 100644 --- a/services/CsvWriter.cs +++ b/services/CsvWriter.cs @@ -8,6 +8,7 @@ class CsvFileWriter public CsvFileWriter(FileInfo fileInfo) { textWriter = new StreamWriter(fileInfo.FullName); + csvWriter = new CsvWriter(textWriter, CultureInfo.InvariantCulture); } @@ -17,6 +18,7 @@ class CsvFileWriter csvWriter.WriteField(orderModel.CustomerId); csvWriter.WriteField(orderModel.TotalPrice); + csvWriter.Flush(); csvWriter.NextRecord(); } } \ No newline at end of file diff --git a/services/DemoApplicationConfiguration.cs b/services/DemoApplicationConfiguration.cs index f16473c..8e2049c 100644 --- a/services/DemoApplicationConfiguration.cs +++ b/services/DemoApplicationConfiguration.cs @@ -30,14 +30,15 @@ namespace Configuration var outputFileName = new FileInfo(args.Last()); // Remove last file-name arg, to get a more consistent array - args = args.SkipLast(1).ToArray(); + var strippedArgs = args.SkipLast(1).ToArray(); - string[] givenFlags = args.Where((value, index) => index % 2 == 0).ToArray(); - string[] givenFilePaths = args.Where((value, index) => index % 2 != 0).ToArray(); + string[] givenFlags = strippedArgs.Where((value, index) => index % 2 == 0).ToArray(); + string[] givenFilePaths = strippedArgs.Where((value, index) => index % 2 != 0).ToArray(); try { + // Assert only a single 'd' was passed if ( givenFlags.Length == 1 && givenFlags[0].Equals("-d") ) { string[] filesInDirectory = Directory.GetFiles(givenFilePaths[0]); return new DemoApplicationConfiguration { @@ -45,6 +46,7 @@ namespace Configuration JsonFilesInfo = filesInDirectory.Select(name => new FileInfo(name)).ToArray(), }; } + // Assert only '-f' were passed else if (givenFlags.All((string flag) => flag.Equals("-f"))) { return new DemoApplicationConfiguration { OutputFile = outputFileName, @@ -52,12 +54,11 @@ namespace Configuration }; } else { - throw new DemoApplicationConfigurationException("Must have either 1 flag that is '-d', of all flags must be '-f"); + throw new DemoApplicationConfigurationException("Must have either 1 flag that is '-d', or all flags must be '-f"); } - } - catch (System.Exception e) + } catch (Exception) { - Console.WriteLine("Something else went wrong"); + Console.WriteLine("Something went wrong"); throw; } } From 06332d76eb0a54d0fe5408b1a742f968043c2529 Mon Sep 17 00:00:00 2001 From: Mguy13 Date: Wed, 4 Jan 2023 23:32:29 +0100 Subject: [PATCH 3/8] review --- .gitignore | 265 +++++++++++++++++++++++++++++++++++++++++++++++++++- Problems.md | 27 ++++++ Program.cs | 4 +- README.md | 0 review.cs | 244 +++++++++++++++++++++++------------------------ 5 files changed, 416 insertions(+), 124 deletions(-) create mode 100644 Problems.md create mode 100644 README.md diff --git a/.gitignore b/.gitignore index d6c8a08..5806ae5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Created by https://www.toptal.com/developers/gitignore/api/csharp,dotnetcore,visualstudiocode,visualstudio,rider +# Edit at https://www.toptal.com/developers/gitignore?templates=csharp,dotnetcore,visualstudiocode,visualstudio,rider + ### Csharp ### ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. @@ -405,6 +408,85 @@ obj/ /node_modules /wwwroot/node_modules +### Rider ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + ### VisualStudioCode ### !.vscode/*.code-snippets @@ -418,4 +500,185 @@ obj/ .history .ionide -# End of https://www.toptal.com/developers/gitignore/api/csharp,visualstudiocode,dotnetcore \ No newline at end of file +### VisualStudio ### + +# User-specific files + +# User-specific files (MonoDevelop/Xamarin Studio) + +# Mono auto generated files + +# Build results + +# Visual Studio 2015/2017 cache/options directory +# Uncomment if you have tasks that create the project's static files in wwwroot + +# Visual Studio 2017 auto generated files + +# MSTest test Results + +# NUnit + +# Build Results of an ATL Project + +# Benchmark Results + +# .NET Core + +# ASP.NET Scaffolding + +# StyleCop + +# Files built by Visual Studio + +# Chutzpah Test files + +# Visual C++ cache files + +# Visual Studio profiler + +# Visual Studio Trace Files + +# TFS 2012 Local Workspace + +# Guidance Automation Toolkit + +# ReSharper is a .NET coding add-in + +# TeamCity is a build add-in + +# DotCover is a Code Coverage Tool + +# AxoCover is a Code Coverage Tool + +# Coverlet is a free, cross platform Code Coverage Tool + +# Visual Studio code coverage results + +# NCrunch + +# MightyMoose + +# Web workbench (sass) + +# Installshield output folder + +# DocProject is a documentation generator add-in + +# Click-Once directory + +# Publish Web Output +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted + +# NuGet Packages +# NuGet Symbol Packages +# The packages folder can be ignored because of Package Restore +# except build/, which is used as an MSBuild target. +# Uncomment if necessary however generally it will be regenerated when needed +# NuGet v3's project.json files produces more ignorable files + +# Microsoft Azure Build Output + +# Microsoft Azure Emulator + +# Windows Store app package directories and files + +# Visual Studio cache files +# files ending in .cache can be ignored +# but keep track of directories ending in .cache + +# Others + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) + +# RIA/Silverlight projects + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) + +# SQL Server files + +# Business Intelligence projects + +# Microsoft Fakes + +# GhostDoc plugin setting file + +# Node.js Tools for Visual Studio + +# Visual Studio 6 build log + +# Visual Studio 6 workspace options file + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) + +# Visual Studio 6 auto-generated project file (contains which files were open etc.) + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) + +# Visual Studio 6 technical files + +# Visual Studio LightSwitch build output + +# Paket dependency manager + +# FAKE - F# Make + +# CodeRush personal settings + +# Python Tools for Visual Studio (PTVS) + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio + +# Telerik's JustMock configuration file + +# BizTalk build output + +# OpenCover UI analysis results + +# Azure Stream Analytics local run output + +# MSBuild Binary and Structured Log + +# NVidia Nsight GPU debugger configuration file + +# MFractors (Xamarin productivity tool) working folder + +# Local History for Visual Studio + +# Visual Studio History (VSHistory) files + +# BeatPulse healthcheck temp database + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 + +# Ionide (cross platform F# VS Code tools) working folder + +# Fody - auto-generated XML schema + +# VS Code files for those working on multiple tools + +# Local History for Visual Studio Code + +# Windows Installer files from build outputs + +# JetBrains Rider + +### VisualStudio Patch ### +# Additional files built by Visual Studio + +# End of https://www.toptal.com/developers/gitignore/api/csharp,dotnetcore,visualstudiocode,visualstudio,rider \ No newline at end of file diff --git a/Problems.md b/Problems.md new file mode 100644 index 0000000..2e4d9a2 --- /dev/null +++ b/Problems.md @@ -0,0 +1,27 @@ +## Code explanation +This is a program manages the processing of orders in the following way: when an order is registered with the `RegisterOrder` method, the order is first deleted using the `DeleteOrder` method. Then, the order is converted to a list of `ReviewExportLine` objects using the `ConvertOrderToExportLines` method and added to a queue of lists of `ReviewExportLine` objects. There is a separate thread running in the background which was mentioend in the assignment itself. This service continuously checks the queue and processes the review export lines when the queue is not empty and the review folder is empty. When the review folder is not empty, the thread waits for 2 seconds and checks the queue again. The processing of review export lines involves opening a file in the review folder with a random 7 character string as the file name, writing the review export lines to the file, and then closing the file. + +## Problems with the code +1. The `ProcessReviewExportLines` method runs in an infinite loop, which means that it will run indefinitely until the program is terminated. This may not be desirable behavior, especially if the program is intended to run for a fixed amount of time or in response to specific user input. Also, this seems hacky: a better practice would be to use some sort of a listener or a state observer (with observer pattern). + +2. Possible race-conditions: the `ProcessReviewExportLines` method checks whether the review folder is empty before processing the review export lines. However, it is possible that other processes or threads may be writing to the review folder at the same time, which could cause the program to skip processing the review export lines even if the folder is not actually empty. + +3. The `ProcessReviewExportLines` method writes to the review folder using a randomly generated file name. This may cause problems if the program needs to read the contents of the review folder at a later time, as it will be difficult to determine which file corresponds to which order. + +4. The `FilterString` method replaces semicolons (;) with colons (:). This may cause problems if the input strings contain colons that are not intended to be replaced. If it is such a simple operation, then it should be done locally. + +5. The `DeleteOrder` method enqueues a single ReviewExportLine object with a Type field value of "9" to the queue. It is not clear what the purpose of this line is, or how it is related to the process of deleting an order. + +6. `GenerateRandomString` method does not check the validity of the returned path: Since it generates these characters randomly, there could be a chaater that is considered illegal to be used as in a system-path. Additionally, 7 characters (which are very bounded) might not prove to be enough to prevent collisions. + +## How to solve them +1. Add a condition to the loop to terminate it after a certain number of iterations or after a certain amount of time has passed Alternatively, a cancellation token to the method and pass it a token that can be used to cancel the loop. Or used better design-patterns + +2. Use a locking-mechanism to syncronize access to a shared resource, in this case, this folder. + +3. Use an incrementative / more predictable file name. Such as using timestamps. + +4. Move the operation upwards, or use the method better, to actually filter-out invalid characters. + +5. Add proper useful logic. Or add comments explaining it. + diff --git a/Program.cs b/Program.cs index de760b5..ee29cc3 100644 --- a/Program.cs +++ b/Program.cs @@ -15,7 +15,9 @@ namespace OrderParser foreach (var jsonFile in filesInfoConfig.JsonFilesInfo) { - var orderModel = new OrderModel(Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText(jsonFile.FullName))); + var orderDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText(jsonFile.FullName)); + + var orderModel = new OrderModel(orderDTO); csvFileWriter.writeOutRow(orderModel); } diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/review.cs b/review.cs index c22c052..dd67e81 100644 --- a/review.cs +++ b/review.cs @@ -1,144 +1,144 @@ -// using SollicitantReview.Models; -// using SollicitantReview.Services; -// using Microsoft.Extensions.Options; -// using System; -// using System.Collections.Generic; -// using System.IO; -// using System.Linq; -// using System.Text; -// using System.Threading; -// using System.Threading.Tasks; +using SollicitantReview.Models; +using SollicitantReview.Services; +using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; -// namespace SollicitantReview -// { -// public class SollicitantReview -// { -// private SollicitantReviewSettings options; -// private IWriter writer; -// private Queue> exportLineQueue; +namespace SollicitantReview +{ + public class SollicitantReview + { + private SollicitantReviewSettings options; + private IWriter writer; + private Queue> exportLineQueue; -// public SollicitantReview(IOptions options, IWriter writer) -// { -// this.options = options.Value; -// this.writer = writer; -// this.exportLineQueue = new Queue>(); + public SollicitantReview(IOptions options, IWriter writer) + { + this.options = options.Value; + this.writer = writer; + this.exportLineQueue = new Queue>(); -// var processExportLinesThread = new Thread(new ThreadStart(ProcessReviewExportLines)); -// processExportLinesThread.Start(); -// } + var processExportLinesThread = new Thread(new ThreadStart(ProcessReviewExportLines)); + processExportLinesThread.Start(); + } -// public void RegisterOrder(Order order) -// { -// DeleteOrder(order); -// exportLineQueue.Enqueue(ConvertOrderToExportLines(order)); -// } + public void RegisterOrder(Order order) + { + DeleteOrder(order); + exportLineQueue.Enqueue(ConvertOrderToExportLines(order)); + } -// private string FilterString(string value) -// { -// return value.Replace(";", ":"); -// } + private string FilterString(string value) + { + return value.Replace(";", ":"); + } -// private void DeleteOrder(Order order) -// { -// var exportLines = new List() -// { -// new ReviewExportLine() -// { -// Type = "9", -// OrderNumber = FilterString(order.OrderNumber), -// Amount = "1", -// UserId = FilterString(order.User), -// JournalPostIndication = "Y" -// } -// }; + private void DeleteOrder(Order order) + { + var exportLines = new List() + { + new ReviewExportLine() + { + Type = "9", + OrderNumber = FilterString(order.OrderNumber), + Amount = "1", + UserId = FilterString(order.User), + JournalPostIndication = "Y" + } + }; -// exportLineQueue.Enqueue(exportLines); -// } + exportLineQueue.Enqueue(exportLines); + } -// private List ConvertOrderToExportLines(Order order, string userId = null) -// { -// var exportLines = new List(); + private List ConvertOrderToExportLines(Order order, string userId = null) + { + var exportLines = new List(); -// foreach (var orderLine in order.OrderLines) -// { -// exportLines.Add(new ReviewExportLine() -// { -// OrderNumber = FilterString(order.OrderNumber), -// Name = FilterString(orderLine.Name), -// Amount = FilterString(orderLine.Amount), -// ProductNumber = FilterString(orderLine.Number), -// ProductDescription = FilterString(orderLine.Description), -// UserId = userId == null ? FilterString(order.User) : userId, -// Location = FilterString(orderLine.Location), -// JournalPostIndication = "Y" -// }); -// } + foreach (var orderLine in order.OrderLines) + { + exportLines.Add(new ReviewExportLine() + { + OrderNumber = FilterString(order.OrderNumber), + Name = FilterString(orderLine.Name), + Amount = FilterString(orderLine.Amount), + ProductNumber = FilterString(orderLine.Number), + ProductDescription = FilterString(orderLine.Description), + UserId = userId == null ? FilterString(order.User) : userId, + Location = FilterString(orderLine.Location), + JournalPostIndication = "Y" + }); + } -// return exportLines; -// } + return exportLines; + } -// private void ProcessReviewExportLines() -// { -// while(true) -// { -// if (exportLineQueue.Any() && IsFolderEmpty(options.ReviewFolderPath)) -// { -// var exportLines = exportLineQueue.Dequeue(); -// var randomStringLength = 7; + private void ProcessReviewExportLines() + { + while(true) + { + if (exportLineQueue.Any() && IsFolderEmpty(options.ReviewFolderPath)) + { + var exportLines = exportLineQueue.Dequeue(); + var randomStringLength = 7; -// writer.OpenFile($"{options.ReviewFolderPath}\\{GenerateRandomString(randomStringLength)}.txt"); + writer.OpenFile($"{options.ReviewFolderPath}\\{GenerateRandomString(randomStringLength)}.txt"); -// foreach (var exportLine in exportLines) -// { -// writer.WriteLine($"{exportLine.Type};" + -// $"{exportLine.OrderNumber};" + -// $"{exportLine.Name};" + -// $"{exportLine.Amount};" + -// $"{exportLine.ProductNumber};" + -// $"{exportLine.ProductDescription};" + -// $"{exportLine.UserId};" + -// $"{exportLine.Location};" + -// $"{exportLine.JournalPostIndication};" + -// $"{exportLine.Info2}"); -// } + foreach (var exportLine in exportLines) + { + writer.WriteLine($"{exportLine.Type};" + + $"{exportLine.OrderNumber};" + + $"{exportLine.Name};" + + $"{exportLine.Amount};" + + $"{exportLine.ProductNumber};" + + $"{exportLine.ProductDescription};" + + $"{exportLine.UserId};" + + $"{exportLine.Location};" + + $"{exportLine.JournalPostIndication};" + + $"{exportLine.Info2}"); + } -// writer.CloseFile(); -// } + writer.CloseFile(); + } -// Thread.Sleep(2000); -// } -// } + Thread.Sleep(2000); + } + } -// private bool IsFolderEmpty(string path) -// { -// DirectoryInfo dirInfo = new DirectoryInfo(path); + private bool IsFolderEmpty(string path) + { + DirectoryInfo dirInfo = new DirectoryInfo(path); -// if (!dirInfo.Exists) -// { -// throw new CouldNotFindReviewDirectory(); -// } + if (!dirInfo.Exists) + { + throw new CouldNotFindReviewDirectory(); + } -// return !dirInfo.GetFiles("*.txt").Any(); -// } + return !dirInfo.GetFiles("*.txt").Any(); + } -// private string GenerateRandomString(int length) -// { -// StringBuilder stringBuilder = new StringBuilder(); -// Random random = new Random(); -// var ASCIIOffset = 65; -// var ASCIIRange = 25; + private string GenerateRandomString(int length) + { + StringBuilder stringBuilder = new StringBuilder(); + Random random = new Random(); + var ASCIIOffset = 65; + var ASCIIRange = 25; -// char letter; + char letter; -// for (int i = 0; i < length; i++) -// { -// double randomDouble = random.NextDouble(); -// int shift = Convert.ToInt32(Math.Floor(ASCIIRange * randomDouble)); -// letter = Convert.ToChar(shift + ASCIIOffset); -// stringBuilder.Append(letter); -// } + for (int i = 0; i < length; i++) + { + double randomDouble = random.NextDouble(); + int shift = Convert.ToInt32(Math.Floor(ASCIIRange * randomDouble)); + letter = Convert.ToChar(shift + ASCIIOffset); + stringBuilder.Append(letter); + } -// return stringBuilder.ToString(); -// } -// } -// } + return stringBuilder.ToString(); + } + } +} From 55f80278ed48e33fa2642520647faca016d5f53d Mon Sep 17 00:00:00 2001 From: Mguy13 Date: Thu, 5 Jan 2023 15:13:42 +0100 Subject: [PATCH 4/8] Update README.md --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index e69de29..a522f7c 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,31 @@ +# Project Description + +## Running the project + +1. Comment out all the code in [review.cs](review.cs) file. Missing directives in that file will otherwise prevent the the program from running. And this file is not central to the project itself. + +2. The easiest way to run this would be to use `dotnet run [args]`. If you want a more realistic use case, you use `dotnet publish` to create an executable binary and then execute that binary with the command-line args as normal. + +## Infrastructure disclaimer +I come from a background of developing mobile applications in Dart/Flutter, with a different set of conventions (such for file naming etc). I have done some research into doing it and tried sticking to it. But in the end, the architecture is still heavily influenced by practices in mobile development. + +### Structure + +#### Assets +There ae 2 JSON files in the `assets` directory. These can be used as input as cli-args. + +#### Data modelling objects +1. [OrderDTO](data/dtos/OrderDTO.cs): Captures the raw data received from a JSON file. +2. [OrderModel](models/OrderModel.cs): Captures the usable part of the `OrderDTO` that will be written out to the CSV. + +#### Services +1. [DemoApplicationConfiguration](services/DemoApplicationConfiguration.cs): Stores the configuration that the program ran with. Such as the command-line args passed (and hence the mode), the actual input data, and the ouput destination. Along with the file-existnece checks +2. [CsvWriter](services/CsvWriter.cs): Writes an order to a file. +3. Deserializing the JSON was handled by a package. Could have made this into a service ideally, for better scaling up. + +#### Packages used +1. Newtonsoft.Json: For deserialzing JSON into constituent DTOs easily. +2. CsvHelper: To write out to a CSV file. + +# Code review exercise +The answer to the review exercise can be found in [Problems.md](Problems.md) file. From 9e2565d1103a905900287c2d5a1573483bc52435 Mon Sep 17 00:00:00 2001 From: Mguy13 Date: Wed, 4 Jan 2023 18:53:48 +0100 Subject: [PATCH 5/8] Added backend skeleton, models and services --- DemoApplication.csproj | 5 ++ OrderParser.cs | 0 Program.cs | 23 +++++- assets/input2.json | 4 +- data/dtos/order_dto.cs | 34 +++++++++ models/OrderModel.cs | 17 +++++ services/CsvWriter.cs | 22 ++++++ services/DemoApplicationConfiguration.cs | 92 ++++++++++++++++++++++++ 8 files changed, 193 insertions(+), 4 deletions(-) delete mode 100644 OrderParser.cs create mode 100644 data/dtos/order_dto.cs create mode 100644 models/OrderModel.cs create mode 100644 services/CsvWriter.cs create mode 100644 services/DemoApplicationConfiguration.cs diff --git a/DemoApplication.csproj b/DemoApplication.csproj index d439800..5e60f9c 100644 --- a/DemoApplication.csproj +++ b/DemoApplication.csproj @@ -7,4 +7,9 @@ enable + + + + + diff --git a/OrderParser.cs b/OrderParser.cs deleted file mode 100644 index e69de29..0000000 diff --git a/Program.cs b/Program.cs index a4cc54d..de760b5 100644 --- a/Program.cs +++ b/Program.cs @@ -1,10 +1,29 @@ -namespace OrderParser +using Configuration; +using DTO; +using static Configuration.DemoApplicationConfiguration; + +namespace OrderParser { class Program { static void Main(string[] args) { - Console.WriteLine(string.Join(" ", args)); + try + { + var filesInfoConfig = DemoApplicationConfiguration.InitializeFrom(args); + var csvFileWriter = new CsvFileWriter(filesInfoConfig.OutputFile); + + foreach (var jsonFile in filesInfoConfig.JsonFilesInfo) + { + var orderModel = new OrderModel(Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText(jsonFile.FullName))); + + csvFileWriter.writeOutRow(orderModel); + } + } + catch (DemoApplicationConfigurationException e) + { + Console.WriteLine(e.Message); + } } } } \ No newline at end of file diff --git a/assets/input2.json b/assets/input2.json index 2a0701f..7e51129 100644 --- a/assets/input2.json +++ b/assets/input2.json @@ -6,13 +6,13 @@ "ProductId": "PID3", "Description": "pid3 description", "Amount": 1.5, - "price": 19.5 + "price": 42.0 }, { "ProductId": "PID4", "Description": "pid4 description", "Amount": 2.5, - "price": 12.1 + "price": 169.0 } ] } \ No newline at end of file diff --git a/data/dtos/order_dto.cs b/data/dtos/order_dto.cs new file mode 100644 index 0000000..22e8c70 --- /dev/null +++ b/data/dtos/order_dto.cs @@ -0,0 +1,34 @@ +namespace DTO +{ + using Newtonsoft.Json; + + public partial class OrderDto + { + [JsonProperty("OrderId")] + public virtual long OrderId { get; set; } + + [JsonProperty("Description")] + public virtual string Description { get; set; } + + [JsonProperty("customer ID")] + public virtual long CustomerId { get; set; } + + [JsonProperty("products")] + public virtual Product[] Products { get; set; } + } + + public partial class Product + { + [JsonProperty("ProductId")] + public virtual string ProductId { get; set; } + + [JsonProperty("Description")] + public virtual string Description { get; set; } + + [JsonProperty("Amount")] + public virtual double Amount { get; set; } + + [JsonProperty("price")] + public virtual double Price { get; set; } + } +} diff --git a/models/OrderModel.cs b/models/OrderModel.cs new file mode 100644 index 0000000..62bb0ba --- /dev/null +++ b/models/OrderModel.cs @@ -0,0 +1,17 @@ +using DTO; + +public sealed class OrderModel { + public long OrderId { get; set; } + public string OrderDescription { get; set; } + + public long CustomerId { get; set; } + + public double TotalPrice { get; set; } + + public OrderModel(OrderDto orderDTO) { + OrderId = orderDTO.OrderId; + OrderDescription = orderDTO.Description; + CustomerId = orderDTO.CustomerId; + TotalPrice = orderDTO.Products.Select(product => product.Price).Sum(); + } +} \ No newline at end of file diff --git a/services/CsvWriter.cs b/services/CsvWriter.cs new file mode 100644 index 0000000..5d655db --- /dev/null +++ b/services/CsvWriter.cs @@ -0,0 +1,22 @@ +using System.Globalization; +using CsvHelper; + +class CsvFileWriter +{ + private TextWriter textWriter; + private CsvWriter csvWriter; + + public CsvFileWriter(FileInfo fileInfo) { + textWriter = new StreamWriter(fileInfo.FullName); + csvWriter = new CsvWriter(textWriter, CultureInfo.InvariantCulture); + } + + public void writeOutRow(OrderModel orderModel) { + csvWriter.WriteField(orderModel.OrderId); + csvWriter.WriteField(orderModel.OrderDescription); + csvWriter.WriteField(orderModel.CustomerId); + csvWriter.WriteField(orderModel.TotalPrice); + + csvWriter.NextRecord(); + } +} \ No newline at end of file diff --git a/services/DemoApplicationConfiguration.cs b/services/DemoApplicationConfiguration.cs new file mode 100644 index 0000000..f16473c --- /dev/null +++ b/services/DemoApplicationConfiguration.cs @@ -0,0 +1,92 @@ +namespace Configuration +{ + public sealed class DemoApplicationConfiguration + { + private FileInfo[] jsonFiles; + public FileInfo[] JsonFilesInfo + { + get { return jsonFiles; } + private set + { + jsonFiles = value.Reverse().ToArray(); + } + } + private FileInfo outputFile; + public FileInfo OutputFile{ + get {return outputFile;} + private set + { + if (!canCreateFile(value.FullName)) + throw new DemoApplicationConfigurationException($"Given output name '{value.FullName}' cannot be used"); + outputFile = value; + } + } + + public static DemoApplicationConfiguration InitializeFrom(string[] args) + { + if (args.Length < 3) + throw new DemoApplicationConfigurationException("Must have atleast: 1 flag, 1 path, 1 output filename"); + + var outputFileName = new FileInfo(args.Last()); + + // Remove last file-name arg, to get a more consistent array + args = args.SkipLast(1).ToArray(); + + + string[] givenFlags = args.Where((value, index) => index % 2 == 0).ToArray(); + string[] givenFilePaths = args.Where((value, index) => index % 2 != 0).ToArray(); + + try + { + if ( givenFlags.Length == 1 && givenFlags[0].Equals("-d") ) { + string[] filesInDirectory = Directory.GetFiles(givenFilePaths[0]); + return new DemoApplicationConfiguration { + OutputFile = outputFileName, + JsonFilesInfo = filesInDirectory.Select(name => new FileInfo(name)).ToArray(), + }; + } + else if (givenFlags.All((string flag) => flag.Equals("-f"))) { + return new DemoApplicationConfiguration { + OutputFile = outputFileName, + JsonFilesInfo = givenFilePaths.Select(name => new FileInfo(name)).ToArray(), + }; + } + else { + throw new DemoApplicationConfigurationException("Must have either 1 flag that is '-d', of all flags must be '-f"); + } + } + catch (System.Exception e) + { + Console.WriteLine("Something else went wrong"); + throw; + } + } + + + private static bool canCreateFile(string filename) { + try + { + using (File.Create(filename)) { + File.Delete(filename); + return true; + } + } + catch + { + return false; + } + } + + [Serializable] + internal sealed class DemoApplicationConfigurationException : Exception + { + public DemoApplicationConfigurationException() + { + } + + public DemoApplicationConfigurationException(string? message) : base(message) + { + } + } + } +} \ No newline at end of file From 4d6f9092eb9dc6fd03782e6436802dc33eadabfd Mon Sep 17 00:00:00 2001 From: Mguy13 Date: Wed, 4 Jan 2023 19:47:37 +0100 Subject: [PATCH 6/8] Minor improvements and quality refactoring --- .vscode/launch.json | 4 +++- assets/input1.json | 2 +- assets/input2.json | 2 +- data/dtos/{order_dto.cs => OrderDTO.cs} | 0 models/OrderModel.cs | 2 +- services/CsvWriter.cs | 2 ++ services/DemoApplicationConfiguration.cs | 15 ++++++++------- 7 files changed, 16 insertions(+), 11 deletions(-) rename data/dtos/{order_dto.cs => OrderDTO.cs} (100%) diff --git a/.vscode/launch.json b/.vscode/launch.json index 87c338c..c646cea 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,6 +1,8 @@ { "version": "0.2.0", "configurations": [ + + { // Use IntelliSense to find out which attributes exist for C# debugging @@ -12,7 +14,7 @@ "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. "program": "${workspaceFolder}/bin/Debug/net7.0/DemoApplication.dll", - "args": [], + "args": ["-f", "assets/input1.json", "-f", "assets/input2.json", "out.csv"], "cwd": "${workspaceFolder}", // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console "console": "internalConsole", diff --git a/assets/input1.json b/assets/input1.json index a373f31..b4c8b82 100644 --- a/assets/input1.json +++ b/assets/input1.json @@ -1,6 +1,6 @@ { "OrderId": 1, - "Description": " Order 123", + "Description": "Order 123", "customer ID": 123, "products": [{ "ProductId": "PID1", diff --git a/assets/input2.json b/assets/input2.json index 7e51129..c2173cf 100644 --- a/assets/input2.json +++ b/assets/input2.json @@ -1,6 +1,6 @@ { "OrderId": 3, - "Description": " Order 11234", + "Description": "Order 11234", "customer ID": 1134, "products": [{ "ProductId": "PID3", diff --git a/data/dtos/order_dto.cs b/data/dtos/OrderDTO.cs similarity index 100% rename from data/dtos/order_dto.cs rename to data/dtos/OrderDTO.cs diff --git a/models/OrderModel.cs b/models/OrderModel.cs index 62bb0ba..783a9ce 100644 --- a/models/OrderModel.cs +++ b/models/OrderModel.cs @@ -10,7 +10,7 @@ public sealed class OrderModel { public OrderModel(OrderDto orderDTO) { OrderId = orderDTO.OrderId; - OrderDescription = orderDTO.Description; + OrderDescription = orderDTO.Description.Trim(); CustomerId = orderDTO.CustomerId; TotalPrice = orderDTO.Products.Select(product => product.Price).Sum(); } diff --git a/services/CsvWriter.cs b/services/CsvWriter.cs index 5d655db..4b9ca0f 100644 --- a/services/CsvWriter.cs +++ b/services/CsvWriter.cs @@ -8,6 +8,7 @@ class CsvFileWriter public CsvFileWriter(FileInfo fileInfo) { textWriter = new StreamWriter(fileInfo.FullName); + csvWriter = new CsvWriter(textWriter, CultureInfo.InvariantCulture); } @@ -17,6 +18,7 @@ class CsvFileWriter csvWriter.WriteField(orderModel.CustomerId); csvWriter.WriteField(orderModel.TotalPrice); + csvWriter.Flush(); csvWriter.NextRecord(); } } \ No newline at end of file diff --git a/services/DemoApplicationConfiguration.cs b/services/DemoApplicationConfiguration.cs index f16473c..8e2049c 100644 --- a/services/DemoApplicationConfiguration.cs +++ b/services/DemoApplicationConfiguration.cs @@ -30,14 +30,15 @@ namespace Configuration var outputFileName = new FileInfo(args.Last()); // Remove last file-name arg, to get a more consistent array - args = args.SkipLast(1).ToArray(); + var strippedArgs = args.SkipLast(1).ToArray(); - string[] givenFlags = args.Where((value, index) => index % 2 == 0).ToArray(); - string[] givenFilePaths = args.Where((value, index) => index % 2 != 0).ToArray(); + string[] givenFlags = strippedArgs.Where((value, index) => index % 2 == 0).ToArray(); + string[] givenFilePaths = strippedArgs.Where((value, index) => index % 2 != 0).ToArray(); try { + // Assert only a single 'd' was passed if ( givenFlags.Length == 1 && givenFlags[0].Equals("-d") ) { string[] filesInDirectory = Directory.GetFiles(givenFilePaths[0]); return new DemoApplicationConfiguration { @@ -45,6 +46,7 @@ namespace Configuration JsonFilesInfo = filesInDirectory.Select(name => new FileInfo(name)).ToArray(), }; } + // Assert only '-f' were passed else if (givenFlags.All((string flag) => flag.Equals("-f"))) { return new DemoApplicationConfiguration { OutputFile = outputFileName, @@ -52,12 +54,11 @@ namespace Configuration }; } else { - throw new DemoApplicationConfigurationException("Must have either 1 flag that is '-d', of all flags must be '-f"); + throw new DemoApplicationConfigurationException("Must have either 1 flag that is '-d', or all flags must be '-f"); } - } - catch (System.Exception e) + } catch (Exception) { - Console.WriteLine("Something else went wrong"); + Console.WriteLine("Something went wrong"); throw; } } From 255072433368e2de41c0d93e46e9ba8b9c73e1c6 Mon Sep 17 00:00:00 2001 From: Mguy13 Date: Wed, 4 Jan 2023 23:32:29 +0100 Subject: [PATCH 7/8] Code Review results documented --- .gitignore | 265 +++++++++++++++++++++++++++++++++++++++++++++++++++- Problems.md | 27 ++++++ Program.cs | 4 +- README.md | 0 review.cs | 244 +++++++++++++++++++++++------------------------ 5 files changed, 416 insertions(+), 124 deletions(-) create mode 100644 Problems.md create mode 100644 README.md diff --git a/.gitignore b/.gitignore index d6c8a08..5806ae5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Created by https://www.toptal.com/developers/gitignore/api/csharp,dotnetcore,visualstudiocode,visualstudio,rider +# Edit at https://www.toptal.com/developers/gitignore?templates=csharp,dotnetcore,visualstudiocode,visualstudio,rider + ### Csharp ### ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. @@ -405,6 +408,85 @@ obj/ /node_modules /wwwroot/node_modules +### Rider ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + ### VisualStudioCode ### !.vscode/*.code-snippets @@ -418,4 +500,185 @@ obj/ .history .ionide -# End of https://www.toptal.com/developers/gitignore/api/csharp,visualstudiocode,dotnetcore \ No newline at end of file +### VisualStudio ### + +# User-specific files + +# User-specific files (MonoDevelop/Xamarin Studio) + +# Mono auto generated files + +# Build results + +# Visual Studio 2015/2017 cache/options directory +# Uncomment if you have tasks that create the project's static files in wwwroot + +# Visual Studio 2017 auto generated files + +# MSTest test Results + +# NUnit + +# Build Results of an ATL Project + +# Benchmark Results + +# .NET Core + +# ASP.NET Scaffolding + +# StyleCop + +# Files built by Visual Studio + +# Chutzpah Test files + +# Visual C++ cache files + +# Visual Studio profiler + +# Visual Studio Trace Files + +# TFS 2012 Local Workspace + +# Guidance Automation Toolkit + +# ReSharper is a .NET coding add-in + +# TeamCity is a build add-in + +# DotCover is a Code Coverage Tool + +# AxoCover is a Code Coverage Tool + +# Coverlet is a free, cross platform Code Coverage Tool + +# Visual Studio code coverage results + +# NCrunch + +# MightyMoose + +# Web workbench (sass) + +# Installshield output folder + +# DocProject is a documentation generator add-in + +# Click-Once directory + +# Publish Web Output +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted + +# NuGet Packages +# NuGet Symbol Packages +# The packages folder can be ignored because of Package Restore +# except build/, which is used as an MSBuild target. +# Uncomment if necessary however generally it will be regenerated when needed +# NuGet v3's project.json files produces more ignorable files + +# Microsoft Azure Build Output + +# Microsoft Azure Emulator + +# Windows Store app package directories and files + +# Visual Studio cache files +# files ending in .cache can be ignored +# but keep track of directories ending in .cache + +# Others + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) + +# RIA/Silverlight projects + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) + +# SQL Server files + +# Business Intelligence projects + +# Microsoft Fakes + +# GhostDoc plugin setting file + +# Node.js Tools for Visual Studio + +# Visual Studio 6 build log + +# Visual Studio 6 workspace options file + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) + +# Visual Studio 6 auto-generated project file (contains which files were open etc.) + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) + +# Visual Studio 6 technical files + +# Visual Studio LightSwitch build output + +# Paket dependency manager + +# FAKE - F# Make + +# CodeRush personal settings + +# Python Tools for Visual Studio (PTVS) + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio + +# Telerik's JustMock configuration file + +# BizTalk build output + +# OpenCover UI analysis results + +# Azure Stream Analytics local run output + +# MSBuild Binary and Structured Log + +# NVidia Nsight GPU debugger configuration file + +# MFractors (Xamarin productivity tool) working folder + +# Local History for Visual Studio + +# Visual Studio History (VSHistory) files + +# BeatPulse healthcheck temp database + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 + +# Ionide (cross platform F# VS Code tools) working folder + +# Fody - auto-generated XML schema + +# VS Code files for those working on multiple tools + +# Local History for Visual Studio Code + +# Windows Installer files from build outputs + +# JetBrains Rider + +### VisualStudio Patch ### +# Additional files built by Visual Studio + +# End of https://www.toptal.com/developers/gitignore/api/csharp,dotnetcore,visualstudiocode,visualstudio,rider \ No newline at end of file diff --git a/Problems.md b/Problems.md new file mode 100644 index 0000000..2e4d9a2 --- /dev/null +++ b/Problems.md @@ -0,0 +1,27 @@ +## Code explanation +This is a program manages the processing of orders in the following way: when an order is registered with the `RegisterOrder` method, the order is first deleted using the `DeleteOrder` method. Then, the order is converted to a list of `ReviewExportLine` objects using the `ConvertOrderToExportLines` method and added to a queue of lists of `ReviewExportLine` objects. There is a separate thread running in the background which was mentioend in the assignment itself. This service continuously checks the queue and processes the review export lines when the queue is not empty and the review folder is empty. When the review folder is not empty, the thread waits for 2 seconds and checks the queue again. The processing of review export lines involves opening a file in the review folder with a random 7 character string as the file name, writing the review export lines to the file, and then closing the file. + +## Problems with the code +1. The `ProcessReviewExportLines` method runs in an infinite loop, which means that it will run indefinitely until the program is terminated. This may not be desirable behavior, especially if the program is intended to run for a fixed amount of time or in response to specific user input. Also, this seems hacky: a better practice would be to use some sort of a listener or a state observer (with observer pattern). + +2. Possible race-conditions: the `ProcessReviewExportLines` method checks whether the review folder is empty before processing the review export lines. However, it is possible that other processes or threads may be writing to the review folder at the same time, which could cause the program to skip processing the review export lines even if the folder is not actually empty. + +3. The `ProcessReviewExportLines` method writes to the review folder using a randomly generated file name. This may cause problems if the program needs to read the contents of the review folder at a later time, as it will be difficult to determine which file corresponds to which order. + +4. The `FilterString` method replaces semicolons (;) with colons (:). This may cause problems if the input strings contain colons that are not intended to be replaced. If it is such a simple operation, then it should be done locally. + +5. The `DeleteOrder` method enqueues a single ReviewExportLine object with a Type field value of "9" to the queue. It is not clear what the purpose of this line is, or how it is related to the process of deleting an order. + +6. `GenerateRandomString` method does not check the validity of the returned path: Since it generates these characters randomly, there could be a chaater that is considered illegal to be used as in a system-path. Additionally, 7 characters (which are very bounded) might not prove to be enough to prevent collisions. + +## How to solve them +1. Add a condition to the loop to terminate it after a certain number of iterations or after a certain amount of time has passed Alternatively, a cancellation token to the method and pass it a token that can be used to cancel the loop. Or used better design-patterns + +2. Use a locking-mechanism to syncronize access to a shared resource, in this case, this folder. + +3. Use an incrementative / more predictable file name. Such as using timestamps. + +4. Move the operation upwards, or use the method better, to actually filter-out invalid characters. + +5. Add proper useful logic. Or add comments explaining it. + diff --git a/Program.cs b/Program.cs index de760b5..ee29cc3 100644 --- a/Program.cs +++ b/Program.cs @@ -15,7 +15,9 @@ namespace OrderParser foreach (var jsonFile in filesInfoConfig.JsonFilesInfo) { - var orderModel = new OrderModel(Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText(jsonFile.FullName))); + var orderDTO = Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText(jsonFile.FullName)); + + var orderModel = new OrderModel(orderDTO); csvFileWriter.writeOutRow(orderModel); } diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/review.cs b/review.cs index c22c052..dd67e81 100644 --- a/review.cs +++ b/review.cs @@ -1,144 +1,144 @@ -// using SollicitantReview.Models; -// using SollicitantReview.Services; -// using Microsoft.Extensions.Options; -// using System; -// using System.Collections.Generic; -// using System.IO; -// using System.Linq; -// using System.Text; -// using System.Threading; -// using System.Threading.Tasks; +using SollicitantReview.Models; +using SollicitantReview.Services; +using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; -// namespace SollicitantReview -// { -// public class SollicitantReview -// { -// private SollicitantReviewSettings options; -// private IWriter writer; -// private Queue> exportLineQueue; +namespace SollicitantReview +{ + public class SollicitantReview + { + private SollicitantReviewSettings options; + private IWriter writer; + private Queue> exportLineQueue; -// public SollicitantReview(IOptions options, IWriter writer) -// { -// this.options = options.Value; -// this.writer = writer; -// this.exportLineQueue = new Queue>(); + public SollicitantReview(IOptions options, IWriter writer) + { + this.options = options.Value; + this.writer = writer; + this.exportLineQueue = new Queue>(); -// var processExportLinesThread = new Thread(new ThreadStart(ProcessReviewExportLines)); -// processExportLinesThread.Start(); -// } + var processExportLinesThread = new Thread(new ThreadStart(ProcessReviewExportLines)); + processExportLinesThread.Start(); + } -// public void RegisterOrder(Order order) -// { -// DeleteOrder(order); -// exportLineQueue.Enqueue(ConvertOrderToExportLines(order)); -// } + public void RegisterOrder(Order order) + { + DeleteOrder(order); + exportLineQueue.Enqueue(ConvertOrderToExportLines(order)); + } -// private string FilterString(string value) -// { -// return value.Replace(";", ":"); -// } + private string FilterString(string value) + { + return value.Replace(";", ":"); + } -// private void DeleteOrder(Order order) -// { -// var exportLines = new List() -// { -// new ReviewExportLine() -// { -// Type = "9", -// OrderNumber = FilterString(order.OrderNumber), -// Amount = "1", -// UserId = FilterString(order.User), -// JournalPostIndication = "Y" -// } -// }; + private void DeleteOrder(Order order) + { + var exportLines = new List() + { + new ReviewExportLine() + { + Type = "9", + OrderNumber = FilterString(order.OrderNumber), + Amount = "1", + UserId = FilterString(order.User), + JournalPostIndication = "Y" + } + }; -// exportLineQueue.Enqueue(exportLines); -// } + exportLineQueue.Enqueue(exportLines); + } -// private List ConvertOrderToExportLines(Order order, string userId = null) -// { -// var exportLines = new List(); + private List ConvertOrderToExportLines(Order order, string userId = null) + { + var exportLines = new List(); -// foreach (var orderLine in order.OrderLines) -// { -// exportLines.Add(new ReviewExportLine() -// { -// OrderNumber = FilterString(order.OrderNumber), -// Name = FilterString(orderLine.Name), -// Amount = FilterString(orderLine.Amount), -// ProductNumber = FilterString(orderLine.Number), -// ProductDescription = FilterString(orderLine.Description), -// UserId = userId == null ? FilterString(order.User) : userId, -// Location = FilterString(orderLine.Location), -// JournalPostIndication = "Y" -// }); -// } + foreach (var orderLine in order.OrderLines) + { + exportLines.Add(new ReviewExportLine() + { + OrderNumber = FilterString(order.OrderNumber), + Name = FilterString(orderLine.Name), + Amount = FilterString(orderLine.Amount), + ProductNumber = FilterString(orderLine.Number), + ProductDescription = FilterString(orderLine.Description), + UserId = userId == null ? FilterString(order.User) : userId, + Location = FilterString(orderLine.Location), + JournalPostIndication = "Y" + }); + } -// return exportLines; -// } + return exportLines; + } -// private void ProcessReviewExportLines() -// { -// while(true) -// { -// if (exportLineQueue.Any() && IsFolderEmpty(options.ReviewFolderPath)) -// { -// var exportLines = exportLineQueue.Dequeue(); -// var randomStringLength = 7; + private void ProcessReviewExportLines() + { + while(true) + { + if (exportLineQueue.Any() && IsFolderEmpty(options.ReviewFolderPath)) + { + var exportLines = exportLineQueue.Dequeue(); + var randomStringLength = 7; -// writer.OpenFile($"{options.ReviewFolderPath}\\{GenerateRandomString(randomStringLength)}.txt"); + writer.OpenFile($"{options.ReviewFolderPath}\\{GenerateRandomString(randomStringLength)}.txt"); -// foreach (var exportLine in exportLines) -// { -// writer.WriteLine($"{exportLine.Type};" + -// $"{exportLine.OrderNumber};" + -// $"{exportLine.Name};" + -// $"{exportLine.Amount};" + -// $"{exportLine.ProductNumber};" + -// $"{exportLine.ProductDescription};" + -// $"{exportLine.UserId};" + -// $"{exportLine.Location};" + -// $"{exportLine.JournalPostIndication};" + -// $"{exportLine.Info2}"); -// } + foreach (var exportLine in exportLines) + { + writer.WriteLine($"{exportLine.Type};" + + $"{exportLine.OrderNumber};" + + $"{exportLine.Name};" + + $"{exportLine.Amount};" + + $"{exportLine.ProductNumber};" + + $"{exportLine.ProductDescription};" + + $"{exportLine.UserId};" + + $"{exportLine.Location};" + + $"{exportLine.JournalPostIndication};" + + $"{exportLine.Info2}"); + } -// writer.CloseFile(); -// } + writer.CloseFile(); + } -// Thread.Sleep(2000); -// } -// } + Thread.Sleep(2000); + } + } -// private bool IsFolderEmpty(string path) -// { -// DirectoryInfo dirInfo = new DirectoryInfo(path); + private bool IsFolderEmpty(string path) + { + DirectoryInfo dirInfo = new DirectoryInfo(path); -// if (!dirInfo.Exists) -// { -// throw new CouldNotFindReviewDirectory(); -// } + if (!dirInfo.Exists) + { + throw new CouldNotFindReviewDirectory(); + } -// return !dirInfo.GetFiles("*.txt").Any(); -// } + return !dirInfo.GetFiles("*.txt").Any(); + } -// private string GenerateRandomString(int length) -// { -// StringBuilder stringBuilder = new StringBuilder(); -// Random random = new Random(); -// var ASCIIOffset = 65; -// var ASCIIRange = 25; + private string GenerateRandomString(int length) + { + StringBuilder stringBuilder = new StringBuilder(); + Random random = new Random(); + var ASCIIOffset = 65; + var ASCIIRange = 25; -// char letter; + char letter; -// for (int i = 0; i < length; i++) -// { -// double randomDouble = random.NextDouble(); -// int shift = Convert.ToInt32(Math.Floor(ASCIIRange * randomDouble)); -// letter = Convert.ToChar(shift + ASCIIOffset); -// stringBuilder.Append(letter); -// } + for (int i = 0; i < length; i++) + { + double randomDouble = random.NextDouble(); + int shift = Convert.ToInt32(Math.Floor(ASCIIRange * randomDouble)); + letter = Convert.ToChar(shift + ASCIIOffset); + stringBuilder.Append(letter); + } -// return stringBuilder.ToString(); -// } -// } -// } + return stringBuilder.ToString(); + } + } +} From 5366b11f55a8c68a0cbf7d507a6dd8c52de271cf Mon Sep 17 00:00:00 2001 From: Mguy13 Date: Thu, 5 Jan 2023 15:13:42 +0100 Subject: [PATCH 8/8] Update README.md --- README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/README.md b/README.md index e69de29..a522f7c 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,31 @@ +# Project Description + +## Running the project + +1. Comment out all the code in [review.cs](review.cs) file. Missing directives in that file will otherwise prevent the the program from running. And this file is not central to the project itself. + +2. The easiest way to run this would be to use `dotnet run [args]`. If you want a more realistic use case, you use `dotnet publish` to create an executable binary and then execute that binary with the command-line args as normal. + +## Infrastructure disclaimer +I come from a background of developing mobile applications in Dart/Flutter, with a different set of conventions (such for file naming etc). I have done some research into doing it and tried sticking to it. But in the end, the architecture is still heavily influenced by practices in mobile development. + +### Structure + +#### Assets +There ae 2 JSON files in the `assets` directory. These can be used as input as cli-args. + +#### Data modelling objects +1. [OrderDTO](data/dtos/OrderDTO.cs): Captures the raw data received from a JSON file. +2. [OrderModel](models/OrderModel.cs): Captures the usable part of the `OrderDTO` that will be written out to the CSV. + +#### Services +1. [DemoApplicationConfiguration](services/DemoApplicationConfiguration.cs): Stores the configuration that the program ran with. Such as the command-line args passed (and hence the mode), the actual input data, and the ouput destination. Along with the file-existnece checks +2. [CsvWriter](services/CsvWriter.cs): Writes an order to a file. +3. Deserializing the JSON was handled by a package. Could have made this into a service ideally, for better scaling up. + +#### Packages used +1. Newtonsoft.Json: For deserialzing JSON into constituent DTOs easily. +2. CsvHelper: To write out to a CSV file. + +# Code review exercise +The answer to the review exercise can be found in [Problems.md](Problems.md) file.