Minor improvements and quality refactoring

This commit is contained in:
Mguy13 2023-01-04 19:47:37 +01:00
parent 9e2565d110
commit 4d6f9092eb
7 changed files with 16 additions and 11 deletions

4
.vscode/launch.json vendored
View File

@ -1,6 +1,8 @@
{ {
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
// Use IntelliSense to find out which attributes exist for C# debugging // Use IntelliSense to find out which attributes exist for C# debugging
@ -12,7 +14,7 @@
"preLaunchTask": "build", "preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path. // If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net7.0/DemoApplication.dll", "program": "${workspaceFolder}/bin/Debug/net7.0/DemoApplication.dll",
"args": [], "args": ["-f", "assets/input1.json", "-f", "assets/input2.json", "out.csv"],
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole", "console": "internalConsole",

View File

@ -1,6 +1,6 @@
{ {
"OrderId": 1, "OrderId": 1,
"Description": " Order 123", "Description": "Order 123",
"customer ID": 123, "customer ID": 123,
"products": [{ "products": [{
"ProductId": "PID1", "ProductId": "PID1",

View File

@ -1,6 +1,6 @@
{ {
"OrderId": 3, "OrderId": 3,
"Description": " Order 11234", "Description": "Order 11234",
"customer ID": 1134, "customer ID": 1134,
"products": [{ "products": [{
"ProductId": "PID3", "ProductId": "PID3",

View File

@ -10,7 +10,7 @@ public sealed class OrderModel {
public OrderModel(OrderDto orderDTO) { public OrderModel(OrderDto orderDTO) {
OrderId = orderDTO.OrderId; OrderId = orderDTO.OrderId;
OrderDescription = orderDTO.Description; OrderDescription = orderDTO.Description.Trim();
CustomerId = orderDTO.CustomerId; CustomerId = orderDTO.CustomerId;
TotalPrice = orderDTO.Products.Select(product => product.Price).Sum(); TotalPrice = orderDTO.Products.Select(product => product.Price).Sum();
} }

View File

@ -8,6 +8,7 @@ class CsvFileWriter
public CsvFileWriter(FileInfo fileInfo) { public CsvFileWriter(FileInfo fileInfo) {
textWriter = new StreamWriter(fileInfo.FullName); textWriter = new StreamWriter(fileInfo.FullName);
csvWriter = new CsvWriter(textWriter, CultureInfo.InvariantCulture); csvWriter = new CsvWriter(textWriter, CultureInfo.InvariantCulture);
} }
@ -17,6 +18,7 @@ class CsvFileWriter
csvWriter.WriteField(orderModel.CustomerId); csvWriter.WriteField(orderModel.CustomerId);
csvWriter.WriteField(orderModel.TotalPrice); csvWriter.WriteField(orderModel.TotalPrice);
csvWriter.Flush();
csvWriter.NextRecord(); csvWriter.NextRecord();
} }
} }

View File

@ -30,14 +30,15 @@ namespace Configuration
var outputFileName = new FileInfo(args.Last()); var outputFileName = new FileInfo(args.Last());
// Remove last file-name arg, to get a more consistent array // 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[] givenFlags = strippedArgs.Where((value, index) => index % 2 == 0).ToArray();
string[] givenFilePaths = args.Where((value, index) => index % 2 != 0).ToArray(); string[] givenFilePaths = strippedArgs.Where((value, index) => index % 2 != 0).ToArray();
try try
{ {
// Assert only a single 'd' was passed
if ( givenFlags.Length == 1 && givenFlags[0].Equals("-d") ) { if ( givenFlags.Length == 1 && givenFlags[0].Equals("-d") ) {
string[] filesInDirectory = Directory.GetFiles(givenFilePaths[0]); string[] filesInDirectory = Directory.GetFiles(givenFilePaths[0]);
return new DemoApplicationConfiguration { return new DemoApplicationConfiguration {
@ -45,6 +46,7 @@ namespace Configuration
JsonFilesInfo = filesInDirectory.Select(name => new FileInfo(name)).ToArray(), JsonFilesInfo = filesInDirectory.Select(name => new FileInfo(name)).ToArray(),
}; };
} }
// Assert only '-f' were passed
else if (givenFlags.All((string flag) => flag.Equals("-f"))) { else if (givenFlags.All((string flag) => flag.Equals("-f"))) {
return new DemoApplicationConfiguration { return new DemoApplicationConfiguration {
OutputFile = outputFileName, OutputFile = outputFileName,
@ -52,12 +54,11 @@ namespace Configuration
}; };
} }
else { 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 (Exception)
catch (System.Exception e)
{ {
Console.WriteLine("Something else went wrong"); Console.WriteLine("Something went wrong");
throw; throw;
} }
} }