Beispiel #1
0
        public async Task <AllCommittedEventsPage> LoadAllCommittedEvents(
            GlobalPosition globalPosition,
            int pageSize,
            CancellationToken cancellationToken)
        {
            var startPostion = globalPosition.IsStart
                ? 0
                : long.Parse(globalPosition.Value);
            var endPosition = startPostion + pageSize;

            const string sql             = @"
                SELECT
                    GlobalSequenceNumber, BatchId, AggregateId, AggregateName, Data, Metadata, AggregateSequenceNumber
                FROM EventFlow
                WHERE
                    GlobalSequenceNumber >= @FromId AND GlobalSequenceNumber <= @ToId
                ORDER BY
                    GlobalSequenceNumber ASC";
            var          eventDataModels = await _connection.QueryAsync <EventDataModel>(
                Label.Named("mssql-fetch-events"),
                cancellationToken,
                sql,
                new
            {
                FromId = startPostion,
                ToId   = endPosition,
            })
                                           .ConfigureAwait(false);

            var nextPosition = eventDataModels.Any()
                ? eventDataModels.Max(e => e.GlobalSequenceNumber) + 1
                : startPostion;

            return(new AllCommittedEventsPage(new GlobalPosition(nextPosition.ToString()), eventDataModels));
        }
Beispiel #2
0
        public async Task <string> GetAsync(SettingKey settingKey, SettingGroupKey settingGroupKey, CancellationToken cancellationToken)
        {
            var values = await _msSqlConnection.QueryAsync <string>(
                cancellationToken,
                GetSql,
                new { Key = settingKey.Value, GroupKey = settingGroupKey.Value })
                         .ConfigureAwait(false);

            return(values.SingleOrDefault());
        }
Beispiel #3
0
        public override async Task <ReadModelEnvelope <TReadModel> > GetAsync(string id, CancellationToken cancellationToken)
        {
            var readModelType           = typeof(TReadModel);
            var readModelNameLowerCased = readModelType.Name.ToLowerInvariant();
            var selectSql  = _readModelSqlGenerator.CreateSelectSql <TReadModel>();
            var readModels = await _connection.QueryAsync <TReadModel>(
                Label.Named(string.Format("mssql-fetch-read-model-{0}", readModelNameLowerCased)),
                cancellationToken,
                selectSql,
                new { EventFlowReadModelId = id })
                             .ConfigureAwait(false);

            var readModel = readModels.SingleOrDefault();

            if (readModel == null)
            {
                Log.Verbose(() => $"Could not find any MSSQL read model '{readModelType.PrettyPrint()}' with ID '{id}'");
                return(ReadModelEnvelope <TReadModel> .Empty(id));
            }

            var readModelVersion = GetVersion(readModel);

            Log.Verbose(() => $"Foud MSSQL read model '{readModelType.PrettyPrint()}' with ID '{readModelVersion}'");

            return(readModelVersion.HasValue
                ? ReadModelEnvelope <TReadModel> .With(id, readModel, readModelVersion.Value)
                : ReadModelEnvelope <TReadModel> .With(id, readModel));
        }
Beispiel #4
0
        public override async Task UpdateReadModelAsync(string aggregateId, IReadOnlyCollection <IDomainEvent> domainEvents, CancellationToken cancellationToken)
        {
            var readModels = await _connection.QueryAsync <TReadModel>(
                cancellationToken,
                SelectSql,
                new { AggregateId = aggregateId })
                             .ConfigureAwait(false);

            var readModel = readModels.SingleOrDefault();
            var isNew     = false;

            if (readModel == null)
            {
                isNew     = true;
                readModel = new TReadModel
                {
                    AggregateId = aggregateId,
                    CreateTime  = domainEvents.First().Timestamp,
                };
            }

            ApplyEvents(readModel, domainEvents);

            var lastDomainEvent = domainEvents.Last();

            readModel.UpdatedTime = lastDomainEvent.Timestamp;
            readModel.LastAggregateSequenceNumber = lastDomainEvent.AggregateSequenceNumber;
            readModel.LastGlobalSequenceNumber    = lastDomainEvent.GlobalSequenceNumber;

            var sql = isNew ? InsertSql : UpdateSql;

            await _connection.ExecuteAsync(cancellationToken, sql, readModel).ConfigureAwait(false);
        }
