Compare commits
4 commits
55f80278ed
...
5366b11f55
Author | SHA1 | Date | |
---|---|---|---|
5366b11f55 | |||
2550724333 | |||
4d6f9092eb | |||
9e2565d110 |
14 changed files with 647 additions and 130 deletions
265
.gitignore
vendored
265
.gitignore
vendored
|
@ -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 ###
|
### Csharp ###
|
||||||
## Ignore Visual Studio temporary files, build results, and
|
## Ignore Visual Studio temporary files, build results, and
|
||||||
## files generated by popular Visual Studio add-ons.
|
## files generated by popular Visual Studio add-ons.
|
||||||
|
@ -405,6 +408,85 @@ obj/
|
||||||
/node_modules
|
/node_modules
|
||||||
/wwwroot/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 ###
|
### VisualStudioCode ###
|
||||||
!.vscode/*.code-snippets
|
!.vscode/*.code-snippets
|
||||||
|
|
||||||
|
@ -418,4 +500,185 @@ obj/
|
||||||
.history
|
.history
|
||||||
.ionide
|
.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
4
.vscode/launch.json
vendored
|
@ -1,6 +1,8 @@
|
||||||
{
|
{
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
// Use IntelliSense to find out which attributes exist for C# debugging
|
// Use IntelliSense to find out which attributes exist for C# debugging
|
||||||
|
@ -12,7 +14,7 @@
|
||||||
"preLaunchTask": "build",
|
"preLaunchTask": "build",
|
||||||
// If you have changed target frameworks, make sure to update the program path.
|
// If you have changed target frameworks, make sure to update the program path.
|
||||||
"program": "${workspaceFolder}/bin/Debug/net7.0/DemoApplication.dll",
|
"program": "${workspaceFolder}/bin/Debug/net7.0/DemoApplication.dll",
|
||||||
"args": [],
|
"args": ["-f", "assets/input1.json", "-f", "assets/input2.json", "out.csv"],
|
||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
|
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
|
||||||
"console": "internalConsole",
|
"console": "internalConsole",
|
||||||
|
|
|
@ -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>
|
||||||
|
|
27
Problems.md
Normal file
27
Problems.md
Normal 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.
|
||||||
|
|
25
Program.cs
25
Program.cs
|
@ -1,10 +1,31 @@
|
||||||
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 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
31
README.md
Normal 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.
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"OrderId": 1,
|
"OrderId": 1,
|
||||||
"Description": " Order 123",
|
"Description": "Order 123",
|
||||||
"customer ID": 123,
|
"customer ID": 123,
|
||||||
"products": [{
|
"products": [{
|
||||||
"ProductId": "PID1",
|
"ProductId": "PID1",
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
{
|
{
|
||||||
"OrderId": 3,
|
"OrderId": 3,
|
||||||
"Description": " Order 11234",
|
"Description": "Order 11234",
|
||||||
"customer ID": 1134,
|
"customer ID": 1134,
|
||||||
"products": [{
|
"products": [{
|
||||||
"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/OrderDTO.cs
Normal file
34
data/dtos/OrderDTO.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.Trim();
|
||||||
|
CustomerId = orderDTO.CustomerId;
|
||||||
|
TotalPrice = orderDTO.Products.Select(product => product.Price).Sum();
|
||||||
|
}
|
||||||
|
}
|
244
review.cs
244
review.cs
|
@ -1,144 +1,144 @@
|
||||||
// using SollicitantReview.Models;
|
using SollicitantReview.Models;
|
||||||
// using SollicitantReview.Services;
|
using SollicitantReview.Services;
|
||||||
// using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
// using System;
|
using System;
|
||||||
// using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
// using System.IO;
|
using System.IO;
|
||||||
// using System.Linq;
|
using System.Linq;
|
||||||
// using System.Text;
|
using System.Text;
|
||||||
// using System.Threading;
|
using System.Threading;
|
||||||
// using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
// namespace SollicitantReview
|
namespace SollicitantReview
|
||||||
// {
|
{
|
||||||
// public class SollicitantReview
|
public class SollicitantReview
|
||||||
// {
|
{
|
||||||
// private SollicitantReviewSettings options;
|
private SollicitantReviewSettings options;
|
||||||
// private IWriter writer;
|
private IWriter writer;
|
||||||
// private Queue<List<ReviewExportLine>> exportLineQueue;
|
private Queue<List<ReviewExportLine>> exportLineQueue;
|
||||||
|
|
||||||
// public SollicitantReview(IOptions<SollicitantReviewSettings> options, IWriter writer)
|
public SollicitantReview(IOptions<SollicitantReviewSettings> options, IWriter writer)
|
||||||
// {
|
{
|
||||||
// this.options = options.Value;
|
this.options = options.Value;
|
||||||
// this.writer = writer;
|
this.writer = writer;
|
||||||
// this.exportLineQueue = new Queue<List<ReviewExportLine>>();
|
this.exportLineQueue = new Queue<List<ReviewExportLine>>();
|
||||||
|
|
||||||
// var processExportLinesThread = new Thread(new ThreadStart(ProcessReviewExportLines));
|
var processExportLinesThread = new Thread(new ThreadStart(ProcessReviewExportLines));
|
||||||
// processExportLinesThread.Start();
|
processExportLinesThread.Start();
|
||||||
// }
|
}
|
||||||
|
|
||||||
// public void RegisterOrder(Order order)
|
public void RegisterOrder(Order order)
|
||||||
// {
|
{
|
||||||
// DeleteOrder(order);
|
DeleteOrder(order);
|
||||||
// exportLineQueue.Enqueue(ConvertOrderToExportLines(order));
|
exportLineQueue.Enqueue(ConvertOrderToExportLines(order));
|
||||||
// }
|
}
|
||||||
|
|
||||||
// private string FilterString(string value)
|
private string FilterString(string value)
|
||||||
// {
|
{
|
||||||
// return value.Replace(";", ":");
|
return value.Replace(";", ":");
|
||||||
// }
|
}
|
||||||
|
|
||||||
// private void DeleteOrder(Order order)
|
private void DeleteOrder(Order order)
|
||||||
// {
|
{
|
||||||
// var exportLines = new List<ReviewExportLine>()
|
var exportLines = new List<ReviewExportLine>()
|
||||||
// {
|
{
|
||||||
// new ReviewExportLine()
|
new ReviewExportLine()
|
||||||
// {
|
{
|
||||||
// Type = "9",
|
Type = "9",
|
||||||
// OrderNumber = FilterString(order.OrderNumber),
|
OrderNumber = FilterString(order.OrderNumber),
|
||||||
// Amount = "1",
|
Amount = "1",
|
||||||
// UserId = FilterString(order.User),
|
UserId = FilterString(order.User),
|
||||||
// JournalPostIndication = "Y"
|
JournalPostIndication = "Y"
|
||||||
// }
|
}
|
||||||
// };
|
};
|
||||||
|
|
||||||
// exportLineQueue.Enqueue(exportLines);
|
exportLineQueue.Enqueue(exportLines);
|
||||||
// }
|
}
|
||||||
|
|
||||||
// private List<ReviewExportLine> ConvertOrderToExportLines(Order order, string userId = null)
|
private List<ReviewExportLine> ConvertOrderToExportLines(Order order, string userId = null)
|
||||||
// {
|
{
|
||||||
// var exportLines = new List<ReviewExportLine>();
|
var exportLines = new List<ReviewExportLine>();
|
||||||
|
|
||||||
// foreach (var orderLine in order.OrderLines)
|
foreach (var orderLine in order.OrderLines)
|
||||||
// {
|
{
|
||||||
// exportLines.Add(new ReviewExportLine()
|
exportLines.Add(new ReviewExportLine()
|
||||||
// {
|
{
|
||||||
// OrderNumber = FilterString(order.OrderNumber),
|
OrderNumber = FilterString(order.OrderNumber),
|
||||||
// Name = FilterString(orderLine.Name),
|
Name = FilterString(orderLine.Name),
|
||||||
// Amount = FilterString(orderLine.Amount),
|
Amount = FilterString(orderLine.Amount),
|
||||||
// ProductNumber = FilterString(orderLine.Number),
|
ProductNumber = FilterString(orderLine.Number),
|
||||||
// ProductDescription = FilterString(orderLine.Description),
|
ProductDescription = FilterString(orderLine.Description),
|
||||||
// UserId = userId == null ? FilterString(order.User) : userId,
|
UserId = userId == null ? FilterString(order.User) : userId,
|
||||||
// Location = FilterString(orderLine.Location),
|
Location = FilterString(orderLine.Location),
|
||||||
// JournalPostIndication = "Y"
|
JournalPostIndication = "Y"
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
|
|
||||||
// return exportLines;
|
return exportLines;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// private void ProcessReviewExportLines()
|
private void ProcessReviewExportLines()
|
||||||
// {
|
{
|
||||||
// while(true)
|
while(true)
|
||||||
// {
|
{
|
||||||
// if (exportLineQueue.Any() && IsFolderEmpty(options.ReviewFolderPath))
|
if (exportLineQueue.Any() && IsFolderEmpty(options.ReviewFolderPath))
|
||||||
// {
|
{
|
||||||
// var exportLines = exportLineQueue.Dequeue();
|
var exportLines = exportLineQueue.Dequeue();
|
||||||
// var randomStringLength = 7;
|
var randomStringLength = 7;
|
||||||
|
|
||||||
// writer.OpenFile($"{options.ReviewFolderPath}\\{GenerateRandomString(randomStringLength)}.txt");
|
writer.OpenFile($"{options.ReviewFolderPath}\\{GenerateRandomString(randomStringLength)}.txt");
|
||||||
|
|
||||||
// foreach (var exportLine in exportLines)
|
foreach (var exportLine in exportLines)
|
||||||
// {
|
{
|
||||||
// writer.WriteLine($"{exportLine.Type};" +
|
writer.WriteLine($"{exportLine.Type};" +
|
||||||
// $"{exportLine.OrderNumber};" +
|
$"{exportLine.OrderNumber};" +
|
||||||
// $"{exportLine.Name};" +
|
$"{exportLine.Name};" +
|
||||||
// $"{exportLine.Amount};" +
|
$"{exportLine.Amount};" +
|
||||||
// $"{exportLine.ProductNumber};" +
|
$"{exportLine.ProductNumber};" +
|
||||||
// $"{exportLine.ProductDescription};" +
|
$"{exportLine.ProductDescription};" +
|
||||||
// $"{exportLine.UserId};" +
|
$"{exportLine.UserId};" +
|
||||||
// $"{exportLine.Location};" +
|
$"{exportLine.Location};" +
|
||||||
// $"{exportLine.JournalPostIndication};" +
|
$"{exportLine.JournalPostIndication};" +
|
||||||
// $"{exportLine.Info2}");
|
$"{exportLine.Info2}");
|
||||||
// }
|
}
|
||||||
|
|
||||||
// writer.CloseFile();
|
writer.CloseFile();
|
||||||
// }
|
}
|
||||||
|
|
||||||
// Thread.Sleep(2000);
|
Thread.Sleep(2000);
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// private bool IsFolderEmpty(string path)
|
private bool IsFolderEmpty(string path)
|
||||||
// {
|
{
|
||||||
// DirectoryInfo dirInfo = new DirectoryInfo(path);
|
DirectoryInfo dirInfo = new DirectoryInfo(path);
|
||||||
|
|
||||||
// if (!dirInfo.Exists)
|
if (!dirInfo.Exists)
|
||||||
// {
|
{
|
||||||
// throw new CouldNotFindReviewDirectory();
|
throw new CouldNotFindReviewDirectory();
|
||||||
// }
|
}
|
||||||
|
|
||||||
// return !dirInfo.GetFiles("*.txt").Any();
|
return !dirInfo.GetFiles("*.txt").Any();
|
||||||
// }
|
}
|
||||||
|
|
||||||
// private string GenerateRandomString(int length)
|
private string GenerateRandomString(int length)
|
||||||
// {
|
{
|
||||||
// StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
// Random random = new Random();
|
Random random = new Random();
|
||||||
// var ASCIIOffset = 65;
|
var ASCIIOffset = 65;
|
||||||
// var ASCIIRange = 25;
|
var ASCIIRange = 25;
|
||||||
|
|
||||||
// char letter;
|
char letter;
|
||||||
|
|
||||||
// for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
// {
|
{
|
||||||
// double randomDouble = random.NextDouble();
|
double randomDouble = random.NextDouble();
|
||||||
// int shift = Convert.ToInt32(Math.Floor(ASCIIRange * randomDouble));
|
int shift = Convert.ToInt32(Math.Floor(ASCIIRange * randomDouble));
|
||||||
// letter = Convert.ToChar(shift + ASCIIOffset);
|
letter = Convert.ToChar(shift + ASCIIOffset);
|
||||||
// stringBuilder.Append(letter);
|
stringBuilder.Append(letter);
|
||||||
// }
|
}
|
||||||
|
|
||||||
// return stringBuilder.ToString();
|
return stringBuilder.ToString();
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
24
services/CsvWriter.cs
Normal file
24
services/CsvWriter.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
93
services/DemoApplicationConfiguration.cs
Normal file
93
services/DemoApplicationConfiguration.cs
Normal 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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue