• 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/EntityUpdateCommandHandler.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 EntityUpdateCommandHandler<TContext, TEntity, TKey, TUpdateModel, TReadModel>
12
    : EntityDataContextHandlerBase<TContext, TEntity, TKey, TReadModel, EntityUpdateCommand<TKey, TUpdateModel, TReadModel>, TReadModel>
13
    where TContext : DbContext
14
    where TEntity : class, IHaveIdentifier<TKey>, new()
15
{
16
    public EntityUpdateCommandHandler(ILoggerFactory loggerFactory, TContext dataContext, IMapper mapper)
17
        : base(loggerFactory, dataContext, mapper)
3✔
18
    {
19

20
    }
3✔
21

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

27
        var dbSet = DataContext
3✔
28
            .Set<TEntity>();
3✔
29

30
        var keyValue = new object[] { request.Id };
3✔
31

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

37
        if (entity == null)
3!
38
            return default!;
×
39

40
        // copy updates from model to entity
41
        Mapper.Map(request.Model, entity);
3✔
42

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

50
        // save updates
51
        await DataContext
3✔
52
            .SaveChangesAsync(cancellationToken)
3✔
53
            .ConfigureAwait(false);
3✔
54

55
        // return read model
56
        var readModel = await Read(entity.Id, cancellationToken)
3✔
57
            .ConfigureAwait(false);
3✔
58

59
        return readModel;
3✔
60
    }
3✔
61
}
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