Added backend skeleton, models and services
This commit is contained in:
parent
f818b2d39e
commit
9e2565d110
8 changed files with 193 additions and 4 deletions
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…
Add table
Add a link
Reference in a new issue