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

View File

@@ -5,7 +5,8 @@ public interface IApplicationDbContext
DbSet<UserAddress> UserAddresss { get; }
DbSet<Package> Packages { get; }
DbSet<Role> Roles { get; }
DbSet<Category> Categorys { get; }
DbSet<Category> Categories { get; }
DbSet<Tag> Tags { get; }
DbSet<UserRole> UserRoles { get; }
DbSet<UserWallet> UserWallets { get; }
DbSet<UserWalletChangeLog> UserWalletChangeLogs { get; }
@@ -20,6 +21,7 @@ public interface IApplicationDbContext
DbSet<OtpToken> OtpTokens { get; }
DbSet<Contract> Contracts { get; }
DbSet<UserContract> UserContracts { get; }
DbSet<PruductTag> PruductTags { get; }
DbSet<PruductCategory> PruductCategorys { get; }
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}
}

View File

@@ -0,0 +1,11 @@
namespace CMSMicroservice.Application.Common.Mappings;
public class PruductTagProfile : IRegister
{
void IRegister.Register(TypeAdapterConfig config)
{
//config.NewConfig<Source,Destination>()
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
}
}

View File

@@ -0,0 +1,11 @@
namespace CMSMicroservice.Application.Common.Mappings;
public class TagProfile : IRegister
{
void IRegister.Register(TypeAdapterConfig config)
{
//config.NewConfig<Source,Destination>()
// .Map(dest => dest.FullName, src => $"{src.Firstname} {src.Lastname}");
}
}

View File

@@ -0,0 +1,10 @@
namespace CMSMicroservice.Application.PruductTagCQ.Commands.CreateNewPruductTag;
public record CreateNewPruductTagCommand : IRequest<CreateNewPruductTagResponseDto>
{
//شناسه محصول
public string ProductId { get; init; }
//شناسه تگ
public string TagId { get; init; }
}

View File

