feat: Add ConfigurationCQ - Phase 2 Application Layer
Implemented complete CQRS pattern for System Configuration management:
Commands:
- SetConfigurationValueCommand: Create or update configurations with history tracking
- DeactivateConfigurationCommand: Deactivate configurations with audit trail
Queries:
- GetConfigurationByKeyQuery: Retrieve configuration by Scope and Key
- GetAllConfigurationsQuery: List all configurations with filters and pagination
- GetConfigurationHistoryQuery: View complete audit history for any configuration
Features:
- All commands include FluentValidation validators
- History recording to SystemConfigurationHistory table
- Pagination support for list queries
- DTOs for clean data transfer
- Null-safe implementations
Updated:
- IApplicationDbContext: Added 11 new DbSets for network-club entities
- GlobalUsings: Added new entity namespaces
Build Status: ✅ Success (0 errors, 184 warnings in legacy code)
This commit is contained in:
+40
@@ -0,0 +1,40 @@
|
||||
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetAllConfigurations;
|
||||
|
||||
/// <summary>
|
||||
/// Query برای دریافت لیست تمام Configuration ها با فیلتر
|
||||
/// </summary>
|
||||
public record GetAllConfigurationsQuery : IRequest<GetAllConfigurationsResponseDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// موقعیت صفحهبندی
|
||||
/// </summary>
|
||||
public PaginationState? PaginationState { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// مرتبسازی بر اساس
|
||||
/// </summary>
|
||||
public string? SortBy { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// فیلتر
|
||||
/// </summary>
|
||||
public GetAllConfigurationsFilter? Filter { get; init; }
|
||||
}
|
||||
|
||||
public class GetAllConfigurationsFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// فیلتر بر اساس محدوده
|
||||
/// </summary>
|
||||
public ConfigurationScope? Scope { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// جستجو در کلید
|
||||
/// </summary>
|
||||
public string? KeyContains { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// فقط Configuration های فعال
|
||||
/// </summary>
|
||||
public bool? IsActive { get; set; }
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetAllConfigurations;
|
||||
|
||||
public class GetAllConfigurationsQueryHandler : IRequestHandler<GetAllConfigurationsQuery, GetAllConfigurationsResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetAllConfigurationsQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetAllConfigurationsResponseDto> Handle(GetAllConfigurationsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.SystemConfigurations
|
||||
.ApplyOrder(sortBy: request.SortBy)
|
||||
.AsNoTracking()
|
||||
.AsQueryable();
|
||||
|
||||
if (request.Filter is not null)
|
||||
{
|
||||
query = query
|
||||
.Where(x => request.Filter.Scope == null || x.Scope == request.Filter.Scope)
|
||||
.Where(x => request.Filter.KeyContains == null || x.Key.Contains(request.Filter.KeyContains))
|
||||
.Where(x => request.Filter.IsActive == null || x.IsActive == request.Filter.IsActive);
|
||||
}
|
||||
|
||||
var meta = await query.GetMetaData(request.PaginationState, cancellationToken);
|
||||
|
||||
var models = await query
|
||||
.PaginatedListAsync(paginationState: request.PaginationState)
|
||||
.Select(x => new GetAllConfigurationsResponseModel
|
||||
{
|
||||
Id = x.Id,
|
||||
Scope = x.Scope,
|
||||
Key = x.Key,
|
||||
Value = x.Value,
|
||||
Description = x.Description,
|
||||
IsActive = x.IsActive,
|
||||
Created = x.Created,
|
||||
LastModified = x.LastModified
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return new GetAllConfigurationsResponseDto
|
||||
{
|
||||
MetaData = meta,
|
||||
Models = models
|
||||
};
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetAllConfigurations;
|
||||
|
||||
public class GetAllConfigurationsQueryValidator : AbstractValidator<GetAllConfigurationsQuery>
|
||||
{
|
||||
public GetAllConfigurationsQueryValidator()
|
||||
{
|
||||
RuleFor(x => x.Filter.Scope)
|
||||
.IsInEnum()
|
||||
.WithMessage("محدوده تنظیمات معتبر نیست")
|
||||
.When(x => x.Filter?.Scope != null);
|
||||
}
|
||||
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(
|
||||
ValidationContext<GetAllConfigurationsQuery>.CreateWithOptions(
|
||||
(GetAllConfigurationsQuery)model,
|
||||
x => x.IncludeProperties(propertyName)));
|
||||
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetAllConfigurations;
|
||||
|
||||
public class GetAllConfigurationsResponseDto
|
||||
{
|
||||
public MetaData MetaData { get; set; }
|
||||
public List<GetAllConfigurationsResponseModel> Models { get; set; }
|
||||
}
|
||||
|
||||
public class GetAllConfigurationsResponseModel
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public ConfigurationScope Scope { get; set; }
|
||||
public string Key { get; set; }
|
||||
public string Value { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public DateTimeOffset Created { get; set; }
|
||||
public DateTimeOffset? LastModified { get; set; }
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationByKey;
|
||||
|
||||
/// <summary>
|
||||
/// DTO برای نمایش اطلاعات Configuration
|
||||
/// </summary>
|
||||
public class ConfigurationDto
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public ConfigurationScope Scope { get; set; }
|
||||
public string Key { get; set; }
|
||||
public string Value { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public DateTimeOffset Created { get; set; }
|
||||
public DateTimeOffset? LastModified { get; set; }
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationByKey;
|
||||
|
||||
/// <summary>
|
||||
/// Query برای دریافت یک Configuration بر اساس Scope و Key
|
||||
/// </summary>
|
||||
public record GetConfigurationByKeyQuery : IRequest<ConfigurationDto?>
|
||||
{
|
||||
/// <summary>
|
||||
/// محدوده تنظیمات
|
||||
/// </summary>
|
||||
public ConfigurationScope Scope { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// کلید تنظیمات
|
||||
/// </summary>
|
||||
public string Key { get; init; }
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationByKey;
|
||||
|
||||
public class GetConfigurationByKeyQueryHandler : IRequestHandler<GetConfigurationByKeyQuery, ConfigurationDto?>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetConfigurationByKeyQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<ConfigurationDto?> Handle(GetConfigurationByKeyQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var config = await _context.SystemConfigurations
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Scope == request.Scope && x.Key == request.Key)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
if (config == null)
|
||||
return null;
|
||||
|
||||
return new ConfigurationDto
|
||||
{
|
||||
Id = config.Id,
|
||||
Scope = config.Scope,
|
||||
Key = config.Key,
|
||||
Value = config.Value,
|
||||
Description = config.Description,
|
||||
IsActive = config.IsActive,
|
||||
Created = config.Created,
|
||||
LastModified = config.LastModified
|
||||
};
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationByKey;
|
||||
|
||||
public class GetConfigurationByKeyQueryValidator : AbstractValidator<GetConfigurationByKeyQuery>
|
||||
{
|
||||
public GetConfigurationByKeyQueryValidator()
|
||||
{
|
||||
RuleFor(x => x.Scope)
|
||||
.IsInEnum()
|
||||
.WithMessage("محدوده تنظیمات معتبر نیست");
|
||||
|
||||
RuleFor(x => x.Key)
|
||||
.NotEmpty()
|
||||
.WithMessage("کلید تنظیمات الزامی است");
|
||||
}
|
||||
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(
|
||||
ValidationContext<GetConfigurationByKeyQuery>.CreateWithOptions(
|
||||
(GetConfigurationByKeyQuery)model,
|
||||
x => x.IncludeProperties(propertyName)));
|
||||
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationHistory;
|
||||
|
||||
/// <summary>
|
||||
/// Query برای دریافت تاریخچه تغییرات یک Configuration
|
||||
/// </summary>
|
||||
public record GetConfigurationHistoryQuery : IRequest<GetConfigurationHistoryResponseDto>
|
||||
{
|
||||
/// <summary>
|
||||
/// شناسه Configuration
|
||||
/// </summary>
|
||||
public long ConfigurationId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// موقعیت صفحهبندی
|
||||
/// </summary>
|
||||
public PaginationState? PaginationState { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// مرتبسازی بر اساس
|
||||
/// </summary>
|
||||
public string? SortBy { get; init; }
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationHistory;
|
||||
|
||||
public class GetConfigurationHistoryQueryHandler : IRequestHandler<GetConfigurationHistoryQuery, GetConfigurationHistoryResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetConfigurationHistoryQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetConfigurationHistoryResponseDto> Handle(GetConfigurationHistoryQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// بررسی وجود Configuration
|
||||
var configExists = await _context.SystemConfigurations
|
||||
.AnyAsync(x => x.Id == request.ConfigurationId, cancellationToken);
|
||||
|
||||
if (!configExists)
|
||||
{
|
||||
throw new NotFoundException(nameof(SystemConfiguration), request.ConfigurationId);
|
||||
}
|
||||
|
||||
var query = _context.SystemConfigurationHistories
|
||||
.Where(x => x.ConfigurationId == request.ConfigurationId)
|
||||
.ApplyOrder(sortBy: request.SortBy ?? "-Created") // پیشفرض: جدیدترین اول
|
||||
.AsNoTracking()
|
||||
.AsQueryable();
|
||||
|
||||
var meta = await query.GetMetaData(request.PaginationState, cancellationToken);
|
||||
|
||||
var models = await query
|
||||
.PaginatedListAsync(paginationState: request.PaginationState)
|
||||
.Select(x => new GetConfigurationHistoryResponseModel
|
||||
{
|
||||
Id = x.Id,
|
||||
ConfigurationId = x.ConfigurationId,
|
||||
Scope = x.Scope,
|
||||
Key = x.Key,
|
||||
OldValue = x.OldValue,
|
||||
NewValue = x.NewValue,
|
||||
ChangeReason = x.Reason,
|
||||
ChangedBy = x.PerformedBy,
|
||||
Created = x.Created
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return new GetConfigurationHistoryResponseDto
|
||||
{
|
||||
MetaData = meta,
|
||||
Models = models
|
||||
};
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationHistory;
|
||||
|
||||
public class GetConfigurationHistoryQueryValidator : AbstractValidator<GetConfigurationHistoryQuery>
|
||||
{
|
||||
public GetConfigurationHistoryQueryValidator()
|
||||
{
|
||||
RuleFor(x => x.ConfigurationId)
|
||||
.GreaterThan(0)
|
||||
.WithMessage("شناسه Configuration معتبر نیست");
|
||||
}
|
||||
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(
|
||||
ValidationContext<GetConfigurationHistoryQuery>.CreateWithOptions(
|
||||
(GetConfigurationHistoryQuery)model,
|
||||
x => x.IncludeProperties(propertyName)));
|
||||
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
namespace CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationHistory;
|
||||
|
||||
public class GetConfigurationHistoryResponseDto
|
||||
{
|
||||
public MetaData MetaData { get; set; }
|
||||
public List<GetConfigurationHistoryResponseModel> Models { get; set; }
|
||||
}
|
||||
|
||||
public class GetConfigurationHistoryResponseModel
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public long ConfigurationId { get; set; }
|
||||
public ConfigurationScope Scope { get; set; }
|
||||
public string Key { get; set; }
|
||||
public string? OldValue { get; set; }
|
||||
public string NewValue { get; set; }
|
||||
public string ChangeReason { get; set; }
|
||||
public string ChangedBy { get; set; }
|
||||
public DateTimeOffset Created { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user