From 1170c76e3faec5d9bf0ca84d044e25e1be0d464f Mon Sep 17 00:00:00 2001 From: Mguy13 Date: Wed, 4 Jan 2023 19:47:37 +0100 Subject: [PATCH] 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; } }