From 9e2565d1103a905900287c2d5a1573483bc52435 Mon Sep 17 00:00:00 2001 From: Mguy13 Date: Wed, 4 Jan 2023 18:53:48 +0100 Subject: [PATCH] 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