Compare commits

...

4 Commits

Author SHA1 Message Date
Mguy13 5366b11f55 Update README.md 2023-01-05 16:31:22 +01:00
Mguy13 2550724333 Code Review results documented 2023-01-05 16:31:22 +01:00
Mguy13 4d6f9092eb Minor improvements and quality refactoring 2023-01-05 16:31:22 +01:00
Mguy13 9e2565d110 Added backend skeleton, models and services 2023-01-05 16:31:22 +01:00
14 changed files with 647 additions and 130 deletions

265
.gitignore vendored
View File

@ -1,3 +1,6 @@
# Created by https://www.toptal.com/developers/gitignore/api/csharp,dotnetcore,visualstudiocode,visualstudio,rider
# Edit at https://www.toptal.com/developers/gitignore?templates=csharp,dotnetcore,visualstudiocode,visualstudio,rider
### Csharp ###
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
@ -405,6 +408,85 @@ obj/
/node_modules
/wwwroot/node_modules
### Rider ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
### VisualStudioCode ###
!.vscode/*.code-snippets
@ -418,4 +500,185 @@ obj/
.history
.ionide
# End of https://www.toptal.com/developers/gitignore/api/csharp,visualstudiocode,dotnetcore
### VisualStudio ###
# User-specific files
# User-specific files (MonoDevelop/Xamarin Studio)
# Mono auto generated files
# Build results
# Visual Studio 2015/2017 cache/options directory
# Uncomment if you have tasks that create the project's static files in wwwroot
# Visual Studio 2017 auto generated files
# MSTest test Results
# NUnit
# Build Results of an ATL Project
# Benchmark Results
# .NET Core
# ASP.NET Scaffolding
# StyleCop
# Files built by Visual Studio
# Chutzpah Test files
# Visual C++ cache files
# Visual Studio profiler
# Visual Studio Trace Files
# TFS 2012 Local Workspace
# Guidance Automation Toolkit
# ReSharper is a .NET coding add-in
# TeamCity is a build add-in
# DotCover is a Code Coverage Tool
# AxoCover is a Code Coverage Tool
# Coverlet is a free, cross platform Code Coverage Tool
# Visual Studio code coverage results
# NCrunch
# MightyMoose
# Web workbench (sass)
# Installshield output folder
# DocProject is a documentation generator add-in
# Click-Once directory
# Publish Web Output
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
# NuGet Packages
# NuGet Symbol Packages
# The packages folder can be ignored because of Package Restore
# except build/, which is used as an MSBuild target.
# Uncomment if necessary however generally it will be regenerated when needed
# NuGet v3's project.json files produces more ignorable files
# Microsoft Azure Build Output
# Microsoft Azure Emulator
# Windows Store app package directories and files
# Visual Studio cache files
# files ending in .cache can be ignored
# but keep track of directories ending in .cache
# Others
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
# RIA/Silverlight projects
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
# SQL Server files
# Business Intelligence projects
# Microsoft Fakes
# GhostDoc plugin setting file
# Node.js Tools for Visual Studio
# Visual Studio 6 build log
# Visual Studio 6 workspace options file
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
# Visual Studio 6 technical files
# Visual Studio LightSwitch build output
# Paket dependency manager
# FAKE - F# Make
# CodeRush personal settings
# Python Tools for Visual Studio (PTVS)
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
# Telerik's JustMock configuration file
# BizTalk build output
# OpenCover UI analysis results
# Azure Stream Analytics local run output
# MSBuild Binary and Structured Log
# NVidia Nsight GPU debugger configuration file
# MFractors (Xamarin productivity tool) working folder
# Local History for Visual Studio
# Visual Studio History (VSHistory) files
# BeatPulse healthcheck temp database
# Backup folder for Package Reference Convert tool in Visual Studio 2017
# Ionide (cross platform F# VS Code tools) working folder
# Fody - auto-generated XML schema
# VS Code files for those working on multiple tools
# Local History for Visual Studio Code
# Windows Installer files from build outputs
# JetBrains Rider
### VisualStudio Patch ###
# Additional files built by Visual Studio
# End of https://www.toptal.com/developers/gitignore/api/csharp,dotnetcore,visualstudiocode,visualstudio,rider

4
.vscode/launch.json vendored
View File

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

View File

@ -7,4 +7,9 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
</Project>

View File

27
Problems.md Normal file
View File

@ -0,0 +1,27 @@
## Code explanation
This is a program manages the processing of orders in the following way: when an order is registered with the `RegisterOrder` method, the order is first deleted using the `DeleteOrder` method. Then, the order is converted to a list of `ReviewExportLine` objects using the `ConvertOrderToExportLines` method and added to a queue of lists of `ReviewExportLine` objects. There is a separate thread running in the background which was mentioend in the assignment itself. This service continuously checks the queue and processes the review export lines when the queue is not empty and the review folder is empty. When the review folder is not empty, the thread waits for 2 seconds and checks the queue again. The processing of review export lines involves opening a file in the review folder with a random 7 character string as the file name, writing the review export lines to the file, and then closing the file.
## Problems with the code
1. The `ProcessReviewExportLines` method runs in an infinite loop, which means that it will run indefinitely until the program is terminated. This may not be desirable behavior, especially if the program is intended to run for a fixed amount of time or in response to specific user input. Also, this seems hacky: a better practice would be to use some sort of a listener or a state observer (with observer pattern).
2. Possible race-conditions: the `ProcessReviewExportLines` method checks whether the review folder is empty before processing the review export lines. However, it is possible that other processes or threads may be writing to the review folder at the same time, which could cause the program to skip processing the review export lines even if the folder is not actually empty.
3. The `ProcessReviewExportLines` method writes to the review folder using a randomly generated file name. This may cause problems if the program needs to read the contents of the review folder at a later time, as it will be difficult to determine which file corresponds to which order.
4. The `FilterString` method replaces semicolons (;) with colons (:). This may cause problems if the input strings contain colons that are not intended to be replaced. If it is such a simple operation, then it should be done locally.
5. The `DeleteOrder` method enqueues a single ReviewExportLine object with a Type field value of "9" to the queue. It is not clear what the purpose of this line is, or how it is related to the process of deleting an order.
6. `GenerateRandomString` method does not check the validity of the returned path: Since it generates these characters randomly, there could be a chaater that is considered illegal to be used as in a system-path. Additionally, 7 characters (which are very bounded) might not prove to be enough to prevent collisions.
## How to solve them
1. Add a condition to the loop to terminate it after a certain number of iterations or after a certain amount of time has passed Alternatively, a cancellation token to the method and pass it a token that can be used to cancel the loop. Or used better design-patterns
2. Use a locking-mechanism to syncronize access to a shared resource, in this case, this folder.
3. Use an incrementative / more predictable file name. Such as using timestamps.
4. Move the operation upwards, or use the method better, to actually filter-out invalid characters.
5. Add proper useful logic. Or add comments explaining it.

View File

@ -1,10 +1,31 @@
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 orderDTO = Newtonsoft.Json.JsonConvert.DeserializeObject<OrderDto>(File.ReadAllText(jsonFile.FullName));
var orderModel = new OrderModel(orderDTO);
csvFileWriter.writeOutRow(orderModel);
}
}
catch (DemoApplicationConfigurationException e)
{
Console.WriteLine(e.Message);
}
}
}
}

31
README.md Normal file
View File

@ -0,0 +1,31 @@
# Project Description
## Running the project
1. Comment out all the code in [review.cs](review.cs) file. Missing directives in that file will otherwise prevent the the program from running. And this file is not central to the project itself.
2. The easiest way to run this would be to use `dotnet run [args]`. If you want a more realistic use case, you use `dotnet publish` to create an executable binary and then execute that binary with the command-line args as normal.
## Infrastructure disclaimer
I come from a background of developing mobile applications in Dart/Flutter, with a different set of conventions (such for file naming etc). I have done some research into doing it and tried sticking to it. But in the end, the architecture is still heavily influenced by practices in mobile development.
### Structure
#### Assets
There ae 2 JSON files in the `assets` directory. These can be used as input as cli-args.
#### Data modelling objects
1. [OrderDTO](data/dtos/OrderDTO.cs): Captures the raw data received from a JSON file.
2. [OrderModel](models/OrderModel.cs): Captures the usable part of the `OrderDTO` that will be written out to the CSV.
#### Services
1. [DemoApplicationConfiguration](services/DemoApplicationConfiguration.cs): Stores the configuration that the program ran with. Such as the command-line args passed (and hence the mode), the actual input data, and the ouput destination. Along with the file-existnece checks
2. [CsvWriter](services/CsvWriter.cs): Writes an order to a file.
3. Deserializing the JSON was handled by a package. Could have made this into a service ideally, for better scaling up.
#### Packages used
1. Newtonsoft.Json: For deserialzing JSON into constituent DTOs easily.
2. CsvHelper: To write out to a CSV file.
# Code review exercise
The answer to the review exercise can be found in [Problems.md](Problems.md) file.

View File

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

View File

@ -1,18 +1,18 @@
{
"OrderId": 3,
"Description": " Order 11234",
"Description": "Order 11234",
"customer ID": 1134,
"products": [{
"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
}
]
}

34
data/dtos/OrderDTO.cs Normal file
View 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
View 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.Trim();
CustomerId = orderDTO.CustomerId;
TotalPrice = orderDTO.Products.Select(product => product.Price).Sum();
}
}

244
review.cs
View File

@ -1,144 +1,144 @@
// using SollicitantReview.Models;
// using SollicitantReview.Services;
// using Microsoft.Extensions.Options;
// using System;
// using System.Collections.Generic;
// using System.IO;
// using System.Linq;
// using System.Text;
// using System.Threading;
// using System.Threading.Tasks;
using SollicitantReview.Models;
using SollicitantReview.Services;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
// namespace SollicitantReview
// {
// public class SollicitantReview
// {
// private SollicitantReviewSettings options;
// private IWriter writer;
// private Queue<List<ReviewExportLine>> exportLineQueue;
namespace SollicitantReview
{
public class SollicitantReview
{
private SollicitantReviewSettings options;
private IWriter writer;
private Queue<List<ReviewExportLine>> exportLineQueue;
// public SollicitantReview(IOptions<SollicitantReviewSettings> options, IWriter writer)
// {
// this.options = options.Value;
// this.writer = writer;
// this.exportLineQueue = new Queue<List<ReviewExportLine>>();
public SollicitantReview(IOptions<SollicitantReviewSettings> options, IWriter writer)
{
this.options = options.Value;
this.writer = writer;
this.exportLineQueue = new Queue<List<ReviewExportLine>>();
// var processExportLinesThread = new Thread(new ThreadStart(ProcessReviewExportLines));
// processExportLinesThread.Start();
// }
var processExportLinesThread = new Thread(new ThreadStart(ProcessReviewExportLines));
processExportLinesThread.Start();
}
// public void RegisterOrder(Order order)
// {
// DeleteOrder(order);
// exportLineQueue.Enqueue(ConvertOrderToExportLines(order));
// }
public void RegisterOrder(Order order)
{
DeleteOrder(order);
exportLineQueue.Enqueue(ConvertOrderToExportLines(order));
}
// private string FilterString(string value)
// {
// return value.Replace(";", ":");
// }
private string FilterString(string value)
{
return value.Replace(";", ":");
}
// private void DeleteOrder(Order order)
// {
// var exportLines = new List<ReviewExportLine>()
// {
// new ReviewExportLine()
// {
// Type = "9",
// OrderNumber = FilterString(order.OrderNumber),
// Amount = "1",
// UserId = FilterString(order.User),
// JournalPostIndication = "Y"
// }
// };
private void DeleteOrder(Order order)
{
var exportLines = new List<ReviewExportLine>()
{
new ReviewExportLine()
{
Type = "9",
OrderNumber = FilterString(order.OrderNumber),
Amount = "1",
UserId = FilterString(order.User),
JournalPostIndication = "Y"
}
};
// exportLineQueue.Enqueue(exportLines);
// }
exportLineQueue.Enqueue(exportLines);
}
// private List<ReviewExportLine> ConvertOrderToExportLines(Order order, string userId = null)
// {
// var exportLines = new List<ReviewExportLine>();
private List<ReviewExportLine> ConvertOrderToExportLines(Order order, string userId = null)
{
var exportLines = new List<ReviewExportLine>();
// foreach (var orderLine in order.OrderLines)
// {
// exportLines.Add(new ReviewExportLine()
// {
// OrderNumber = FilterString(order.OrderNumber),
// Name = FilterString(orderLine.Name),
// Amount = FilterString(orderLine.Amount),
// ProductNumber = FilterString(orderLine.Number),
// ProductDescription = FilterString(orderLine.Description),
// UserId = userId == null ? FilterString(order.User) : userId,
// Location = FilterString(orderLine.Location),
// JournalPostIndication = "Y"
// });
// }
foreach (var orderLine in order.OrderLines)
{
exportLines.Add(new ReviewExportLine()
{
OrderNumber = FilterString(order.OrderNumber),
Name = FilterString(orderLine.Name),
Amount = FilterString(orderLine.Amount),
ProductNumber = FilterString(orderLine.Number),
ProductDescription = FilterString(orderLine.Description),
UserId = userId == null ? FilterString(order.User) : userId,
Location = FilterString(orderLine.Location),
JournalPostIndication = "Y"
});
}
// return exportLines;
// }
return exportLines;
}
// private void ProcessReviewExportLines()
// {
// while(true)
// {
// if (exportLineQueue.Any() && IsFolderEmpty(options.ReviewFolderPath))
// {
// var exportLines = exportLineQueue.Dequeue();
// var randomStringLength = 7;
private void ProcessReviewExportLines()
{
while(true)
{
if (exportLineQueue.Any() && IsFolderEmpty(options.ReviewFolderPath))
{
var exportLines = exportLineQueue.Dequeue();
var randomStringLength = 7;
// writer.OpenFile($"{options.ReviewFolderPath}\\{GenerateRandomString(randomStringLength)}.txt");
writer.OpenFile($"{options.ReviewFolderPath}\\{GenerateRandomString(randomStringLength)}.txt");
// foreach (var exportLine in exportLines)
// {
// writer.WriteLine($"{exportLine.Type};" +
// $"{exportLine.OrderNumber};" +
// $"{exportLine.Name};" +
// $"{exportLine.Amount};" +
// $"{exportLine.ProductNumber};" +
// $"{exportLine.ProductDescription};" +
// $"{exportLine.UserId};" +
// $"{exportLine.Location};" +
// $"{exportLine.JournalPostIndication};" +
// $"{exportLine.Info2}");
// }
foreach (var exportLine in exportLines)
{
writer.WriteLine($"{exportLine.Type};" +
$"{exportLine.OrderNumber};" +
$"{exportLine.Name};" +
$"{exportLine.Amount};" +
$"{exportLine.ProductNumber};" +
$"{exportLine.ProductDescription};" +
$"{exportLine.UserId};" +
$"{exportLine.Location};" +
$"{exportLine.JournalPostIndication};" +
$"{exportLine.Info2}");
}
// writer.CloseFile();
// }
writer.CloseFile();
}
// Thread.Sleep(2000);
// }
// }
Thread.Sleep(2000);
}
}
// private bool IsFolderEmpty(string path)
// {
// DirectoryInfo dirInfo = new DirectoryInfo(path);
private bool IsFolderEmpty(string path)
{
DirectoryInfo dirInfo = new DirectoryInfo(path);
// if (!dirInfo.Exists)
// {
// throw new CouldNotFindReviewDirectory();
// }
if (!dirInfo.Exists)
{
throw new CouldNotFindReviewDirectory();
}
// return !dirInfo.GetFiles("*.txt").Any();
// }
return !dirInfo.GetFiles("*.txt").Any();
}
// private string GenerateRandomString(int length)
// {
// StringBuilder stringBuilder = new StringBuilder();
// Random random = new Random();
// var ASCIIOffset = 65;
// var ASCIIRange = 25;
private string GenerateRandomString(int length)
{
StringBuilder stringBuilder = new StringBuilder();
Random random = new Random();
var ASCIIOffset = 65;
var ASCIIRange = 25;
// char letter;
char letter;
// for (int i = 0; i < length; i++)
// {
// double randomDouble = random.NextDouble();
// int shift = Convert.ToInt32(Math.Floor(ASCIIRange * randomDouble));
// letter = Convert.ToChar(shift + ASCIIOffset);
// stringBuilder.Append(letter);
// }
for (int i = 0; i < length; i++)
{
double randomDouble = random.NextDouble();
int shift = Convert.ToInt32(Math.Floor(ASCIIRange * randomDouble));
letter = Convert.ToChar(shift + ASCIIOffset);
stringBuilder.Append(letter);
}
// return stringBuilder.ToString();
// }
// }
// }
return stringBuilder.ToString();
}
}
}

24
services/CsvWriter.cs Normal file
View File

@ -0,0 +1,24 @@
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.Flush();
csvWriter.NextRecord();
}
}

View File

@ -0,0 +1,93 @@
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
var strippedArgs = args.SkipLast(1).ToArray();
string[] givenFlags = strippedArgs.Where((value, index) => index % 2 == 0).ToArray();
string[] givenFilePaths = strippedArgs.Where((value, index) => index % 2 != 0).ToArray();
try
{
// Assert only a single 'd' was passed
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(),
};
}
// Assert only '-f' were passed
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', or all flags must be '-f");
}
} catch (Exception)
{
Console.WriteLine("Something 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)
{
}
}
}
}