• 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

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

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

26
        // create new entity from model
27
        var entity = Mapper.Map<TEntity>(request.Model);
4✔
28

29
        // apply create metadata
30
        if (entity is ITrackCreated createdModel)
4✔
31
        {
32
            createdModel.Created = request.Activated;
4✔
33
            createdModel.CreatedBy = request.ActivatedBy;
4✔
34
        }
35

36
        // apply update metadata
37
        if (entity is ITrackUpdated updateEntity)
4✔
38
        {
39
            updateEntity.Updated = request.Activated;
4✔
40
            updateEntity.UpdatedBy = request.ActivatedBy;
4✔
41
        }
42

43
        var dbSet = DataContext
4✔
44
            .Set<TEntity>();
4✔
45

46
        // add to data set, id should be generated
47
        await dbSet
4✔
48
            .AddAsync(entity, cancellationToken)
4✔
49
            .ConfigureAwait(false);
4✔
50

51
        // save to database
52
        await DataContext
4✔
53
            .SaveChangesAsync(cancellationToken)
4✔
54
            .ConfigureAwait(false);
4✔
55

56
        // convert to read model
57
        var readModel = await Read(entity.Id, cancellationToken)
4✔
58
            .ConfigureAwait(false);
4✔
59

60
        return readModel;
4✔
61
    }
4✔
62
}
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