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

loresoft / MediatR.CommandQuery / 8410320720

24 Mar 2024 03:15PM UTC coverage: 58.562%. Remained the same
8410320720

push

github

pwelter34
Update EntityQueryEndpointBase.cs

230 of 438 branches covered (52.51%)

Branch coverage included in aggregate %.

560 of 911 relevant lines covered (61.47%)

19.53 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 System;
2
using System.Diagnostics.CodeAnalysis;
3
using System.Threading;
4
using System.Threading.Tasks;
5

6
using AutoMapper;
7

8
using Cosmos.Abstracts;
9

10
using MediatR.CommandQuery.Commands;
11
using MediatR.CommandQuery.Definitions;
12

13
using Microsoft.Azure.Cosmos;
14
using Microsoft.Extensions.Logging;
15

16
namespace MediatR.CommandQuery.Cosmos.Handlers;
17

18
public class EntityDeleteCommandHandler<TRepository, TEntity, TReadModel>
19
    : RepositoryHandlerBase<TRepository, TEntity, EntityDeleteCommand<string, TReadModel>, TReadModel>
20
    where TRepository : ICosmosRepository<TEntity>
21
    where TEntity : class, IHaveIdentifier<string>, new()
22
{
23

24
    public EntityDeleteCommandHandler(ILoggerFactory loggerFactory, TRepository repository, IMapper mapper)
25
        : base(loggerFactory, repository, mapper)
×
26
    {
27
    }
×
28

29
    protected override async Task<TReadModel> Process(EntityDeleteCommand<string, TReadModel> request, CancellationToken cancellationToken)
30
    {
31
        if (request is null)
32
            throw new ArgumentNullException(nameof(request));
33
        if (!CosmosKey.TryDecode(request.Id, out var id, out var partitionKey))
34
            throw new InvalidOperationException("Invalid Cosmos Key format");
35

36
        var entity = await Repository
37
            .FindAsync(id, partitionKey, cancellationToken)
38
            .ConfigureAwait(false);
39

40
        if (entity == null)
41
            return default!;
42

43
        // apply update metadata
44
        if (entity is ITrackUpdated updateEntity)
45
        {
46
            updateEntity.UpdatedBy = request.ActivatedBy;
47
            updateEntity.Updated = request.Activated;
48
        }
49

50
        TEntity savedEntity;
51

52
        // entity supports soft delete
53
        if (entity is ITrackDeleted deleteEntity)
54
        {
55
            deleteEntity.IsDeleted = true;
56

57
            savedEntity = await Repository
58
                .UpdateAsync(entity, cancellationToken)
59
                .ConfigureAwait(false);
60
        }
61
        else
62
        {
63
            // when history is tracked, need to update the entity with update metadata before deleting
64
            if (entity is ITrackHistory and ITrackUpdated)
65
            {
66
                savedEntity = await Repository
67
                   .UpdateAsync(entity, cancellationToken)
68
                   .ConfigureAwait(false);
69
            }
70
            else
71
            {
72
                savedEntity = entity;
73
            }
74

75
            await Repository
76
                .DeleteAsync(entity, cancellationToken)
77
                .ConfigureAwait(false);
78
        }
79

80
        // convert deleted entity to read model
81
        var model = Mapper.Map<TReadModel>(savedEntity);
82
        return model;
83
    }
84

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