-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathExportAuditTrailsQuery.cs
More file actions
58 lines (52 loc) · 2.31 KB
/
ExportAuditTrailsQuery.cs
File metadata and controls
58 lines (52 loc) · 2.31 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using AutoMapper.QueryableExtensions;
using CleanArchitecture.Blazor.Application.Features.AuditTrails.DTOs;
namespace CleanArchitecture.Blazor.Application.Features.AuditTrails.Queries.Export;
public class ExportAuditTrailsQuery : IRequest<byte[]>
{
public string Keyword { get; set; } = string.Empty;
public string OrderBy { get; set; } = "Id";
public string SortDirection { get; set; } = "Descending";
}
public class ExportAuditTrailsQueryHandler :
IRequestHandler<ExportAuditTrailsQuery, byte[]>
{
private readonly IApplicationDbContext _context;
private readonly IExcelService _excelService;
private readonly IMapper _mapper;
private readonly IStringLocalizer<ExportAuditTrailsQueryHandler> _localizer;
public ExportAuditTrailsQueryHandler(
IApplicationDbContext context,
IExcelService excelService,
IMapper mapper,
IStringLocalizer<ExportAuditTrailsQueryHandler> localizer
)
{
_context = context;
_excelService = excelService;
_mapper = mapper;
_localizer = localizer;
}
public async Task<byte[]> Handle(ExportAuditTrailsQuery request, CancellationToken cancellationToken)
{
var data = await _context.AuditTrails
.Where(x => x.TableName!.Contains(request.Keyword))
.OrderBy($"{request.OrderBy} {request.SortDirection}")
.ProjectTo<AuditTrailDto>(_mapper.ConfigurationProvider)
.ToListAsync(cancellationToken);
var result = await _excelService.ExportAsync(data,
new Dictionary<string, Func<AuditTrailDto, object?>>
{
//{ _localizer["Id"], item => item.Id },
{ _localizer["Date Time"], item => item.DateTime.ToString("yyyy-MM-dd HH:mm:ss") },
{ _localizer["Table Name"], item => item.TableName },
{ _localizer["Audit Type"], item => item.AuditType },
{ _localizer["Old Values"], item => item.OldValues },
{ _localizer["New Values"], item => item.NewValues },
{ _localizer["Primary Key"], item => item.PrimaryKey }
}, _localizer["AuditTrails"]
);
return result;
}
}