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)
54 lines
1.9 KiB
C#
54 lines
1.9 KiB
C#
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
|
|
};
|
|
}
|
|
}
|