• 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

93.33
/src/MediatR.CommandQuery.MongoDB/Handlers/EntityUpsertCommandHandler.cs
1
using AutoMapper;
2

3
using MediatR.CommandQuery.Commands;
4
using MediatR.CommandQuery.Definitions;
5

6
using Microsoft.Extensions.Logging;
7

8
using MongoDB.Abstracts;
9

10
namespace MediatR.CommandQuery.MongoDB.Handlers;
11

12
public class EntityUpsertCommandHandler<TRepository, TEntity, TKey, TUpdateModel, TReadModel>
13
    : RepositoryHandlerBase<TRepository, TEntity, TKey, EntityUpsertCommand<TKey, TUpdateModel, TReadModel>, TReadModel>
14
    where TRepository : IMongoRepository<TEntity, TKey>
15
    where TEntity : class, IHaveIdentifier<TKey>, new()
16
{
17
    public EntityUpsertCommandHandler(ILoggerFactory loggerFactory, TRepository repository, IMapper mapper)
18
        : base(loggerFactory, repository, mapper)
12✔
19
    {
20
    }
12✔
21

22
    protected override async Task<TReadModel> Process(EntityUpsertCommand<TKey, TUpdateModel, TReadModel> request, CancellationToken cancellationToken)
23
    {
24
        if (request is null)
12!
25
            throw new ArgumentNullException(nameof(request));
×
26

27
        var entity = await Repository
12✔
28
            .FindAsync(request.Id, cancellationToken)
12✔
29
            .ConfigureAwait(false);
12✔
30

31
        // create entity if not found
32
        if (entity == null)
12✔
33
        {
34
            entity = new TEntity { Id = request.Id };
6✔
35

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

44
        // copy updates from model to entity
45
        Mapper.Map(request.Model, entity);
12✔
46

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

54
        // save updates
55
        var savedEntity = await Repository
12✔
56
            .UpsertAsync(entity, cancellationToken)
12✔
57
            .ConfigureAwait(false);
12✔
58

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