• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / MediatR.CommandQuery / 8410320720

24 Mar 2024 03:15PM CUT coverage: 58.562%. Remained the same
8410320720

push

github

pwelter34
Update EntityQueryEndpointBase.cs

230 of 438 branches covered (52.51%)

Branch coverage included in aggregate %.

560 of 911 relevant lines covered (61.47%)

19.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/MediatR.CommandQuery.Cosmos/Handlers/EntityCreateCommandHandler.cs
1
using System;
2
using System.Threading;
3
using System.Threading.Tasks;
4

5
using AutoMapper;
6

7
using Cosmos.Abstracts;
8

9
using MediatR.CommandQuery.Commands;
10
using MediatR.CommandQuery.Definitions;
11

12
using Microsoft.Extensions.Logging;
13

14
namespace MediatR.CommandQuery.Cosmos.Handlers;
15

16
public class EntityCreateCommandHandler<TRepository, TEntity, TCreateModel, TReadModel>
17
    : RepositoryHandlerBase<TRepository, TEntity, EntityCreateCommand<TCreateModel, TReadModel>, TReadModel>
18
    where TRepository : ICosmosRepository<TEntity>
19
    where TEntity : class, IHaveIdentifier<string>, new()
20
{
21
    public EntityCreateCommandHandler(ILoggerFactory loggerFactory, TRepository repository, IMapper mapper)
22
        : base(loggerFactory, repository, mapper)
×
23
    {
24
    }
×
25

26
    protected override async Task<TReadModel> Process(EntityCreateCommand<TCreateModel, TReadModel> request, CancellationToken cancellationToken)
27
    {
28
        if (request is null)
29
            throw new ArgumentNullException(nameof(request));
30

31
        // create new entity from model
32
        var entity = Mapper.Map<TEntity>(request.Model);
33

34
        // apply create metadata
35
        if (entity is ITrackCreated createdModel)
36
        {
37
            createdModel.Created = request.Activated;
38
            createdModel.CreatedBy = request.ActivatedBy;
39
        }
40

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

48
        var result = await Repository
49
            .CreateAsync(entity, cancellationToken)
50
            .ConfigureAwait(false);
51

52
        // convert to read model
53
        return Mapper.Map<TReadModel>(result);
54
    }
55
}
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