@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.PruductTagCQ.Commands.CreateNewPruductTag;
public class CreateNewPruductTagCommandHandler : IRequestHandler<CreateNewPruductTagCommand, CreateNewPruductTagResponseDto>
{
private readonly IApplicationDbContext _context;
public CreateNewPruductTagCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<CreateNewPruductTagResponseDto> Handle(CreateNewPruductTagCommand request,
CancellationToken cancellationToken)
{
var entity = request.Adapt<PruductTag>();
await _context.PruductTags.AddAsync(entity, cancellationToken);
entity.AddDomainEvent(new CreateNewPruductTagEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return entity.Adapt<CreateNewPruductTagResponseDto>();
}
}

View File

@@ -0,0 +1,19 @@
namespace CMSMicroservice.Application.PruductTagCQ.Commands.CreateNewPruductTag;
public class CreateNewPruductTagCommandValidator : AbstractValidator<CreateNewPruductTagCommand>
{
public CreateNewPruductTagCommandValidator()
{
RuleFor(model => model.ProductId)
.NotEmpty();
RuleFor(model => model.TagId)
.NotEmpty();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<CreateNewPruductTagCommand>.CreateWithOptions((CreateNewPruductTagCommand)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}

View File

@@ -0,0 +1,8 @@
namespace CMSMicroservice.Application.PruductTagCQ.Commands.CreateNewPruductTag;
public class CreateNewPruductTagResponseDto
{
//شناسه
public long Id { get; set; }
}

View File

@@ -0,0 +1,8 @@
namespace CMSMicroservice.Application.PruductTagCQ.Commands.DeletePruductTag;
public record DeletePruductTagCommand : IRequest<Unit>
{
//شناسه
public long Id { get; init; }
}

View File

@@ -0,0 +1,23 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.PruductTagCQ.Commands.DeletePruductTag;
public class DeletePruductTagCommandHandler : IRequestHandler<DeletePruductTagCommand, Unit>
{
private readonly IApplicationDbContext _context;
public DeletePruductTagCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(DeletePruductTagCommand request, CancellationToken cancellationToken)
{
var entity = await _context.PruductTags
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(PruductTag), request.Id);
entity.IsDeleted = true;
_context.PruductTags.Update(entity);
entity.AddDomainEvent(new DeletePruductTagEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}

View File

@@ -0,0 +1,17 @@
namespace CMSMicroservice.Application.PruductTagCQ.Commands.DeletePruductTag;
public class DeletePruductTagCommandValidator : AbstractValidator<DeletePruductTagCommand>
{
public DeletePruductTagCommandValidator()
{
RuleFor(model => model.Id)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<DeletePruductTagCommand>.CreateWithOptions((DeletePruductTagCommand)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}

View File

@@ -0,0 +1,12 @@
namespace CMSMicroservice.Application.PruductTagCQ.Commands.UpdatePruductTag;
public record UpdatePruductTagCommand : IRequest<Unit>
{
//شناسه
public long Id { get; init; }
//شناسه محصول
public string ProductId { get; init; }
//شناسه تگ
public string TagId { get; init; }
}

View File

@@ -0,0 +1,23 @@
using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.PruductTagCQ.Commands.UpdatePruductTag;
public class UpdatePruductTagCommandHandler : IRequestHandler<UpdatePruductTagCommand, Unit>
{
private readonly IApplicationDbContext _context;
public UpdatePruductTagCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(UpdatePruductTagCommand request, CancellationToken cancellationToken)
{
var entity = await _context.PruductTags
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(PruductTag), request.Id);
request.Adapt(entity);
_context.PruductTags.Update(entity);
entity.AddDomainEvent(new UpdatePruductTagEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}

View File

@@ -0,0 +1,21 @@
namespace CMSMicroservice.Application.PruductTagCQ.Commands.UpdatePruductTag;
public class UpdatePruductTagCommandValidator : AbstractValidator<UpdatePruductTagCommand>
{
public UpdatePruductTagCommandValidator()
{
RuleFor(model => model.Id)
.NotNull();
RuleFor(model => model.ProductId)
.NotEmpty();
RuleFor(model => model.TagId)
.NotEmpty();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<UpdatePruductTagCommand>.CreateWithOptions((UpdatePruductTagCommand)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}

View File

@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
using Microsoft.Extensions.Logging;
namespace CMSMicroservice.Application.PruductTagCQ.EventHandlers;
public class CreateNewPruductTagEventHandler : INotificationHandler<CreateNewPruductTagEvent>
{
private readonly ILogger<CreateNewPruductTagEventHandler> _logger;
public CreateNewPruductTagEventHandler(ILogger<CreateNewPruductTagEventHandler> logger)
{
_logger = logger;
}
public Task Handle(CreateNewPruductTagEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}

View File

@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
using Microsoft.Extensions.Logging;
namespace CMSMicroservice.Application.PruductTagCQ.EventHandlers;
public class DeletePruductTagEventHandler : INotificationHandler<DeletePruductTagEvent>
{
private readonly ILogger<DeletePruductTagEventHandler> _logger;
public DeletePruductTagEventHandler(ILogger<DeletePruductTagEventHandler> logger)
{
_logger = logger;
}
public Task Handle(DeletePruductTagEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}

View File

@@ -0,0 +1,22 @@
using CMSMicroservice.Domain.Events;
using Microsoft.Extensions.Logging;
namespace CMSMicroservice.Application.PruductTagCQ.EventHandlers;
public class UpdatePruductTagEventHandler : INotificationHandler<UpdatePruductTagEvent>
{
private readonly ILogger<UpdatePruductTagEventHandler> _logger;
public UpdatePruductTagEventHandler(ILogger<UpdatePruductTagEventHandler> logger)
{
_logger = logger;
}
public Task Handle(UpdatePruductTagEvent notification, CancellationToken cancellationToken)
{
_logger.LogInformation("Domain Event: {DomainEvent}", notification.GetType().Name);
return Task.CompletedTask;
}
}

View File

@@ -0,0 +1,21 @@
namespace CMSMicroservice.Application.PruductTagCQ.Queries.GetAllPruductTagByFilter;
public record GetAllPruductTagByFilterQuery : IRequest<GetAllPruductTagByFilterResponseDto>
{
//موقعیت صفحه بندی
public PaginationState? PaginationState { get; init; }
//مرتب سازی بر اساس
public string? SortBy { get; init; }
//فیلتر
public GetAllPruductTagByFilterFilter? Filter { get; init; }
}
public class GetAllPruductTagByFilterFilter
{
//شناسه
public long? Id { get; set; }
//شناسه محصول
public string? ProductId { get; set; }
//شناسه تگ
public string? TagId { get; set; }
}

View File

@@ -0,0 +1,33 @@
namespace CMSMicroservice.Application.PruductTagCQ.Queries.GetAllPruductTagByFilter;
public class GetAllPruductTagByFilterQueryHandler : IRequestHandler<GetAllPruductTagByFilterQuery, GetAllPruductTagByFilterResponseDto>
{
private readonly IApplicationDbContext _context;
public GetAllPruductTagByFilterQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetAllPruductTagByFilterResponseDto> Handle(GetAllPruductTagByFilterQuery request, CancellationToken cancellationToken)
{
var query = _context.PruductTags
.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.ProductId == null || x.ProductId.Contains(request.Filter.ProductId))
.Where(x => request.Filter.TagId == null || x.TagId.Contains(request.Filter.TagId))
;
}
return new GetAllPruductTagByFilterResponseDto
{
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
.ProjectToType<GetAllPruductTagByFilterResponseModel>().ToListAsync(cancellationToken)
};
}
}

View File

@@ -0,0 +1,15 @@
namespace CMSMicroservice.Application.PruductTagCQ.Queries.GetAllPruductTagByFilter;
public class GetAllPruductTagByFilterQueryValidator : AbstractValidator<GetAllPruductTagByFilterQuery>
{
public GetAllPruductTagByFilterQueryValidator()
{
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<GetAllPruductTagByFilterQuery>.CreateWithOptions((GetAllPruductTagByFilterQuery)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}

View File

@@ -0,0 +1,19 @@
namespace CMSMicroservice.Application.PruductTagCQ.Queries.GetAllPruductTagByFilter;
public class GetAllPruductTagByFilterResponseDto
{
//متادیتا
public MetaData MetaData { get; set; }
//مدل خروجی
public List<GetAllPruductTagByFilterResponseModel>? Models { get; set; }
}
public class GetAllPruductTagByFilterResponseModel
{
//شناسه
public long Id { get; set; }
//شناسه محصول
public string ProductId { get; set; }
//شناسه تگ
public string TagId { get; set; }
}

View File

@@ -0,0 +1,8 @@
namespace CMSMicroservice.Application.PruductTagCQ.Queries.GetPruductTag;
public record GetPruductTagQuery : IRequest<GetPruductTagResponseDto>
{
//شناسه
public long Id { get; init; }
}

View File

@@ -0,0 +1,23 @@
namespace CMSMicroservice.Application.PruductTagCQ.Queries.GetPruductTag;
public class GetPruductTagQueryHandler : IRequestHandler<GetPruductTagQuery, GetPruductTagResponseDto>
{
private readonly IApplicationDbContext _context;
public GetPruductTagQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetPruductTagResponseDto> Handle(GetPruductTagQuery request,
CancellationToken cancellationToken)
{
var response = await _context.PruductTags
.AsNoTracking()
.Where(x => x.Id == request.Id)
.ProjectToType<GetPruductTagResponseDto>()
.FirstOrDefaultAsync(cancellationToken);
return response ?? throw new NotFoundException(nameof(PruductTag), request.Id);
}
}

View File

@@ -0,0 +1,17 @@
namespace CMSMicroservice.Application.PruductTagCQ.Queries.GetPruductTag;
public class GetPruductTagQueryValidator : AbstractValidator<GetPruductTagQuery>
{
public GetPruductTagQueryValidator()
{
RuleFor(model => model.Id)
.NotNull();
}
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<GetPruductTagQuery>.CreateWithOptions((GetPruductTagQuery)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}

View File

@@ -0,0 +1,11 @@
namespace CMSMicroservice.Application.PruductTagCQ.Queries.GetPruductTag;
public class GetPruductTagResponseDto
{
//شناسه
public long Id { get; set; }
//شناسه محصول
public string ProductId { get; set; }
//شناسه تگ
public string TagId { get; set; }
}

View File

@@ -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; }
}

View File

@@ -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>();
}
}

View File

@@ -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);
};
}

View File

@@ -0,0 +1,8 @@
namespace CMSMicroservice.Application.TagCQ.Commands.CreateNewTag;
public class CreateNewTagResponseDto
{
//شناسه
public long Id { get; set; }
}

View File

@@ -0,0 +1,8 @@
namespace CMSMicroservice.Application.TagCQ.Commands.DeleteTag;
public record DeleteTagCommand : IRequest<Unit>
{
//شناسه
public long Id { get; init; }
}

View File

@@ -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;
}
}

View File

@@ -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);
};
}

View File

@@ -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; }
}

View File

@@ -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;
}
}

View File

@@ -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);
};
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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; }
}

View File

@@ -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)
};
}
}

View File

@@ -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);
};
}

View File

@@ -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; }
}

View File

@@ -0,0 +1,8 @@
namespace CMSMicroservice.Application.TagCQ.Queries.GetTag;
public record GetTagQuery : IRequest<GetTagResponseDto>
{
//شناسه
public long Id { get; init; }
}

View File

@@ -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);
}
}

View File

@@ -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);
};
}

View File

@@ -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; }
}