This commit is contained in:
masoodafar-web
2025-11-18 22:38:50 +03:30
parent dba8aecc97
commit f6dcd43346
76 changed files with 3778 additions and 1386 deletions
@@ -0,0 +1,16 @@
namespace CMSMicroservice.Application.TagCQ.Commands.CreateNewTag;
public record CreateNewTagCommand : IRequest<CreateNewTagResponseDto>
{
//نام لاتین
public string Name { get; init; }
//عنوان
public string Title { get; init; }
//توضیحات
public string? Description { get; init; }
//فعال؟
public bool IsActive { get; init; }
//ترتیب نمایش
public int SortOrder { get; init; }
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.TagCQ.Commands.CreateNewTag;
public class CreateNewTagCommandHandler : IRequestHandler<CreateNewTagCommand, CreateNewTagResponseDto>
{
private readonly IApplicationDbContext _context;
public CreateNewTagCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<CreateNewTagResponseDto> Handle(CreateNewTagCommand request,
CancellationToken cancellationToken)
{
var entity = request.Adapt<Tag>();
await _context.Tags.AddAsync(entity, cancellationToken);
entity.AddDomainEvent(new CreateNewTagEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return entity.Adapt<CreateNewTagResponseDto>();
}
}
@@ -0,0 +1,21 @@
namespace CMSMicroservice.Application.TagCQ.Commands.CreateNewTag;
public class CreateNewTagCommandValidator : AbstractValidator<CreateNewTagCommand>
{
public CreateNewTagCommandValidator()
{
RuleFor(model => model.Name)
.NotEmpty();
RuleFor(model => model.Title)
.NotEmpty();
RuleFor(model => model.SortOrder)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<CreateNewTagCommand>.CreateWithOptions((CreateNewTagCommand)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,8 @@
namespace CMSMicroservice.Application.TagCQ.Commands.CreateNewTag;
public class CreateNewTagResponseDto
{
//شناسه
public long Id { get; set; }
}
@@ -0,0 +1,8 @@
namespace CMSMicroservice.Application.TagCQ.Commands.DeleteTag;
public record DeleteTagCommand : IRequest<Unit>
{
//شناسه
public long Id { get; init; }
}
@@ -0,0 +1,23 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.TagCQ.Commands.DeleteTag;
public class DeleteTagCommandHandler : IRequestHandler<DeleteTagCommand, Unit>
{
private readonly IApplicationDbContext _context;
public DeleteTagCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(DeleteTagCommand request, CancellationToken cancellationToken)
{
var entity = await _context.Tags
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(Tag), request.Id);
entity.IsDeleted = true;
_context.Tags.Update(entity);
entity.AddDomainEvent(new DeleteTagEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}
@@ -0,0 +1,17 @@
namespace CMSMicroservice.Application.TagCQ.Commands.DeleteTag;
public class DeleteTagCommandValidator : AbstractValidator<DeleteTagCommand>
{
public DeleteTagCommandValidator()
{
RuleFor(model => model.Id)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<DeleteTagCommand>.CreateWithOptions((DeleteTagCommand)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,18 @@
namespace CMSMicroservice.Application.TagCQ.Commands.UpdateTag;
public record UpdateTagCommand : IRequest<Unit>
{
//شناسه
public long Id { get; init; }
//نام لاتین
public string Name { get; init; }
//عنوان
public string Title { get; init; }
//توضیحات
public string? Description { get; init; }
//فعال؟
public bool IsActive { get; init; }
//ترتیب نمایش
public int SortOrder { get; init; }
}
@@ -0,0 +1,23 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.TagCQ.Commands.UpdateTag;
public class UpdateTagCommandHandler : IRequestHandler<UpdateTagCommand, Unit>
{
private readonly IApplicationDbContext _context;
public UpdateTagCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(UpdateTagCommand request, CancellationToken cancellationToken)
{
var entity = await _context.Tags
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(Tag), request.Id);
request.Adapt(entity);
_context.Tags.Update(entity);
entity.AddDomainEvent(new UpdateTagEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}
@@ -0,0 +1,23 @@
namespace CMSMicroservice.Application.TagCQ.Commands.UpdateTag;
public class UpdateTagCommandValidator : AbstractValidator<UpdateTagCommand>
{
public UpdateTagCommandValidator()
{
RuleFor(model => model.Id)
.NotNull();
RuleFor(model => model.Name)
.NotEmpty();
RuleFor(model => model.Title)
.NotEmpty();
RuleFor(model => model.SortOrder)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<UpdateTagCommand>.CreateWithOptions((UpdateTagCommand)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
using Microsoft.Extensions.Logging;
namespace CMSMicroservice.Application.TagCQ.EventHandlers;
public class CreateNewTagEventHandler : INotificationHandler<CreateNewTagEvent>
{
private readonly ILogger<CreateNewTagEventHandler> _logger;
public CreateNewTagEventHandler(ILogger<CreateNewTagEventHandler> logger)
{
_logger = logger;
}
public Task Handle(CreateNewTagEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
using Microsoft.Extensions.Logging;
namespace CMSMicroservice.Application.TagCQ.EventHandlers;
public class DeleteTagEventHandler : INotificationHandler<DeleteTagEvent>
{
private readonly ILogger<DeleteTagEventHandler> _logger;
public DeleteTagEventHandler(ILogger<DeleteTagEventHandler> logger)
{
_logger = logger;
}
public Task Handle(DeleteTagEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}
@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
using Microsoft.Extensions.Logging;
namespace CMSMicroservice.Application.TagCQ.EventHandlers;
public class UpdateTagEventHandler : INotificationHandler<UpdateTagEvent>
{
private readonly ILogger<UpdateTagEventHandler> _logger;
public UpdateTagEventHandler(ILogger<UpdateTagEventHandler> logger)
{
_logger = logger;
}
public Task Handle(UpdateTagEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}
@@ -0,0 +1,27 @@
namespace CMSMicroservice.Application.TagCQ.Queries.GetAllTagByFilter;
public record GetAllTagByFilterQuery : IRequest<GetAllTagByFilterResponseDto>
{
//موقعیت صفحه بندی
public PaginationState? PaginationState { get; init; }
//مرتب سازی بر اساس
public string? SortBy { get; init; }
//فیلتر
public GetAllTagByFilterFilter? Filter { get; init; }
}
public class GetAllTagByFilterFilter
{
//شناسه
public long? Id { get; set; }
//نام لاتین
public string? Name { get; set; }
//عنوان
public string? Title { get; set; }
//توضیحات
public string? Description { get; set; }
//فعال؟
public bool? IsActive { get; set; }
//ترتیب نمایش
public int? SortOrder { get; set; }
}
@@ -0,0 +1,36 @@
namespace CMSMicroservice.Application.TagCQ.Queries.GetAllTagByFilter;
public class GetAllTagByFilterQueryHandler : IRequestHandler<GetAllTagByFilterQuery, GetAllTagByFilterResponseDto>
{
private readonly IApplicationDbContext _context;
public GetAllTagByFilterQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetAllTagByFilterResponseDto> Handle(GetAllTagByFilterQuery request, CancellationToken cancellationToken)
{
var query = _context.Tags
.ApplyOrder(sortBy: request.SortBy)
.AsNoTracking()
.AsQueryable();
if (request.Filter is not null)
{
query = query
.Where(x => request.Filter.Id == null || x.Id == request.Filter.Id)
.Where(x => request.Filter.Name == null || x.Name.Contains(request.Filter.Name))
.Where(x => request.Filter.Title == null || x.Title.Contains(request.Filter.Title))
.Where(x => request.Filter.Description == null || (x.Description != null && x.Description.Contains(request.Filter.Description)))
.Where(x => request.Filter.IsActive == null || x.IsActive == request.Filter.IsActive)
.Where(x => request.Filter.SortOrder == null || x.SortOrder == request.Filter.SortOrder)
;
}
return new GetAllTagByFilterResponseDto
{
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
.ProjectToType<GetAllTagByFilterResponseModel>().ToListAsync(cancellationToken)
};
}
}
@@ -0,0 +1,15 @@
namespace CMSMicroservice.Application.TagCQ.Queries.GetAllTagByFilter;
public class GetAllTagByFilterQueryValidator : AbstractValidator<GetAllTagByFilterQuery>
{
public GetAllTagByFilterQueryValidator()
{
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<GetAllTagByFilterQuery>.CreateWithOptions((GetAllTagByFilterQuery)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,25 @@
namespace CMSMicroservice.Application.TagCQ.Queries.GetAllTagByFilter;
public class GetAllTagByFilterResponseDto
{
//متادیتا
public MetaData MetaData { get; set; }
//مدل خروجی
public List<GetAllTagByFilterResponseModel>? Models { get; set; }
}
public class GetAllTagByFilterResponseModel
{
//شناسه
public long Id { get; set; }
//نام لاتین
public string Name { get; set; }
//عنوان
public string Title { get; set; }
//توضیحات
public string? Description { get; set; }
//فعال؟
public bool IsActive { get; set; }
//ترتیب نمایش
public int SortOrder { get; set; }
}
@@ -0,0 +1,8 @@
namespace CMSMicroservice.Application.TagCQ.Queries.GetTag;
public record GetTagQuery : IRequest<GetTagResponseDto>
{
//شناسه
public long Id { get; init; }
}
@@ -0,0 +1,23 @@
namespace CMSMicroservice.Application.TagCQ.Queries.GetTag;
public class GetTagQueryHandler : IRequestHandler<GetTagQuery, GetTagResponseDto>
{
private readonly IApplicationDbContext _context;
public GetTagQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetTagResponseDto> Handle(GetTagQuery request,
CancellationToken cancellationToken)
{
var response = await _context.Tags
.AsNoTracking()
.Where(x => x.Id == request.Id)
.ProjectToType<GetTagResponseDto>()
.FirstOrDefaultAsync(cancellationToken);
return response ?? throw new NotFoundException(nameof(Tag), request.Id);
}
}
@@ -0,0 +1,17 @@
namespace CMSMicroservice.Application.TagCQ.Queries.GetTag;
public class GetTagQueryValidator : AbstractValidator<GetTagQuery>
{
public GetTagQueryValidator()
{
RuleFor(model => model.Id)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<GetTagQuery>.CreateWithOptions((GetTagQuery)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
@@ -0,0 +1,18 @@
namespace CMSMicroservice.Application.TagCQ.Queries.GetTag;
public class GetTagResponseDto
{
//شناسه
public long Id { get; set; }
//نام لاتین
public string Name { get; set; }
//عنوان
public string Title { get; set; }
//توضیحات
public string? Description { get; set; }
//فعال؟
public bool IsActive { get; set; }
//ترتیب نمایش
public int SortOrder { get; set; }
}