Added backend skeleton, models and services
parent
f818b2d39e
commit
9e2565d110
@ -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; }
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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 New Issue