• 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

0.0
/src/MediatR.CommandQuery.Cosmos/Handlers/EntityUpdateCommandHandler.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 EntityUpdateCommandHandler<TRepository, TEntity, TUpdateModel, TReadModel>
13
    : RepositoryHandlerBase<TRepository, TEntity, EntityUpdateCommand<string, TUpdateModel, TReadModel>, TReadModel>
14
    where TRepository : ICosmosRepository<TEntity>
15
    where TEntity : class, IHaveIdentifier<string>, new()
16
{
17
    public EntityUpdateCommandHandler(ILoggerFactory loggerFactory, TRepository repository, IMapper mapper)
18
        : base(loggerFactory, repository, mapper)
×
19
    {
20
    }
×
21

22
    protected override async Task<TReadModel> Process(EntityUpdateCommand<string, TUpdateModel, TReadModel> request, CancellationToken cancellationToken)
23
    {
24
        if (request is null)
×
25
            throw new ArgumentNullException(nameof(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
        // copy updates from model to entity
37
        Mapper.Map(request.Model, entity);
×
38

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

46
        // save updates
47
        var savedEntity = await Repository
×
48
            .UpdateAsync(entity, cancellationToken)
×
49
            .ConfigureAwait(false);
×
50

51
        // return read model
52
        var model = Mapper.Map<TReadModel>(savedEntity);
×
53
        return model;
×
54
    }
×
55
}
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