public Command Map(CloudEventRequest request)
        {
            Ensure.NotNull(request, nameof(request));
            Ensure.NotNull(request.Data, nameof(request.Data));

            if (!_dataContentTypes.Contains(request.DataContentType))
            {
                throw new ArgumentException($"While running Map in '{nameof(CreateDiaryMapper)}' I can't recognize the DataContentType:{request.DataContentType}");
            }
            if (!request.DataSchema.Equals(Schema) || !request.Source.Equals(Source))
            {
                throw new ArgumentException($"While running Map in '{nameof(CreateDiaryMapper)}' I can't recognize the data (DataSchema:{request.DataSchema};Source:{request.Source})");
            }

            CreateDiary cmd = JsonSerializer.Deserialize <CreateDiary>(request.Data.ToString());

            cmd.Metadata = new Dictionary <string, string>
            {
                { "$correlationId", cmd.CorrelationId },
                { "source", request.Source.ToString() },
                { "$applies", request.Time.ToString("O") },
                { "cloudrequest-id", request.Id },
                { "schema", request.DataSchema.ToString() },
                { "content-type", request.DataContentType }
            };
            return(cmd);
        }
Пример #2
0
        internal void Process(CloudEventRequest cloudRequest)
        {
            if (!_deserializers.ContainsKey(cloudRequest.DataSchema.ToString()) &&
                !_deserializers.ContainsKey($"{cloudRequest.DataSchema}{cloudRequest.Source}"))
            {
                throw new Exception(
                          $"I can't find a mapper for schema:'{cloudRequest.DataSchema}' source:''{cloudRequest.Source}''");
            }

            var command = _deserializers.ContainsKey(cloudRequest.DataSchema.ToString())
                ? _deserializers[cloudRequest.DataSchema.ToString()](cloudRequest)
                : _deserializers[$"{cloudRequest.DataSchema}{cloudRequest.Source}"](cloudRequest);

            if (command == null)
            {
                throw new Exception(
                          $"I received CloudRequest Type:'{cloudRequest.Type}' Source:'{cloudRequest.Source}' Schema:'{cloudRequest.DataSchema}' but I was unable to deserialize a Command out of it");
            }

            IAggregate aggregate = null;

            try
            {
                switch (command)
                {
                case CreateDiary createDiary:
                    aggregate = Handle(createDiary);
                    break;

                case Log log:
                    aggregate = Handle(log);
                    break;
                }

                // Add here any further command matches

                if (aggregate == null)
                {
                    throw new Exception(
                              $"Received CloudRequest Type:'{cloudRequest.Type}' Source:'{cloudRequest.Source}' Schema:'{cloudRequest.DataSchema}' but I can't find an available handler for it");
                }
            }
            finally
            {
                if (aggregate != null && aggregate.UncommitedEvents().Any())
                {
                    var uncommittedEventsList = aggregate.UncommitedEvents().ToList();
                    _domainRepositories[command.Metadata["source"]].Save(aggregate);

                    var error = new StringBuilder();
                    foreach (var uncommittedEvent in uncommittedEventsList)
                    {
                        Log.Info($"Handled '{cloudRequest.Type}' AggregateId:'{aggregate.AggregateId}' [0]Resulted event:'{uncommittedEvent.GetType()}'");

                        if (uncommittedEvent.GetType().ToString().EndsWith("FailedV1"))
                        {
                            error.Append(HandleFailedEvent(uncommittedEvent, command));
                        }
                    }

                    if (error.Length > 0)
                    {
                        throw new BusinessException(error.ToString());
                    }
                }
                else
                {
                    Log.Info(
                        $"Handled CloudRequest Type:'{cloudRequest.Type}' Source:'{cloudRequest.Source}' Schema:'{cloudRequest.DataSchema}' with no events to save");
                }
            }
        }