• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / MediatR.CommandQuery / 14854706016

06 May 2025 08:07AM CUT coverage: 59.379%. First build
14854706016

Pull #659

github

web-flow
Merge ce1fb0980 into c45a0959f
Pull Request #659: Bump FluentValidation from 11.11.0 to 12.0.0

495 of 971 branches covered (50.98%)

Branch coverage included in aggregate %.

1474 of 2345 relevant lines covered (62.86%)

22.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/MediatR.CommandQuery.Cosmos/Handlers/EntityDeleteCommandHandler.cs
1
using AutoMapper;
2

3
using Cosmos.Abstracts;
4

5
using MediatR.CommandQuery.Commands;
6
using MediatR.CommandQuery.Definitions;
7

8
using Microsoft.Extensions.Logging;
9

10
namespace MediatR.CommandQuery.Cosmos.Handlers;
11

12
public class EntityDeleteCommandHandler<TRepository, TEntity, TReadModel>
13
    : RepositoryHandlerBase<TRepository, TEntity, EntityDeleteCommand<string, TReadModel>, TReadModel>
14
    where TRepository : ICosmosRepository<TEntity>
15
    where TEntity : class, IHaveIdentifier<string>, new()
16
{
17

18
    public EntityDeleteCommandHandler(ILoggerFactory loggerFactory, TRepository repository, IMapper mapper)
19
        : base(loggerFactory, repository, mapper)
×
20
    {
21
    }
×
22

23
    protected override async Task<TReadModel> Process(EntityDeleteCommand<string, TReadModel> request, CancellationToken cancellationToken)
24
    {
25
        ArgumentNullException.ThrowIfNull(request);
×
26
        if (!CosmosKey.TryDecode(request.Id, out var id, out var partitionKey))
×
27
            throw new InvalidOperationException("Invalid Cosmos Key format");
×
28

29
        var entity = await Repository
×
30
            .FindAsync(id, partitionKey, cancellationToken)
×
31
            .ConfigureAwait(false);
×
32

33
        if (entity == null)
×
34
            return default!;
×
35

36
        // apply update metadata
37
        if (entity is ITrackUpdated updateEntity)
×
38
        {
39
            updateEntity.UpdatedBy = request.ActivatedBy;
×
40
            updateEntity.Updated = request.Activated;
×
41
        }
42

43
        TEntity savedEntity;
44

45
        // entity supports soft delete
46
        if (entity is ITrackDeleted deleteEntity)
×
47
        {
48
            deleteEntity.IsDeleted = true;
×
49

50
            savedEntity = await Repository
×
51
                .UpdateAsync(entity, cancellationToken)
×
52
                .ConfigureAwait(false);
×
53
        }
54
        else
55
        {
56
            // when history is tracked, need to update the entity with update metadata before deleting
57
            if (entity is ITrackHistory and ITrackUpdated)
×
58
            {
59
                savedEntity = await Repository
×
60
                   .UpdateAsync(entity, cancellationToken)
×
61
                   .ConfigureAwait(false);
×
62
            }
63
            else
64
            {
65
                savedEntity = entity;
×
66
            }
67

68
            await Repository
×
69
                .DeleteAsync(entity, cancellationToken)
×
70
                .ConfigureAwait(false);
×
71
        }
72

73
        // convert deleted entity to read model
74
        var model = Mapper.Map<TReadModel>(savedEntity);
×
75
        return model;
×
76
    }
×
77

78
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc