-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathDeleteContactCommand.cs
More file actions
57 lines (47 loc) · 1.86 KB
/
DeleteContactCommand.cs
File metadata and controls
57 lines (47 loc) · 1.86 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
//------------------------------------------------------------------------------
// <auto-generated>
// CleanArchitecture.Blazor - MIT Licensed.
// Author: neozhu
// Created/Modified: 2025-03-19
// Command and handler for deleting Contact entities.
// Implements cache invalidation and triggers domain events.
// Docs: https://docs.cleanarchitectureblazor.com/features/contact
// </auto-generated>
//------------------------------------------------------------------------------
// Usage:
// Delete multiple Contacts by specifying their IDs.
// Domain events are raised for each deletion to support cache invalidation.
using CleanArchitecture.Blazor.Application.Features.Contacts.Caching;
namespace CleanArchitecture.Blazor.Application.Features.Contacts.Commands.Delete;
public class DeleteContactCommand: ICacheInvalidatorRequest<Result<int>>
{
public int[] Id { get; }
public string CacheKey => ContactCacheKey.GetAllCacheKey;
public IEnumerable<string>? Tags => ContactCacheKey.Tags;
public DeleteContactCommand(int[] id)
{
Id = id;
}
}
public class DeleteContactCommandHandler :
IRequestHandler<DeleteContactCommand, Result<int>>
{
private readonly IApplicationDbContext _context;
public DeleteContactCommandHandler(
IApplicationDbContext context)
{
_context = context;
}
public async Task<Result<int>> Handle(DeleteContactCommand request, CancellationToken cancellationToken)
{
var items = await _context.Contacts.Where(x=>request.Id.Contains(x.Id)).ToListAsync(cancellationToken);
foreach (var item in items)
{
// raise a delete domain event
item.AddDomainEvent(new ContactDeletedEvent(item));
_context.Contacts.Remove(item);
}
var result = await _context.SaveChangesAsync(cancellationToken);
return await Result<int>.SuccessAsync(result);
}
}