• 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

92.11
/src/MediatR.CommandQuery.EntityFrameworkCore/Handlers/EntityUpsertCommandHandler.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 EntityUpsertCommandHandler<TContext, TEntity, TKey, TUpdateModel, TReadModel>
12
    : EntityDataContextHandlerBase<TContext, TEntity, TKey, TReadModel, EntityUpsertCommand<TKey, TUpdateModel, TReadModel>, TReadModel>
13
    where TContext : DbContext
14
    where TEntity : class, IHaveIdentifier<TKey>, new()
15
{
16
    public EntityUpsertCommandHandler(ILoggerFactory loggerFactory, TContext dataContext, IMapper mapper)
17
        : base(loggerFactory, dataContext, mapper)
4✔
18
    {
19

20
    }
4✔
21

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

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

30
        // don't query if default value
31
        var entity = !EqualityComparer<TKey>.Default.Equals(request.Id, default)
4!
32
            ? await dbSet.FindAsync([request.Id], cancellationToken).ConfigureAwait(false)
4✔
33
            : default;
4✔
34

35
        // create entity if not found
36
        if (entity == null)
4✔
37
        {
38
            entity = new TEntity();
2✔
39
            entity.Id = request.Id;
2✔
40

41
            // apply create metadata
42
            if (entity is ITrackCreated createdModel)
2✔
43
            {
44
                createdModel.Created = request.Activated;
2✔
45
                createdModel.CreatedBy = request.ActivatedBy;
2✔
46
            }
47

48
            await dbSet
2✔
49
                .AddAsync(entity, cancellationToken)
2✔
50
                .ConfigureAwait(false);
2✔
51
        }
52

53
        // copy updates from model to entity
54
        Mapper.Map(request.Model, entity);
4✔
55

56
        // apply update metadata
57
        if (entity is ITrackUpdated updateEntity)
4✔
58
        {
59
            updateEntity.Updated = request.Activated;
4✔
60
            updateEntity.UpdatedBy = request.ActivatedBy;
4✔
61
        }
62

63
        // save updates
64
        await DataContext
4✔
65
            .SaveChangesAsync(cancellationToken)
4✔
66
            .ConfigureAwait(false);
4✔
67

68
        // return read model
69
        var readModel = await Read(entity.Id, cancellationToken).ConfigureAwait(false);
4✔
70

71
        return readModel;
4✔
72
    }
4✔
73
}
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