• 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

86.21
/src/MediatR.CommandQuery.EntityFrameworkCore/Handlers/EntityPatchCommandHandler.cs
1
using AutoMapper;
2

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

6
using Microsoft.EntityFrameworkCore;
7
using Microsoft.Extensions.Logging;
8

9
namespace MediatR.CommandQuery.EntityFrameworkCore.Handlers;
10

11
public class EntityPatchCommandHandler<TContext, TEntity, TKey, TReadModel>
12
    : EntityDataContextHandlerBase<TContext, TEntity, TKey, TReadModel, EntityPatchCommand<TKey, TReadModel>, TReadModel>
13
    where TContext : DbContext
14
    where TEntity : class, IHaveIdentifier<TKey>, new()
15
{
16
    public EntityPatchCommandHandler(ILoggerFactory loggerFactory, TContext dataContext, IMapper mapper)
17
        : base(loggerFactory, dataContext, mapper)
2✔
18
    {
19
    }
2✔
20

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

26
        var dbSet = DataContext
2✔
27
            .Set<TEntity>();
2✔
28

29
        var keyValue = new object[] { request.Id };
2✔
30

31
        // find entity to update by message id, not model id
32
        var entity = await dbSet
2✔
33
            .FindAsync(keyValue, cancellationToken)
2✔
34
            .ConfigureAwait(false);
2✔
35

36
        if (entity == null)
2!
37
            return default!;
×
38

39
        // apply json patch to entity
40
        var jsonPatch = request.Patch;
2✔
41
        jsonPatch.ApplyTo(entity);
2✔
42

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

50
        await DataContext
2✔
51
            .SaveChangesAsync(cancellationToken)
2✔
52
            .ConfigureAwait(false);
2✔
53

54
        // return read model
55
        var model = await Read(entity.Id, cancellationToken).ConfigureAwait(false);
2✔
56

57
        return model;
2✔
58
    }
2✔
59
}
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