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? GenericAttributes { get; set; } } public class OrderDtoTableItem : OrderDto { //[Newtonsoft.Json.JsonProperty] //public new ObservableCollection? GenericAttributes { get; set; } //[Newtonsoft.Json.JsonProperty] //public new ObservableCollection? OrderItemDtos { get; set; } } public class ProductDtoTable(FruitBankSignalRClient fruitBankSignalRClient) : ObservableCollection { private readonly SemaphoreSlim _semaphoreSlim = new(1); public async Task 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 { private readonly SemaphoreSlim _semaphoreSlim = new(1); public async Task 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()! as ProductDtoTable)!; //set => AddRows(value); } public OrderDtoTable OrderDtoTable => (GetRows()! as OrderDtoTable)!; } public abstract class DatabaseClientBase { //private ObservableCollection a = new ObservableCollection(); private readonly ConcurrentDictionary _database = new(); protected void AddTable(IList table)where TEntity: class, IEntityInt { if (GetTableObject() == null) { if (!_database.TryAdd(typeof(TEntity), table)) return; } return; } protected IList? GetTableObject() where T : class, IEntityInt { return _database.GetValueOrDefault(typeof(T)) as IList; } public IEnumerable? GetRows() where T : class, IEntityInt { return GetTableObject(); } public T? GetRow(int id) where T : class, IEntityInt { return GetTableObject()?.FirstOrDefault(x => x.Id == id) as T; } public T AddRow(T entity) where T : class, IEntityInt => AddRow(GetTableObject()!, entity); protected T AddRow(in IList table, T entity) where T : class, IEntityInt { table.Add(entity); return entity; } public void AddRows(IEnumerable entities) where T : class, IEntityInt { var table = GetTableObject()!; foreach (var entity in entities) { AddRow(table, entity); } } public T? DeleteRow(int id) where T : class, IEntityInt { var table = GetTableObject(); 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; } }