skeleton done
This commit is contained in:
parent
f818b2d39e
commit
a738ad1054
8 changed files with 193 additions and 4 deletions
|
@ -7,4 +7,9 @@
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="CsvHelper" Version="30.0.1" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
23
Program.cs
23
Program.cs
|
@ -1,10 +1,29 @@
|
||||||
namespace OrderParser
|
using Configuration;
|
||||||
|
using DTO;
|
||||||
|
using static Configuration.DemoApplicationConfiguration;
|
||||||
|
|
||||||
|
namespace OrderParser
|
||||||
{
|
{
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static void Main(string[] args)
|
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<OrderDto>(File.ReadAllText(jsonFile.FullName)));
|
||||||
|
|
||||||
|
csvFileWriter.writeOutRow(orderModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (DemoApplicationConfigurationException e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e.Message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,13 +6,13 @@
|
||||||
"ProductId": "PID3",
|
"ProductId": "PID3",
|
||||||
"Description": "pid3 description",
|
"Description": "pid3 description",
|
||||||
"Amount": 1.5,
|
"Amount": 1.5,
|
||||||
"price": 19.5
|
"price": 42.0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ProductId": "PID4",
|
"ProductId": "PID4",
|
||||||
"Description": "pid4 description",
|
"Description": "pid4 description",
|
||||||
"Amount": 2.5,
|
"Amount": 2.5,
|
||||||
"price": 12.1
|
"price": 169.0
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
34
data/dtos/order_dto.cs
Normal file
34
data/dtos/order_dto.cs
Normal file
|
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
17
models/OrderModel.cs
Normal file
17
models/OrderModel.cs
Normal file
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
22
services/CsvWriter.cs
Normal file
22
services/CsvWriter.cs
Normal file
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
92
services/DemoApplicationConfiguration.cs
Normal file
92
services/DemoApplicationConfiguration.cs
Normal file
|
@ -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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue