-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathExportContactsQuery.cs
More file actions
76 lines (71 loc) · 3.59 KB
/
ExportContactsQuery.cs
File metadata and controls
76 lines (71 loc) · 3.59 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//------------------------------------------------------------------------------
// <auto-generated>
// This file is part of the CleanArchitecture.Blazor project.
// Licensed to the .NET Foundation under the MIT license.
// See the LICENSE file in the project root for more information.
//
// Author: neozhu
// Created Date: 2025-03-19
// Last Modified: 2025-03-19
// Description:
// Defines a query to export contact data to an Excel file. This query
// applies advanced filtering options and generates an Excel file with
// the specified contact details.
// </auto-generated>
//------------------------------------------------------------------------------
using CleanArchitecture.Blazor.Application.Features.Contacts.DTOs;
using CleanArchitecture.Blazor.Application.Features.Contacts.Caching;
using CleanArchitecture.Blazor.Application.Features.Contacts.Specifications;
namespace CleanArchitecture.Blazor.Application.Features.Contacts.Queries.Export;
public class ExportContactsQuery : ContactAdvancedFilter, ICacheableRequest<Result<byte[]>>
{
public ContactAdvancedSpecification Specification => new ContactAdvancedSpecification(this);
public IEnumerable<string>? Tags => ContactCacheKey.Tags;
public override string ToString()
{
return $"Listview:{ListView}:{CurrentUser?.UserId}, Search:{Keyword}, {OrderBy}, {SortDirection}";
}
public string CacheKey => ContactCacheKey.GetExportCacheKey($"{this}");
}
public class ExportContactsQueryHandler :
IRequestHandler<ExportContactsQuery, Result<byte[]>>
{
private readonly IMapper _mapper;
private readonly IApplicationDbContext _context;
private readonly IExcelService _excelService;
private readonly IStringLocalizer<ExportContactsQueryHandler> _localizer;
private readonly ContactDto _dto = new();
public ExportContactsQueryHandler(
IMapper mapper,
IApplicationDbContext context,
IExcelService excelService,
IStringLocalizer<ExportContactsQueryHandler> localizer
)
{
_mapper = mapper;
_context = context;
_excelService = excelService;
_localizer = localizer;
}
#nullable disable warnings
public async Task<Result<byte[]>> Handle(ExportContactsQuery request, CancellationToken cancellationToken)
{
var data = await _context.Contacts.ApplySpecification(request.Specification)
.OrderBy($"{request.OrderBy} {request.SortDirection}")
.ProjectTo<ContactDto>(_mapper.ConfigurationProvider)
.AsNoTracking()
.ToListAsync(cancellationToken);
var result = await _excelService.ExportAsync(data,
new Dictionary<string, Func<ContactDto, object?>>()
{
// TODO: Define the fields that should be exported, for example:
{_localizer[_dto.GetMemberDescription(x=>x.Name)],item => item.Name},
{_localizer[_dto.GetMemberDescription(x=>x.Description)],item => item.Description},
{_localizer[_dto.GetMemberDescription(x=>x.Email)],item => item.Email},
{_localizer[_dto.GetMemberDescription(x=>x.PhoneNumber)],item => item.PhoneNumber},
{_localizer[_dto.GetMemberDescription(x=>x.Country)],item => item.Country},
}
, _localizer[_dto.GetClassDescription()]);
return await Result<byte[]>.SuccessAsync(result);
}
}