update
This commit is contained in:
+31
@@ -0,0 +1,31 @@
|
||||
namespace CMSMicroservice.Application.CategoryCQ.Queries.GetAllCategoryByFilter;
|
||||
public record GetAllCategoryByFilterQuery : IRequest<GetAllCategoryByFilterResponseDto>
|
||||
{
|
||||
//موقعیت صفحه بندی
|
||||
public PaginationState? PaginationState { get; init; }
|
||||
//مرتب سازی بر اساس
|
||||
public string? SortBy { get; init; }
|
||||
//فیلتر
|
||||
public GetAllCategoryByFilterFilter? Filter { get; init; }
|
||||
|
||||
}
|
||||
public class GetAllCategoryByFilterFilter
|
||||
{
|
||||
//شناسه
|
||||
public long? Id { get; set; }
|
||||
//نام لاتین
|
||||
public string? Name { get; set; }
|
||||
//عنوان
|
||||
public string? Title { get; set; }
|
||||
//توضیحات
|
||||
public string? Description { get; set; }
|
||||
//آدرس تصویر
|
||||
public string? ImagePath { get; set; }
|
||||
//شناسه والد
|
||||
public long? ParentId { get; set; }
|
||||
//فعال؟
|
||||
public bool? IsActive { get; set; }
|
||||
//ترتیب نمایش
|
||||
public int? SortOrder { get; set; }
|
||||
}
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
namespace CMSMicroservice.Application.CategoryCQ.Queries.GetAllCategoryByFilter;
|
||||
public class GetAllCategoryByFilterQueryHandler : IRequestHandler<GetAllCategoryByFilterQuery, GetAllCategoryByFilterResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetAllCategoryByFilterQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetAllCategoryByFilterResponseDto> Handle(GetAllCategoryByFilterQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.Categories
|
||||
.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.ImagePath == null || (x.ImagePath != null && x.ImagePath.Contains(request.Filter.ImagePath)))
|
||||
.Where(x => request.Filter.ParentId == null || x.ParentId == request.Filter.ParentId)
|
||||
.Where(x => request.Filter.IsActive == null || x.IsActive == request.Filter.IsActive)
|
||||
.Where(x => request.Filter.SortOrder == null || x.SortOrder == request.Filter.SortOrder)
|
||||
;
|
||||
}
|
||||
return new GetAllCategoryByFilterResponseDto
|
||||
{
|
||||
MetaData = await query.GetMetaData(request.PaginationState, cancellationToken),
|
||||
Models = await query.PaginatedListAsync(paginationState: request.PaginationState)
|
||||
.ProjectToType<GetAllCategoryByFilterResponseModel>().ToListAsync(cancellationToken)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
namespace CMSMicroservice.Application.CategoryCQ.Queries.GetAllCategoryByFilter;
|
||||
public class GetAllCategoryByFilterQueryValidator : AbstractValidator<GetAllCategoryByFilterQuery>
|
||||
{
|
||||
public GetAllCategoryByFilterQueryValidator()
|
||||
{
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetAllCategoryByFilterQuery>.CreateWithOptions((GetAllCategoryByFilterQuery)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
namespace CMSMicroservice.Application.CategoryCQ.Queries.GetAllCategoryByFilter;
|
||||
public class GetAllCategoryByFilterResponseDto
|
||||
{
|
||||
//متادیتا
|
||||
public MetaData MetaData { get; set; }
|
||||
//مدل خروجی
|
||||
public List<GetAllCategoryByFilterResponseModel>? Models { get; set; }
|
||||
|
||||
}
|
||||
public class GetAllCategoryByFilterResponseModel
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
//نام لاتین
|
||||
public string Name { get; set; }
|
||||
//عنوان
|
||||
public string Title { get; set; }
|
||||
//توضیحات
|
||||
public string? Description { get; set; }
|
||||
//آدرس تصویر
|
||||
public string? ImagePath { get; set; }
|
||||
//شناسه والد
|
||||
public long? ParentId { get; set; }
|
||||
//فعال؟
|
||||
public bool IsActive { get; set; }
|
||||
//ترتیب نمایش
|
||||
public int SortOrder { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace CMSMicroservice.Application.CategoryCQ.Queries.GetCategory;
|
||||
public record GetCategoryQuery : IRequest<GetCategoryResponseDto>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
namespace CMSMicroservice.Application.CategoryCQ.Queries.GetCategory;
|
||||
public class GetCategoryQueryHandler : IRequestHandler<GetCategoryQuery, GetCategoryResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetCategoryQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetCategoryResponseDto> Handle(GetCategoryQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await _context.Categories
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.ProjectToType<GetCategoryResponseDto>()
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
return response ?? throw new NotFoundException(nameof(Category), request.Id);
|
||||
}
|
||||
}
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
namespace CMSMicroservice.Application.CategoryCQ.Queries.GetCategory;
|
||||
public class GetCategoryQueryValidator : AbstractValidator<GetCategoryQuery>
|
||||
{
|
||||
public GetCategoryQueryValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<GetCategoryQuery>.CreateWithOptions((GetCategoryQuery)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.CategoryCQ.Queries.GetCategory;
|
||||
public class GetCategoryResponseDto
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; set; }
|
||||
//نام لاتین
|
||||
public string Name { get; set; }
|
||||
//عنوان
|
||||
public string Title { get; set; }
|
||||
//توضیحات
|
||||
public string? Description { get; set; }
|
||||
//آدرس تصویر
|
||||
public string? ImagePath { get; set; }
|
||||
//شناسه والد
|
||||
public long? ParentId { get; set; }
|
||||
//فعال؟
|
||||
public bool IsActive { get; set; }
|
||||
//ترتیب نمایش
|
||||
public int SortOrder { get; set; }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user