-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPullRequestStateProvider.cs
33 lines (25 loc) · 1.16 KB
/
PullRequestStateProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright (c) Arjen Post. See LICENSE in the project root for license information.
using Giddup.ApplicationCore.Application.PullRequests;
using Giddup.ApplicationCore.Domain.PullRequests;
using Microsoft.EntityFrameworkCore;
namespace Giddup.Infrastructure.PullRequests.CommandModel;
public class PullRequestStateProvider : IPullRequestStateProvider
{
private readonly GiddupDbContext _dbContext;
public PullRequestStateProvider(GiddupDbContext dbContext)
=> _dbContext = dbContext;
public async Task<(IPullRequestState State, long? Version)> Provide(Guid pullRequestId)
{
var events = await _dbContext.Events
.Where(@event => @event.AggregateId == pullRequestId)
.Select(@event => new { Version = @event.AggregateVersion, Event = PullRequestEventSerializer.Deserialize(@event) })
.ToListAsync();
var state = events
.Select(@event => @event.Event)
.Aggregate(IPullRequestState.InitialState, IPullRequestState.ProcessEvent);
var version = events
.Select(@event => (long?)@event.Version)
.LastOrDefault();
return (state, version);
}
}