Skip to content

Commit

Permalink
feature: allow to choose sources for stock quantity conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
anion0278 committed Apr 14, 2024
1 parent cdea6f5 commit 0331da0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ namespace Mapp.BusinessLogic.StockQuantity;

public interface IStockQuantityUpdater
{
Task<IEnumerable<StockData>> ConvertWarehouseData();
IReadOnlyList<StockDataXmlSourceDefinition> SourceDefinitions { get; }
Task<IEnumerable<StockData>> ConvertWarehouseData(IReadOnlyList<StockDataXmlSourceDefinition> sources);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ public class StockQuantityUpdater : IStockQuantityUpdater
{
private readonly IJsonManager _jsonManager;
private readonly IDialogService _dialogService;
private readonly IEnumerable<StockDataXmlSourceDefinition> _sourceDefinitions;
public IReadOnlyList<StockDataXmlSourceDefinition> SourceDefinitions { get; }

public StockQuantityUpdater(IJsonManager jsonManager, IDialogService dialogService)
{
_jsonManager = jsonManager;
_dialogService = dialogService;
_sourceDefinitions = _jsonManager.LoadStockQuantityUpdaterConfigs();
SourceDefinitions = _jsonManager.LoadStockQuantityUpdaterConfigs();
}

public async Task<IEnumerable<StockData>> ConvertWarehouseData()
public async Task<IEnumerable<StockData>> ConvertWarehouseData(IReadOnlyList<StockDataXmlSourceDefinition> sources)
{
var httpClient = new HttpClient();

var stockDataTotal = new List<StockData>();

Dictionary<string, int> statistics = new Dictionary<string, int>();

foreach (var source in _sourceDefinitions)
foreach (var source in sources)
{
var stream = await (await httpClient.GetAsync(source.Url)).Content.ReadAsStreamAsync();
var stockData = ExtractStockData(stream, source);
Expand Down
16 changes: 14 additions & 2 deletions Mapp.DataAccess/JsonManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public interface IJsonManager
Dictionary<string, string> DeserializeJsonDictionary(string fileName);
void SerializeDictionaryToJson(Dictionary<string, string> map, string fileName);
IEnumerable<MarketPlaceTransactionsConfigData> LoadTransactionsConfigs();
IEnumerable<StockDataXmlSourceDefinition> LoadStockQuantityUpdaterConfigs();
IReadOnlyList<StockDataXmlSourceDefinition> LoadStockQuantityUpdaterConfigs();
void SaveStockQuantityUpdaterConfigs(IReadOnlyList<StockDataXmlSourceDefinition> definitions);
}

public class JsonManager : IJsonManager
Expand Down Expand Up @@ -79,7 +80,7 @@ public IEnumerable<MarketPlaceTransactionsConfigData> LoadTransactionsConfigs()
return configDtos;
}

public IEnumerable<StockDataXmlSourceDefinition> LoadStockQuantityUpdaterConfigs()
public IReadOnlyList<StockDataXmlSourceDefinition> LoadStockQuantityUpdaterConfigs()
{
var serializeOptions = new JsonSerializerOptions
{
Expand All @@ -89,5 +90,16 @@ public IEnumerable<StockDataXmlSourceDefinition> LoadStockQuantityUpdaterConfigs
string json = _fileManager.ReadAllTextFromFile(Path.Join("StockQuantityUpdater", "config.json"));
return JsonSerializer.Deserialize<StockDataXmlSourceDefinition[]>(json, serializeOptions);
}

public void SaveStockQuantityUpdaterConfigs(IReadOnlyList<StockDataXmlSourceDefinition> definitions)
{
var serializeOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
var json = JsonSerializer.Serialize(definitions, serializeOptions);
_fileManager.WriteAllTextToFile(Path.Join("StockQuantityUpdater", "config.json"), json);
}
}
}
1 change: 1 addition & 0 deletions Mapp.Models/StockQuantity/StockDataXmlSourceDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Mapp.Models.StockQuantity;
public record StockDataXmlSourceDefinition()
{
public string Url { get; init; }
public bool? IsEnabled { get; set; }
public string ItemNodeName { get; init; }
public IEnumerable<ValueParsingOption> SkuNodeParsingOptions { get; init; }
public IEnumerable<ValueParsingOption> StockQuantityNodeParsingOptions { get; init; }
Expand Down
2 changes: 2 additions & 0 deletions Mapp.UI/Startup/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Mapp.UI.Extensions;
using Mapp.UI.Settings;
using Mapp.UI.Views;
using Mapp.BusinessLogic.StockQuantity;

namespace Mapp.UI.Startup;

Expand Down Expand Up @@ -44,6 +45,7 @@ public IContainer ConfigureContainer()
builder.RegisterAsInterfaceSingleton<FileManager>();
builder.RegisterAsInterfaceSingleton<DateTimeManager>();
builder.RegisterAsInterfaceSingleton<KeyboardHook>();
builder.RegisterAsInterfaceSingleton<StockQuantityUpdater>();
builder.RegisterAsInterfaceSingleton<InputSimulator>();

builder.RegisterAsInterfaceSingleton<MainViewModel>();
Expand Down
49 changes: 15 additions & 34 deletions Mapp.UI/ViewModels/WarehouseQuantityUpdaterViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,62 +24,44 @@ public class WarehouseQuantityUpdaterViewModel : TabViewModelBase, IWarehouseQua
{
private readonly ISettingsWrapper _settingsWrapper;
private readonly IJsonManager _jsonManager;
private readonly IStockQuantityUpdater stockQuantityUpdater;
private readonly IFileOperationService _fileOperationService;
private readonly IStockQuantityUpdater _stockQuantityUpdater;
private readonly IDialogService _dialogService;
public ICommand ConvertWarehouseDataCommand { get; set; }

public IReadOnlyList<StockDataXmlSourceDefinition> SourceDefinitions { get; }

public override string Title => LocalizationStrings.QuantityUpdaterTabTitle.GetLocalized();

public WarehouseQuantityUpdaterViewModel(
ISettingsWrapper settingsWrapper,
IJsonManager jsonManager,
IStockQuantityUpdater stockQuantityUpdater,
IFileOperationService fileOperationService,
IDialogService dialogService)
IDialogService dialogService)
{
_settingsWrapper = settingsWrapper;
_jsonManager = jsonManager;
_stockQuantityUpdater = stockQuantityUpdater;
_fileOperationService = fileOperationService;
_dialogService = dialogService;

//AsyncRelayCommandOptions.None - disable button during execution of Async Task (AllowConcurrentExecutions = false)
ConvertWarehouseDataCommand = new AsyncRelayCommand(ConvertWarehouseData, AsyncRelayCommandOptions.None);

SourceDefinitions = _stockQuantityUpdater.SourceDefinitions;
foreach (var sourceDefinition in SourceDefinitions)
{
if (sourceDefinition.IsEnabled == null) sourceDefinition.IsEnabled = true;
}
}

private async Task ConvertWarehouseData()
{
StockDataXmlSourceDefinition[] sources =
{
new()
{
Url = "https://www.rappa.cz/export/vo.xml",
ItemNodeName = "SHOPITEM",
SkuNodeParsingOptions = new[]
{
new ValueParsingOption("EAN", null),
},
StockQuantityNodeParsingOptions = new[]
{
new ValueParsingOption("STOCK", null),
},
},
new()
{
Url = "https://en.bushman.eu/content/feeds/uQ5TueFNQh_expando_4.xml",
ItemNodeName = "item",
SkuNodeParsingOptions = new[]
{
new ValueParsingOption("g:gtin", null),
new ValueParsingOption("g:sku_with_ean", @"\d{13}"),
},
StockQuantityNodeParsingOptions = new[]
{
new ValueParsingOption("g:quantity", null),
},
}
};
_jsonManager.SaveStockQuantityUpdaterConfigs(SourceDefinitions);

var stockQuantityUpdater = new StockQuantityUpdater(_jsonManager, _dialogService);
var stockData = await stockQuantityUpdater.ConvertWarehouseData();
var stockData = await _stockQuantityUpdater.ConvertWarehouseData(SourceDefinitions.Where(s => s.IsEnabled == true).ToList());
var columnNamesLine =
"sku\tprice\tminimum-seller-allowed-price\tmaximum-seller-allowed-price\tquantity\thandling-time\tfulfillment-channel";
var lines = new List<string>(stockData.Count() + 1) { columnNamesLine };
Expand All @@ -104,5 +86,4 @@ private async Task ConvertWarehouseData()
_fileOperationService.OpenFileFolder(saveFileDialog.FileName);
}
}

}
13 changes: 11 additions & 2 deletions Mapp.UI/Views/WarehouseQuantityUpdaterView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Mapp.UI.Views.Converters"
mc:Ignorable="d"
xmlns:local="clr-namespace:Mapp.UI.Views.Converters" xmlns:viewmodels="clr-namespace:Mapp.UI.ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance viewmodels:WarehouseQuantityUpdaterViewModel, IsDesignTimeCreatable=False}"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
Expand All @@ -13,6 +14,13 @@
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Grid.Row="1">
<DataGrid ItemsSource="{Binding SourceDefinitions}" AutoGenerateColumns="False" HorizontalAlignment="Center">
<DataGrid.Columns>
<DataGridTextColumn Header="URL" IsReadOnly="True" Binding="{Binding Url}"/>
<DataGridCheckBoxColumn Header="Aktivni" Binding="{Binding IsEnabled}"/>
</DataGrid.Columns>
</DataGrid>

<Button Width="200" Height="35" Command="{Binding ConvertWarehouseDataCommand}" >
<Button.Style>
<Style TargetType="Button">
Expand All @@ -25,6 +33,7 @@
</Style>
</Button.Style>
</Button>

</StackPanel>
</Grid>
</UserControl>

0 comments on commit 0331da0

Please sign in to comment.