Beispiel #5
0
        protected override async Task <IReadOnlyCollection <ICommittedDomainEvent> > LoadCommittedEventsAsync(string id, CancellationToken cancellationToken)
        {
            const string sql             = @"SELECT * FROM EventFlow WHERE AggregateId = @AggregateId ORDER BY AggregateSequenceNumber ASC";
            var          eventDataModels = await _connection.QueryAsync <EventDataModel>(cancellationToken, sql, new { AggregateId = id })
                                           .ConfigureAwait(false);

            return(eventDataModels);
        }
Beispiel #6
0
        public async Task <EmployeeDto[]> ExecuteQueryAsync(GetAllEmployeesQuery query, CancellationToken cancellationToken)
        {
            var readModels = await _msSqlConnection.QueryAsync <EmployeeReadModel>(
                Label.Named(nameof(GetAllEmployeesQueryHandler)),
                cancellationToken,
                "SELECT * FROM [ReadModel-Employee]")
                             .ConfigureAwait(false);

            return(readModels.Select(rm => rm.ToEmployeeDto()).ToArray());
        }
Beispiel #7
0
        public async Task <IEnumerable <PaymentDto> > ExecuteQueryAsync(GetAllPaymentsQuery query, CancellationToken cancellationToken)
        {
            var readModels = await _msSqlConnection.QueryAsync <PaymentReadModel>(
                Label.Named(nameof(GetAllInvoicesQueryHandler)),
                cancellationToken,
                "SELECT * FROM [ReadModel-Payment]")
                             .ConfigureAwait(false);

            return(readModels.Select(rm => rm.ToPaymentDto()));
        }
Beispiel #8
0
        public async Task <CustomerDto[]> ExecuteQueryAsync(GetAllCustomersQuery query, CancellationToken cancellationToken)
        {
            var readModels = await _msSqlConnection.QueryAsync <CustomerReadModel>(
                Label.Named("mssql-fetch-loanapp-read-model"),
                cancellationToken,
                "SELECT * FROM [ReadModel-Customer]")
                             .ConfigureAwait(false);

            return(readModels.Select(rm => rm.ToCustomerDto()).ToArray());
        }
Beispiel #9
0
        public async Task <IReadOnlyCollection <OrderReadModel> > ExecuteQueryAsync(GetOrdersQuery query, CancellationToken cancellationToken)
        {
            var readModels = await _msSqlConnection.QueryAsync <OrderReadModel>(
                Label.Named("Orders"),
                cancellationToken,
                "SELECT * FROM [Orders]")
                             .ConfigureAwait(false);

            return(readModels.ToList());
        }
Beispiel #10
0
        public async Task <long?> ExecuteQueryAsync(ThingyGetVersionQuery query, CancellationToken cancellationToken)
        {
            var readModels = await _msSqlConnection.QueryAsync <MsSqlThingyReadModel>(
                Label.Named("mssql-fetch-test-read-model"),
                cancellationToken,
                "SELECT * FROM [ReadModel-ThingyAggregate] WHERE AggregateId = @AggregateId",
                new { AggregateId = query.ThingyId.Value })
                             .ConfigureAwait(false);

            return(readModels.SingleOrDefault()?.LastAggregateSequenceNumber);
        }
Beispiel #11
0
        public async Task <PaymentDetailsReadModel> ExecuteQueryAsync(GetPaymentDetailsQuery query, CancellationToken cancellationToken)
        {
            var readModel = await _msSqlConnection.QueryAsync <PaymentDetailsReadModel>(
                Label.Named("mssql-get-payment-details-read-model"),
                cancellationToken,
                $"SELECT * FROM [ReadModel-PaymentDetails] WHERE {nameof(PaymentDetailsReadModel.ExternalId)} = @{nameof(query.ExternalId)}",
                new { query.ExternalId })
                            .ConfigureAwait(false);

            return(readModel.SingleOrDefault());
        }
