-
-
Notifications
You must be signed in to change notification settings - Fork 747
Expand file tree
/
Copy pathGetAllBrandsQuery.cs
More file actions
43 lines (39 loc) · 1.65 KB
/
Copy pathGetAllBrandsQuery.cs
File metadata and controls
43 lines (39 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using AutoMapper;
using BlazorHero.CleanArchitecture.Application.Interfaces.Repositories;
using BlazorHero.CleanArchitecture.Domain.Entities.Catalog;
using BlazorHero.CleanArchitecture.Shared.Constants.Application;
using BlazorHero.CleanArchitecture.Shared.Wrapper;
using LazyCache;
using MediatR;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace BlazorHero.CleanArchitecture.Application.Features.Brands.Queries.GetAll
{
public class GetAllBrandsQuery : IRequest<Result<List<GetAllBrandsResponse>>>
{
public GetAllBrandsQuery()
{
}
}
internal class GetAllBrandsCachedQueryHandler : IRequestHandler<GetAllBrandsQuery, Result<List<GetAllBrandsResponse>>>
{
private readonly IUnitOfWork<int> _unitOfWork;
private readonly IMapper _mapper;
private readonly IAppCache _cache;
public GetAllBrandsCachedQueryHandler(IUnitOfWork<int> unitOfWork, IMapper mapper, IAppCache cache)
{
_unitOfWork = unitOfWork;
_mapper = mapper;
_cache = cache;
}
public async Task<Result<List<GetAllBrandsResponse>>> Handle(GetAllBrandsQuery request, CancellationToken cancellationToken)
{
Func<Task<List<Brand>>> getAllBrands = () => _unitOfWork.Repository<Brand>().GetAllListAsync();
var brandList = await _cache.GetOrAddAsync(ApplicationConstants.Cache.GetAllBrandsCacheKey, getAllBrands);
var mappedBrands = _mapper.Map<List<GetAllBrandsResponse>>(brandList);
return await Result<List<GetAllBrandsResponse>>.SuccessAsync(mappedBrands);
}
}
}