-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathImportContactsCommand.cs
More file actions
109 lines (99 loc) · 5.24 KB
/
ImportContactsCommand.cs
File metadata and controls
109 lines (99 loc) · 5.24 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//------------------------------------------------------------------------------
// <auto-generated>
// CleanArchitecture.Blazor - MIT Licensed.
// Author: neozhu
// Created/Modified: 2025-03-19
// Import command & template for contacts.
// Validates Excel data, prevents duplicates, and provides a template for bulk entry.
// Docs: https://docs.cleanarchitectureblazor.com/features/contact
// </auto-generated>
//------------------------------------------------------------------------------
//
// Usage:
// - Use `ImportContactsCommand` to import contacts from Excel.
// - Use `CreateContactsTemplateCommand` to generate an Excel template for contact data.
using CleanArchitecture.Blazor.Application.Features.Contacts.DTOs;
using CleanArchitecture.Blazor.Application.Features.Contacts.Caching;
namespace CleanArchitecture.Blazor.Application.Features.Contacts.Commands.Import;
public class ImportContactsCommand: ICacheInvalidatorRequest<Result<int>>
{
public string FileName { get; set; }
public byte[] Data { get; set; }
public string CacheKey => ContactCacheKey.GetAllCacheKey;
public IEnumerable<string>? Tags => ContactCacheKey.Tags;
public ImportContactsCommand(string fileName,byte[] data)
{
FileName = fileName;
Data = data;
}
}
public record class CreateContactsTemplateCommand : IRequest<Result<byte[]>>
{
}
public class ImportContactsCommandHandler :
IRequestHandler<CreateContactsTemplateCommand, Result<byte[]>>,
IRequestHandler<ImportContactsCommand, Result<int>>
{
private readonly IApplicationDbContext _context;
private readonly IStringLocalizer<ImportContactsCommandHandler> _localizer;
private readonly IExcelService _excelService;
private readonly ContactDto _dto = new();
private readonly IMapper _mapper;
public ImportContactsCommandHandler(
IApplicationDbContext context,
IMapper mapper,
IExcelService excelService,
IStringLocalizer<ImportContactsCommandHandler> localizer)
{
_context = context;
_localizer = localizer;
_excelService = excelService;
_mapper = mapper;
}
#nullable disable warnings
public async Task<Result<int>> Handle(ImportContactsCommand request, CancellationToken cancellationToken)
{
var result = await _excelService.ImportAsync(request.Data, mappers: new Dictionary<string, Func<DataRow, ContactDto, object?>>
{
{ _localizer[_dto.GetMemberDescription(x=>x.Name)], (row, item) => item.Name = row[_localizer[_dto.GetMemberDescription(x=>x.Name)]].ToString() },
{ _localizer[_dto.GetMemberDescription(x=>x.Description)], (row, item) => item.Description = row[_localizer[_dto.GetMemberDescription(x=>x.Description)]].ToString() },
{ _localizer[_dto.GetMemberDescription(x=>x.Email)], (row, item) => item.Email = row[_localizer[_dto.GetMemberDescription(x=>x.Email)]].ToString() },
{ _localizer[_dto.GetMemberDescription(x=>x.PhoneNumber)], (row, item) => item.PhoneNumber = row[_localizer[_dto.GetMemberDescription(x=>x.PhoneNumber)]].ToString() },
{ _localizer[_dto.GetMemberDescription(x=>x.Country)], (row, item) => item.Country = row[_localizer[_dto.GetMemberDescription(x=>x.Country)]].ToString() },
}, _localizer[_dto.GetClassDescription()]);
if (result.Succeeded && result.Data is not null)
{
foreach (var dto in result.Data)
{
var exists = await _context.Contacts.AnyAsync(x => x.Name == dto.Name, cancellationToken);
if (!exists)
{
var item = _mapper.Map<Contact>(dto);
// add create domain events if this entity implement the IHasDomainEvent interface
// item.AddDomainEvent(new ContactCreatedEvent(item));
await _context.Contacts.AddAsync(item, cancellationToken);
}
}
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(result.Data.Count());
}
else
{
return await Result<int>.FailureAsync(result.Errors);
}
}
public async Task<Result<byte[]>> Handle(CreateContactsTemplateCommand request, CancellationToken cancellationToken)
{
// TODO: Implement ImportContactsCommandHandler method
var fields = new string[] {
// TODO: Define the fields that should be generate in the template, for example:
_localizer[_dto.GetMemberDescription(x=>x.Name)],
_localizer[_dto.GetMemberDescription(x=>x.Description)],
_localizer[_dto.GetMemberDescription(x=>x.Email)],
_localizer[_dto.GetMemberDescription(x=>x.PhoneNumber)],
_localizer[_dto.GetMemberDescription(x=>x.Country)],
};
var result = await _excelService.CreateTemplateAsync(fields, _localizer[_dto.GetClassDescription()]);
return await Result<byte[]>.SuccessAsync(result);
}
}