improvements, fixes, etc...
This commit is contained in:
parent
f7f2d30915
commit
c4742f0d86
|
|
@ -0,0 +1,35 @@
|
|||
using System.Diagnostics.CodeAnalysis;
|
||||
using AyCode.Utils.Extensions;
|
||||
using LinqToDB.Common;
|
||||
using Nop.Core;
|
||||
using Nop.Core.Domain.Common;
|
||||
|
||||
namespace Mango.Nop.Core.Extensions;
|
||||
|
||||
public static class GenericAttributeExtensions
|
||||
{
|
||||
public static TValue? GetValueOrNull<TValue>(this IEnumerable<GenericAttribute> src, string key) where TValue : struct
|
||||
{
|
||||
var ga = src.SingleOrDefault(x => x.Key == key);
|
||||
if (ga == null || ga.Value.IsNullOrWhiteSpace()) return null;
|
||||
|
||||
return CommonHelper.To<TValue>(ga.Value);
|
||||
}
|
||||
|
||||
public static TValue GetValueOrDefault<TValue>(this IEnumerable<GenericAttribute> src, string key, TValue defaultValue = default) where TValue : struct
|
||||
{
|
||||
var gaValue = GetValueOrNull<TValue>(src, key);
|
||||
return gaValue == null ? defaultValue : CommonHelper.To<TValue>(gaValue);
|
||||
}
|
||||
|
||||
public static bool TryGetValue<TValue>(this IEnumerable<GenericAttribute> src, string key, [NotNullWhen(true)] out TValue? value) where TValue : struct
|
||||
{
|
||||
value = null;
|
||||
|
||||
var gaValue = GetValueOrNull<TValue>(src, key);
|
||||
if (gaValue == null) return false;
|
||||
|
||||
value = CommonHelper.To<TValue>(gaValue);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +1,15 @@
|
|||
using AyCode.Core.Helpers;
|
||||
using AyCode.Entities;
|
||||
using AyCode.Entities.Server.LogItems;
|
||||
using AyCode.Utils.Extensions;
|
||||
using LogLevel = AyCode.Core.Loggers.LogLevel;
|
||||
using LogLevelNop = Nop.Core.Domain.Logging.LogLevel;
|
||||
|
||||
namespace Mango.Nop.Core.Loggers
|
||||
{
|
||||
public class NopLogWriter : AcLogItemWriterBase<AcLogItem>//AcTextLogWriterBase
|
||||
{
|
||||
private static readonly SemaphoreSlim LockSlim = new(0);
|
||||
private readonly global::Nop.Services.Logging.ILogger _nopLogger;
|
||||
|
||||
public NopLogWriter(global::Nop.Services.Logging.ILogger nopLogger) : this(nopLogger, null)
|
||||
|
|
@ -23,21 +26,28 @@ namespace Mango.Nop.Core.Loggers
|
|||
//}
|
||||
|
||||
protected override void WriteLogItemCallback(AcLogItem logItem)
|
||||
{
|
||||
using (LockSlim.UseWait())
|
||||
{
|
||||
switch (logItem.LogLevel)
|
||||
{
|
||||
case LogLevel.Detail:
|
||||
case LogLevel.Trace:
|
||||
case LogLevel.Debug:
|
||||
if (_nopLogger.IsEnabled(LogLevelNop.Debug)) _nopLogger.InsertLog(LogLevelNop.Debug, logItem.Text, logItem.Exception, null);
|
||||
break;
|
||||
case LogLevel.Info:
|
||||
_nopLogger.Information(logItem.Text);//.Forget();
|
||||
if (_nopLogger.IsEnabled(LogLevelNop.Information)) _nopLogger.InsertLog(LogLevelNop.Information, logItem.Text, logItem.Exception, null);
|
||||
//_nopLogger.Information(logItem.Text); //.Forget();
|
||||
break;
|
||||
case LogLevel.Suggest:
|
||||
case LogLevel.Warning:
|
||||
_nopLogger.Warning(logItem.Text);//.Forget();
|
||||
if (_nopLogger.IsEnabled(LogLevelNop.Warning)) _nopLogger.InsertLog(LogLevelNop.Warning, logItem.Text, logItem.Exception, null);
|
||||
//_nopLogger.Warning(logItem.Text); //.Forget();
|
||||
break;
|
||||
case LogLevel.Error:
|
||||
_nopLogger.Error(logItem.Text);//.Forget();//, logItem.Exception);
|
||||
if (_nopLogger.IsEnabled(LogLevelNop.Error)) _nopLogger.InsertLog(LogLevelNop.Error, logItem.Text, logItem.Exception, null);
|
||||
//_nopLogger.Error(logItem.Text); //.Forget();//, logItem.Exception);
|
||||
break;
|
||||
case LogLevel.Disabled:
|
||||
break;
|
||||
|
|
@ -47,3 +57,4 @@ namespace Mango.Nop.Core.Loggers
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,20 @@
|
|||
using System.Linq.Expressions;
|
||||
using AyCode.Interfaces.Entities;
|
||||
using AyCode.Interfaces.Entities;
|
||||
using AyCode.Interfaces.TimeStampInfo;
|
||||
using DocumentFormat.OpenXml.Vml.Office;
|
||||
using Mango.Nop.Core.Interfaces;
|
||||
using Nop.Core;
|
||||
using Nop.Core.Caching;
|
||||
using Nop.Core.Configuration;
|
||||
using Nop.Core.Domain.Catalog;
|
||||
using Nop.Core.Domain.Common;
|
||||
using Nop.Core.Events;
|
||||
using Nop.Data;
|
||||
using Nop.Services.Logging;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Mango.Nop.Core.Repositories;
|
||||
|
||||
public class MgDbTableBase<TEntity>(IEventPublisher eventPublisher, INopDataProvider dataProvider, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, AppSettings appSettings, ILogger logger)
|
||||
public abstract class MgDbTableBase<TEntity>(IEventPublisher eventPublisher, INopDataProvider dataProvider, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, AppSettings appSettings, ILogger logger)
|
||||
: EntityRepository<TEntity>(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings), IMgDbTableBase where TEntity : BaseEntity
|
||||
{
|
||||
protected ILogger Logger = logger;
|
||||
|
|
@ -149,7 +151,8 @@ public class MgDbTableBase<TEntity>(IEventPublisher eventPublisher, INopDataProv
|
|||
|
||||
protected override IQueryable<TEntity> AddDeletedFilter(IQueryable<TEntity> query, in bool includeDeleted)
|
||||
{
|
||||
foreach (var entity in query) OnDelete(entity);
|
||||
//EZ NEM DELETE METHOD! A GET-EKNÉL HASZNÁLJA A ISFOTDELETE FILTER-HEZ! - J.
|
||||
//foreach (var entity in query) OnDelete(entity);
|
||||
|
||||
return base.AddDeletedFilter(query, in includeDeleted);
|
||||
}
|
||||
|
|
@ -182,7 +185,7 @@ public class MgDbTableBase<TEntity>(IEventPublisher eventPublisher, INopDataProv
|
|||
return base.DeleteAsync(predicate);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool publishEvent)
|
||||
public virtual async Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool publishEvent)
|
||||
{
|
||||
if (publishEvent)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,79 @@
|
|||
using System.Linq.Expressions;
|
||||
using LinqToDB;
|
||||
using Nop.Core;
|
||||
using Nop.Core.Caching;
|
||||
using Nop.Core.Configuration;
|
||||
using Nop.Core.Domain.Orders;
|
||||
using Nop.Core.Events;
|
||||
using Nop.Data;
|
||||
using Nop.Services.Events;
|
||||
using Nop.Services.Logging;
|
||||
|
||||
namespace Mango.Nop.Core.Repositories;
|
||||
|
||||
public abstract class MgDtoDbTableBase<TDtoEntity, TMainEntity> : MgDbTableBase<TDtoEntity>, IConsumer<EntityInsertedEvent<TDtoEntity>>, IConsumer<EntityUpdatedEvent<TDtoEntity>>,
|
||||
IConsumer<EntityDeletedEvent<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, ILogger logger)
|
||||
: base(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings, logger)
|
||||
{
|
||||
}
|
||||
|
||||
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}'!");
|
||||
}
|
||||
Loading…
Reference in New Issue