using System.Linq.Expressions; using LinqToDB; using Mango.Nop.Core.Loggers; using Nop.Core; using Nop.Core.Caching; using Nop.Core.Configuration; using Nop.Core.Domain.Orders; using Nop.Core.Events; using Nop.Data; namespace Mango.Nop.Data.Repositories; public abstract class MgDtoDbTableBase : MgDbTableBase where TDtoEntity : BaseEntity/*, IMgModelDtoBase*/ where TMainEntity : BaseEntity { public Type MainEntityType { get; } = typeof(Order); public MgDtoDbTableBase(IEventPublisher eventPublisher, INopDataProvider dataProvider, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, AppSettings appSettings) : base(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings) { } public async Task GetMainEntityById(int id) => await _dataProvider.GetTable().FirstOrDefaultAsync(x => x.Id == id); public virtual async Task DeleteMainEntityById(int id, bool publishEvent = true) { var affectedRows = 0; var mainEntity = await GetMainEntityById(id); if (mainEntity == null) return affectedRows; affectedRows = await _dataProvider.GetTable().DeleteAsync(x => x.Id == id); if (publishEvent) await _eventPublisher.PublishAsync(new EntityDeletedEvent(mainEntity)); return affectedRows; } public Task HandleEventAsync(EntityDeletedEvent eventMessage) => throw new Exception($"To delete, you must use the DeleteMainEntityById<{MainEntityType.Name}> function instead of '{typeof(TDtoEntity).Name}'!"); public async Task HandleEventAsync(EntityInsertedEvent eventMessage) { var mainEntity = await GetMainEntityById(eventMessage.Entity.Id); if (mainEntity == null) throw new Exception($"MgDtoDbTableBase<{typeof(TDtoEntity).Name}, {MainEntityType.Name}>->EntityInsertedEvent<{typeof(TDtoEntity).Name}>(); (mainEntity == null); Id: {eventMessage.Entity.Id}"); await _eventPublisher.PublishAsync(new EntityInsertedEvent(mainEntity)); } public async Task HandleEventAsync(EntityUpdatedEvent eventMessage) { var mainEntity = await GetMainEntityById(eventMessage.Entity.Id); if (mainEntity == null) throw new Exception($"MgDtoDbTableBase<{typeof(TDtoEntity).Name}, {MainEntityType.Name}>->EntityUpdatedEvent<{typeof(TDtoEntity).Name}>(); (mainEntity == null); Id: {eventMessage.Entity.Id}"); await _eventPublisher.PublishAsync(new EntityUpdatedEvent(mainEntity)); } public override Task DeleteAsync(int entityId, bool publishEvent = true) => DeleteMainEntityById(entityId, publishEvent); public override Task DeleteAsync(TDtoEntity entity, bool publishEvent = true) => DeleteMainEntityById(entity.Id, publishEvent); public override Task DeleteAsync(IList entities, bool publishEvent = true) => throw new Exception($"To delete, you must use the DeleteMainEntityById<{MainEntityType.Name}> function instead of '{typeof(TDtoEntity).Name}'!"); public override Task DeleteAsync(Expression> predicate) => throw new Exception($"To delete, you must use the DeleteMainEntityById<{MainEntityType.Name}> function instead of '{typeof(TDtoEntity).Name}'!"); public override Task DeleteAsync(Expression> predicate, bool publishEvent) => throw new Exception($"To delete, you must use the DeleteMainEntityById<{MainEntityType.Name}> function instead of '{typeof(TDtoEntity).Name}'!"); public override void Delete(TDtoEntity entity, bool publishEvent = true) => throw new Exception($"To delete, you must use the DeleteMainEntityById<{MainEntityType.Name}> function instead of '{typeof(TDtoEntity).Name}'!"); public override void Delete(IList entities, bool publishEvent = true) => throw new Exception($"To delete, you must use the DeleteMainEntityById<{MainEntityType.Name}> function instead of '{typeof(TDtoEntity).Name}'!"); public override int Delete(Expression> predicate) => throw new Exception($"To delete, you must use the DeleteMainEntityById<{MainEntityType.Name}> function instead of '{typeof(TDtoEntity).Name}'!"); }