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

loresoft / MediatR.CommandQuery / 11418431610

19 Oct 2024 02:43PM UTC coverage: 58.337% (+0.9%) from 57.424%
11418431610

push

github

pwelter34
add HybridCache, misc refactors

331 of 675 branches covered (49.04%)

Branch coverage included in aggregate %.

28 of 176 new or added lines in 24 files covered. (15.91%)

156 existing lines in 33 files now uncovered.

1163 of 1886 relevant lines covered (61.66%)

19.49 hits per line

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

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

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

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

32
        // create entity if not found
UNCOV
33
        if (entity == null)
×
34
        {
UNCOV
35
            entity = new TEntity { Id = id };
×
36

37
            // apply create metadata
UNCOV
38
            if (entity is ITrackCreated createdModel)
×
39
            {
UNCOV
40
                createdModel.Created = request.Activated;
×
41
                createdModel.CreatedBy = request.ActivatedBy;
×
42
            }
43
        }
44

45
        // copy updates from model to entity
UNCOV
46
        Mapper.Map(request.Model, entity);
×
47

48
        // apply update metadata
UNCOV
49
        if (entity is ITrackUpdated updateEntity)
×
50
        {
UNCOV
51
            updateEntity.Updated = request.Activated;
×
52
            updateEntity.UpdatedBy = request.ActivatedBy;
×
53
        }
54

55
        // save updates
UNCOV
56
        var savedEntity = await Repository
×
57
            .UpdateAsync(entity, cancellationToken)
×
58
            .ConfigureAwait(false);
×
59

60
        // return read model
UNCOV
61
        var model = Mapper.Map<TReadModel>(savedEntity);
×
62
        return model;
×
63
    }
×
64
}
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

© 2026 Coveralls, Inc