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

loresoft / MediatR.CommandQuery / 9307610999

30 May 2024 06:43PM UTC coverage: 57.741%. Remained the same
9307610999

Pull #513

github

web-flow
Merge e90664f48 into 7184530a4
Pull Request #513: Bump Microsoft.NET.Test.Sdk from 17.9.0 to 17.10.0

348 of 708 branches covered (49.15%)

Branch coverage included in aggregate %.

1032 of 1682 relevant lines covered (61.36%)

19.52 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
        if (request is null)
×
26
            throw new ArgumentNullException(nameof(request));
×
27
        if (!CosmosKey.TryDecode(request.Id, out var id, out var partitionKey))
×
28
            throw new InvalidOperationException("Invalid Cosmos Key format");
×
29

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

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

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

44
        TEntity savedEntity;
45

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

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

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

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

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