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

loresoft / MediatR.CommandQuery / 8911801772

01 May 2024 04:09PM CUT coverage: 57.379% (-1.8%) from 59.163%
8911801772

push

github

pwelter34
tweak build

342 of 700 branches covered (48.86%)

Branch coverage included in aggregate %.

1011 of 1658 relevant lines covered (60.98%)

18.48 hits per line

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

65.0
/src/MediatR.CommandQuery.MongoDB/Handlers/EntityDeleteCommandHandler.cs
1
using AutoMapper;
2

3
using MediatR.CommandQuery.Commands;
4
using MediatR.CommandQuery.Definitions;
5

6
using Microsoft.Extensions.Logging;
7

8
using MongoDB.Abstracts;
9

10
namespace MediatR.CommandQuery.MongoDB.Handlers;
11

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

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

23
    protected override async Task<TReadModel> Process(EntityDeleteCommand<TKey, TReadModel> request, CancellationToken cancellationToken)
24
    {
25
        if (request is null)
6!
26
            throw new ArgumentNullException(nameof(request));
×
27

28
        var entity = await Repository
6✔
29
            .FindAsync(request.Id, cancellationToken)
6✔
30
            .ConfigureAwait(false);
6✔
31

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

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

42
        TEntity savedEntity;
43

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

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

67
            await Repository
6✔
68
                .DeleteAsync(entity, cancellationToken)
6✔
69
                .ConfigureAwait(false);
6✔
70
        }
71

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

77
}
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