-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathAddEditContactCommand.cs
More file actions
85 lines (76 loc) · 3 KB
/
AddEditContactCommand.cs
File metadata and controls
85 lines (76 loc) · 3 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
//------------------------------------------------------------------------------
// <auto-generated>
// CleanArchitecture.Blazor - MIT Licensed.
// Author: neozhu
// Created/Modified: 2025-03-19
// Command for adding/editing a contact entity with validation, mapping,
// domain events, and cache invalidation.
// Documentation: https://docs.cleanarchitectureblazor.com/features/contact
// </auto-generated>
//------------------------------------------------------------------------------
// Usage: Use this command to add or edit a contact. Caching and domain event handling are automatic.
using CleanArchitecture.Blazor.Application.Features.Contacts.Caching;
using CleanArchitecture.Blazor.Application.Features.Contacts.DTOs;
namespace CleanArchitecture.Blazor.Application.Features.Contacts.Commands.AddEdit;
public class AddEditContactCommand: ICacheInvalidatorRequest<Result<int>>
{
[Description("Id")]
public int Id { get; set; }
[Description("Name")]
public string Name {get;set;}
[Description("Description")]
public string? Description {get;set;}
[Description("Email")]
public string? Email {get;set;}
[Description("Phone number")]
public string? PhoneNumber {get;set;}
[Description("Country")]
public string? Country {get;set;}
public string CacheKey => ContactCacheKey.GetAllCacheKey;
public IEnumerable<string>? Tags => ContactCacheKey.Tags;
private class Mapping : Profile
{
public Mapping()
{
CreateMap<ContactDto, AddEditContactCommand>(MemberList.None);
CreateMap<AddEditContactCommand, Contact>(MemberList.None);
}
}
}
public class AddEditContactCommandHandler : IRequestHandler<AddEditContactCommand, Result<int>>
{
private readonly IMapper _mapper;
private readonly IApplicationDbContext _context;
public AddEditContactCommandHandler(
IMapper mapper,
IApplicationDbContext context)
{
_mapper = mapper;
_context = context;
}
public async Task<Result<int>> Handle(AddEditContactCommand request, CancellationToken cancellationToken)
{
if (request.Id > 0)
{
var item = await _context.Contacts.FindAsync(request.Id, cancellationToken);
if (item == null)
{
return await Result<int>.FailureAsync($"Contact with id: [{request.Id}] not found.");
}
item = _mapper.Map(request, item);
// raise a update domain event
item.AddDomainEvent(new ContactUpdatedEvent(item));
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
}
else
{
var item = _mapper.Map<Contact>(request);
// raise a create domain event
item.AddDomainEvent(new ContactCreatedEvent(item));
_context.Contacts.Add(item);
await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(item.Id);
}
}
}