Skip to content

Commit

Permalink
feat: added get subcategories command
Browse files Browse the repository at this point in the history
  • Loading branch information
abubakrmirgiyasov committed Feb 25, 2024
1 parent 0cc4b83 commit c72c410
Showing 1 changed file with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Mint.Domain.Helpers;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Mint.Domain.Helpers;
using Mint.Infrastructure.MessageBrokers.Interfaces;
using Mint.WebApp.Admin.Operations.Dtos.SubCategories;
using Mint.WebApp.Admin.Repositories.Interfaces;

namespace Mint.WebApp.Admin.Operations.Commands.SubCategories;

Expand All @@ -10,11 +13,37 @@ public sealed record GetSubCategoriesCommand(
int PageSize = 50
) : ICommand<PaginatedResult<SubCategoryDTO>>;

internal sealed class GetSubCategoriesCommandHandler
: ICommandHandler<GetSubCategoriesCommand, PaginatedResult<SubCategoryDTO>>
internal sealed class GetSubCategoriesCommandHandler(
ISubCategoryRepository subCategoryRepository,
IMapper mapper
) : ICommandHandler<GetSubCategoriesCommand, PaginatedResult<SubCategoryDTO>>
{
public Task<PaginatedResult<SubCategoryDTO>> Handle(GetSubCategoriesCommand request, CancellationToken cancellationToken)
private readonly ISubCategoryRepository _subCategoryRepository = subCategoryRepository;
private readonly IMapper _mapper = mapper;

public async Task<PaginatedResult<SubCategoryDTO>> Handle(GetSubCategoriesCommand request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
var query = _subCategoryRepository.Context.SubCategories.AsQueryable();

if (!string.IsNullOrEmpty(request.Search))
query = query.Where(x => x.Name.Contains(request.Search));

var subCategories = query
.AsNoTracking()
.Include(x => x.Category)
.Take(request.PageSize)
.ToListAsync(cancellationToken);

var totalCount = await _subCategoryRepository
.Context
.Categories
.AsNoTracking()
.CountAsync(cancellationToken);

return new PaginatedResult<SubCategoryDTO>
{
Items = _mapper.Map<List<SubCategoryDTO>>(subCategories),
TotalCount = totalCount
};
}
}

0 comments on commit c72c410

Please sign in to comment.