Mango.Nop.Libraries/Mango.Nop.Data/Repositories/MgDtoDbTableBase.cs

77 lines
4.3 KiB
C#

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<TDtoEntity, TMainEntity> : MgDbTableBase<TDtoEntity> where TDtoEntity : BaseEntity/*, IMgModelDtoBase<TDtoEntity>*/ 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<TMainEntity?> GetMainEntityById(int id) => await _dataProvider.GetTable<TMainEntity>().FirstOrDefaultAsync(x => x.Id == id);
public async Task<int> DeleteMainEntityById(int id, bool publishEvent = true)
{
var affectedRows = 0;
var mainEntity = await GetMainEntityById(id);
if (mainEntity == null) return affectedRows;
affectedRows = await _dataProvider.GetTable<TMainEntity>().DeleteAsync(x => x.Id == id);
if (publishEvent) await _eventPublisher.PublishAsync(new EntityDeletedEvent<TMainEntity>(mainEntity));
return affectedRows;
}
public Task HandleEventAsync(EntityDeletedEvent<TDtoEntity> eventMessage)
=> throw new Exception($"To delete, you must use the DeleteMainEntityById<{MainEntityType.Name}> function instead of '{typeof(TDtoEntity).Name}'!");
public async Task HandleEventAsync(EntityInsertedEvent<TDtoEntity> 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<TMainEntity>(mainEntity));
}
public async Task HandleEventAsync(EntityUpdatedEvent<TDtoEntity> 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<TMainEntity>(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<TDtoEntity> 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<int> DeleteAsync(Expression<Func<TDtoEntity, bool>> predicate)
=> throw new Exception($"To delete, you must use the DeleteMainEntityById<{MainEntityType.Name}> function instead of '{typeof(TDtoEntity).Name}'!");
public override Task DeleteAsync(Expression<Func<TDtoEntity, bool>> 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<TDtoEntity> 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<Func<TDtoEntity, bool>> predicate)
=> throw new Exception($"To delete, you must use the DeleteMainEntityById<{MainEntityType.Name}> function instead of '{typeof(TDtoEntity).Name}'!");
}