160 lines
4.5 KiB
C#
160 lines
4.5 KiB
C#
using AyCode.Core.Extensions;
|
|
using AyCode.Interfaces.Entities;
|
|
using AyCode.Utils.Wrappers;
|
|
using FruitBank.Common.Dtos;
|
|
using FruitBankHybrid.Shared.Services.SignalRs;
|
|
using Nop.Core.Domain.Common;
|
|
using Nop.Core.Domain.Orders;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.ObjectModel;
|
|
using AyCode.Utils.Extensions;
|
|
|
|
namespace FruitBankHybrid.Shared.Databases;
|
|
|
|
public class ProductDtoTableItem : ProductDto
|
|
{
|
|
//[Newtonsoft.Json.JsonProperty]
|
|
//public new ObservableCollection<GenericAttribute>? GenericAttributes { get; set; }
|
|
}
|
|
|
|
public class OrderDtoTableItem : OrderDto
|
|
{
|
|
//[Newtonsoft.Json.JsonProperty]
|
|
//public new ObservableCollection<GenericAttribute>? GenericAttributes { get; set; }
|
|
//[Newtonsoft.Json.JsonProperty]
|
|
//public new ObservableCollection<GenericAttribute>? OrderItemDtos { get; set; }
|
|
}
|
|
|
|
public class ProductDtoTable(FruitBankSignalRClient fruitBankSignalRClient) : ObservableCollection<ProductDtoTableItem>
|
|
{
|
|
private readonly SemaphoreSlim _semaphoreSlim = new(1);
|
|
public async Task<ProductDtoTable> LoadDataAsync(bool onlyIfEmpty = true)
|
|
{
|
|
if (onlyIfEmpty && Count != 0) return this;
|
|
|
|
using (await _semaphoreSlim.UseWaitAsync())
|
|
{
|
|
if (Count != 0) return this;
|
|
|
|
var items = (await fruitBankSignalRClient.GetProductDtoTableItems() ?? []);
|
|
|
|
Clear();
|
|
foreach (var productDto in items)
|
|
{
|
|
this.Add(productDto);
|
|
}
|
|
}
|
|
|
|
return this;
|
|
}
|
|
}
|
|
public class OrderDtoTable(FruitBankSignalRClient fruitBankSignalRClient) : ObservableCollection<OrderDtoTableItem>
|
|
{
|
|
private readonly SemaphoreSlim _semaphoreSlim = new(1);
|
|
|
|
public async Task<OrderDtoTable> LoadDataAsync(bool onlyIfEmpty = true)
|
|
{
|
|
if (onlyIfEmpty && Count != 0) return this;
|
|
|
|
using (await _semaphoreSlim.UseWaitAsync())
|
|
{
|
|
if (Count != 0) return this;
|
|
|
|
var items = (await fruitBankSignalRClient.GetAllOrderDtoTableItems() ?? []);
|
|
|
|
Clear();
|
|
foreach (var orderDto in items)
|
|
{
|
|
this.Add(orderDto);
|
|
}
|
|
}
|
|
|
|
return this;
|
|
}
|
|
}
|
|
|
|
public class DatabaseClient : DatabaseClientBase
|
|
{
|
|
private FruitBankSignalRClient _fruitBankSignalRClient;
|
|
|
|
public DatabaseClient(FruitBankSignalRClient fruitBankSignalRClient)
|
|
{
|
|
_fruitBankSignalRClient = fruitBankSignalRClient;
|
|
|
|
AddTable(new ProductDtoTable(_fruitBankSignalRClient));
|
|
AddTable(new OrderDtoTable(_fruitBankSignalRClient));
|
|
}
|
|
|
|
public ProductDtoTable ProductDtoTable
|
|
{
|
|
get => (GetRows<ProductDtoTableItem>()! as ProductDtoTable)!;
|
|
//set => AddRows(value);
|
|
}
|
|
|
|
public OrderDtoTable OrderDtoTable => (GetRows<OrderDtoTableItem>()! as OrderDtoTable)!;
|
|
}
|
|
|
|
public abstract class DatabaseClientBase
|
|
{
|
|
//private ObservableCollection<IEntityInt> a = new ObservableCollection<IEntityInt>();
|
|
private readonly ConcurrentDictionary<Type, object> _database = new();
|
|
|
|
protected void AddTable<TEntity>(IList<TEntity> table)where TEntity: class, IEntityInt
|
|
{
|
|
if (GetTableObject<TEntity>() == null)
|
|
{
|
|
if (!_database.TryAdd(typeof(TEntity), table))
|
|
return;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
protected IList<T>? GetTableObject<T>() where T : class, IEntityInt
|
|
{
|
|
return _database.GetValueOrDefault(typeof(T)) as IList<T>;
|
|
}
|
|
|
|
public IEnumerable<T>? GetRows<T>() where T : class, IEntityInt
|
|
{
|
|
return GetTableObject<T>();
|
|
}
|
|
|
|
public T? GetRow<T>(int id) where T : class, IEntityInt
|
|
{
|
|
return GetTableObject<T>()?.FirstOrDefault(x => x.Id == id) as T;
|
|
}
|
|
|
|
public T AddRow<T>(T entity) where T : class, IEntityInt
|
|
=> AddRow(GetTableObject<T>()!, entity);
|
|
|
|
protected T AddRow<T>(in IList<T> table, T entity) where T : class, IEntityInt
|
|
{
|
|
table.Add(entity);
|
|
return entity;
|
|
}
|
|
|
|
public void AddRows<T>(IEnumerable<T> entities) where T : class, IEntityInt
|
|
{
|
|
var table = GetTableObject<T>()!;
|
|
|
|
foreach (var entity in entities)
|
|
{
|
|
AddRow(table, entity);
|
|
}
|
|
}
|
|
|
|
public T? DeleteRow<T>(int id) where T : class, IEntityInt
|
|
{
|
|
var table = GetTableObject<T>();
|
|
if (table == null) return null;
|
|
|
|
var index = table.FindIndex(x => x.Id == id);
|
|
if (index < 0) return null;
|
|
|
|
var entity = table[index];
|
|
table.RemoveAt(index);
|
|
|
|
return entity as T;
|
|
}
|
|
} |