Beispiel #12
0
        public async Task <IReadOnlyCollection <OrderLineReadModel> > ExecuteQueryAsync(GetOrderLinesByOrderIdQuery query, CancellationToken cancellationToken)
        {
            var readModels = await _msSqlConnection.QueryAsync <OrderLineReadModel>(
                Label.Named("OrderLines"),
                cancellationToken,
                "SELECT * FROM [OrderLines] WHERE OrderId = @OrderId",
                new { OrderId = query.OrderId })
                             .ConfigureAwait(false);

            return(readModels.ToList());
        }
        public async Task <BankStatementAnalysisSummaryView> ExecuteQueryAsync(BankStatementAnalysisSummaryQuery query,
                                                                               CancellationToken cancellationToken)
        {
            Logger.Debug($"query: ", query.BankStatementId);
            //    var aggregate = await _aggregateStore.LoadAsync<BankStatementAggregate, BankStatementId>(query.BankStatementId,cancellationToken);

            var readModels = await _msSqlConnection.QueryAsync <BankStatementReadModel>(
                Label.Named("mssql-fetch-BankStatementReadModel"),
                cancellationToken,
                "SELECT * FROM [BankStatementReadModel] WHERE AggregateId = @AggregateId AND TransactionUtcDate BETWEEN @FromDate AND @ToDate",
                new
            {
                AggregateId = query.BankStatementId.Value,
                FromDate    = query.FromUtcDateTime,
                ToDate      = query.ToUtcDateTime
            })
                             .ConfigureAwait(false);

            var converted = readModels
                            .ToList()
                            .Select(x => new { x.Category, x.SubCategory, x.Amount })
                            .ToList();

            Logger.Debug($"query results: {converted.Count}");

            var cats = from p in converted
                       group p by p.Category
                       into g
                       select new { Category = g.Key, Total = converted.Where(a => a.Category == g.Key)
                                                              .Sum(x => x.Amount) };

            var subs = from p in converted.Where(x => !string.IsNullOrEmpty(x.SubCategory))
                       group p by p.SubCategory
                       into g
                       select new { SubCategory = g.Key, Total = converted.Where(a => a.SubCategory == g.Key)
                                                                 .Sum(x => x.Amount) };

            var view = new BankStatementAnalysisSummaryView
            {
                BankStatementId   = query.BankStatementId.Value,
                CategoryTotals    = new ConcurrentDictionary <string, decimal>(),
                SubCategoryTotals = new ConcurrentDictionary <string, decimal>(),
                Unmatched         = readModels.Where(x => x.SubCategory == SubCategories.Uncategorised)
                                    .Select(x => x.Description)
                                    .ToList()
            };

            cats.AsParallel().ForAll(x => view.CategoryTotals.Add(new KeyValuePair <string, decimal>(x.Category, x.Total)));
            subs.AsParallel().ForAll((x => view.SubCategoryTotals.Add(new KeyValuePair <string, decimal>(x.SubCategory, x.Total))));

            return(view);
        }
Beispiel #14
0
        public async Task <IReadOnlyCollection <ThingyMessage> > ExecuteQueryAsync(ThingyGetMessagesQuery query, CancellationToken cancellationToken)
        {
            var readModels = await _msSqlConnection.QueryAsync <MsSqlThingyMessageReadModel>(
                Label.Named("mssql-fetch-thingy-message-read-model"),
                cancellationToken,
                "SELECT * FROM [ReadModel-ThingyMessage] WHERE ThingyId = @ThingyId",
                new { ThingyId = query.ThingyId.Value })
                             .ConfigureAwait(false);

            return(readModels
                   .Select(rm => rm.ToThingyMessage())
                   .ToList());
        }
        public override async Task <ReadModelEnvelope <TReadModel> > GetAsync(string id, CancellationToken cancellationToken)
        {
            var readModelNameLowerCased = typeof(TReadModel).Name.ToLowerInvariant();
            var selectSql  = _readModelSqlGenerator.CreateSelectSql <TReadModel>();
            var readModels = await _connection.QueryAsync <TReadModel>(
                Label.Named(string.Format("mssql-fetch-read-model-{0}", readModelNameLowerCased)),
                cancellationToken,
                selectSql,
                new { AggregateId = id })
                             .ConfigureAwait(false);

            var readModel = readModels.SingleOrDefault();

            return(readModel == null
                ? ReadModelEnvelope <TReadModel> .Empty
                : ReadModelEnvelope <TReadModel> .With(readModel, readModel.LastAggregateSequenceNumber));
        }