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

loresoft / MediatR.CommandQuery / 8911801772

01 May 2024 04:09PM UTC 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/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
        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
        // create entity if not found
34
        if (entity == null)
×
35
        {
36
            entity = new TEntity { Id = id };
×
37

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

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

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

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

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