Exemplo n.º 1
0
        public async Task <Unit> Handle(MakeTransfer command, CancellationToken cancellationToken = default)
        {
            var(fromAccountId, toAccountId, amount) = command;
            var accountFrom = await Store.AggregateStreamAsync <Account>(fromAccountId, token : cancellationToken)
                              ?? throw AggregateNotFoundException.For <Account>(fromAccountId);

            accountFrom.RecordOutflow(toAccountId, amount);

            var accountFromEvents = accountFrom.DequeueUncommittedEvents();

            Store.Append(accountFrom.Id, accountFromEvents);

            var accountTo = await Store.AggregateStreamAsync <Account>(toAccountId, token : cancellationToken)
                            ?? throw AggregateNotFoundException.For <Account>(toAccountId);

            accountTo.RecordInflow(fromAccountId, amount);

            var accountToEvents = accountFrom.DequeueUncommittedEvents();

            Store.Append(accountTo.Id, accountTo);

            await session.SaveChangesAsync(cancellationToken);

            await eventBus.Publish(accountFromEvents);

            await eventBus.Publish(accountToEvents);

            return(Unit.Value);
        }
        public async Task GivenAReferenceThatDoesNotExistsThenAnAggregateNotFoundExceptionIsThrownAsync()
        {
            _ = repository
                .Setup(repo => repo.GetAsync(
                           It.IsAny <Guid>(),
                           It.IsAny <CancellationToken?>(),
                           It.IsAny <SignedVersion>()))
                .ReturnsAsync(default(SerializableAggregateRoot));

            var aggregateId = Guid.NewGuid();
            var reference   = Reference <SerializableAggregateRoot> .Create(aggregateId);

            AggregateNotFoundException <SerializableAggregateRoot> exception =
                await Assert.ThrowsAsync <AggregateNotFoundException <SerializableAggregateRoot> >(
                    () => reference.RetrieveAsync(context, repository.Object));

            repository.Verify(
                repo => repo.GetAsync(
                    It.IsAny <Guid>(),
                    It.IsAny <CancellationToken?>(),
                    It.IsAny <SignedVersion>()),
                Times.Once);

            Assert.Equal(reference, exception.Aggregate);
            Assert.Equal(context, exception.Context);
        }
        private void HandleAggregateNotFoundException(ExceptionContext context)
        {
            AggregateNotFoundException aggregateNotFoundException =
                context.Exception as AggregateNotFoundException;

            context.Result = new ErrorResponse(aggregateNotFoundException).AsObjectResult();

            context.ExceptionHandled = true;
        }
Exemplo n.º 4
0
        public void UsingTheConstructorWithMessageReturnsExceptionWithExpectedProperties()
        {
            var sut = new AggregateNotFoundException(AggregateIdentifier, AggregateType, "Message");

            Assert.That(sut.Identifier, Is.EqualTo(AggregateIdentifier));
            Assert.That(sut.ClrType, Is.EqualTo(AggregateType));
            Assert.That(sut.Message, Is.EqualTo("Message"));
            Assert.That(sut.InnerException, Is.Null);
        }
Exemplo n.º 5
0
        public void UsingTheDefaultContstructorReturnsExceptionWithExpectedProperties()
        {
            var sut = new AggregateNotFoundException(AggregateIdentifier, AggregateType);

            Assert.That(sut.Identifier, Is.EqualTo(AggregateIdentifier));
            Assert.That(sut.ClrType, Is.EqualTo(AggregateType));
            Assert.That(sut.Message, Is.EqualTo(
                            "The Object aggregate with identifier " + AggregateIdentifier +
                            " could not be found. Please make sure the call site is indeed passing in an identifier for an Object aggregate."));
            Assert.That(sut.InnerException, Is.Null);
        }
Exemplo n.º 6
0
    public async Task <Unit> Handle(ConfirmReservation command, CancellationToken cancellationToken)
    {
        var reservation = await repository.Find(command.ReservationId, cancellationToken)
                          ?? throw AggregateNotFoundException.For <Reservation>(command.ReservationId);

        reservation.Confirm();

        await repository.Update(reservation, cancellationToken);

        return(Unit.Value);
    }
        public void UsingTheConstructorWithMessageReturnsExceptionWithExpectedProperties()
        {
            var aggregateId = Guid.NewGuid();
              var aggregateType = typeof(Object);
              var sut = new AggregateNotFoundException(aggregateId, aggregateType, "Message");

              Assert.That(sut.AggregateId, Is.EqualTo(aggregateId));
              Assert.That(sut.AggregateType, Is.EqualTo(aggregateType));
              Assert.That(sut.Message, Is.EqualTo("Message"));
              Assert.That(sut.InnerException, Is.Null);
        }
Exemplo n.º 8
0
        public async Task <Unit> Handle(ScheduleMeeting request, CancellationToken cancellationToken)
        {
            var meeting = await repository.Find(request.MeetingId, cancellationToken)
                          ?? throw AggregateNotFoundException.For <Meeting>(request.MeetingId);

            meeting.Schedule(request.Occurs);

            await repository.Update(meeting, cancellationToken);

            return(Unit.Value);
        }
        public void GivenAnAggregateAndAContextThenAnInstanceIsReturnedWithAllPropertiesSet()
        {
            var aggregate = Guid.NewGuid().ToReference <SerializableAggregateRoot>();
            var context   = new SerializableMessage();

            var instance = new AggregateNotFoundException <SerializableAggregateRoot>(
                context,
                aggregate);

            Assert.Equal(aggregate, instance.Aggregate);
            Assert.Equal(context, instance.Context);
        }
Exemplo n.º 10
0
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseExceptionHandlingMiddleware(exception => exception switch
        {
            AggregateNotFoundException _ => HttpStatusCode.NotFound,
            _ => HttpStatusCode.InternalServerError
        });
        public async Task <Unit> Handle(ScheduleMeeting command, CancellationToken cancellationToken)
        {
            var(meetingId, dateRange) = command;

            var meeting = await repository.Find(meetingId, cancellationToken)
                          ?? throw AggregateNotFoundException.For <Meeting>(meetingId);

            meeting.Schedule(dateRange);

            await repository.Update(meeting, cancellationToken);

            return(Unit.Value);
        }
Exemplo n.º 12
0
        public async Task <Unit> Handle(CancelReservation command, CancellationToken cancellationToken)
        {
            Guard.Against.Null(command, nameof(command));

            var reservation = await repository.Find(command.ReservationId, cancellationToken)
                              ?? throw AggregateNotFoundException.For <Reservation>(command.ReservationId);

            reservation.Cancel();

            await repository.Update(reservation, cancellationToken);

            return(Unit.Value);
        }
        public void GivenAnInstanceThenAllPropertiesAreSerialized()
        {
            var aggregate = Guid.NewGuid().ToReference <SerializableAggregateRoot>();
            var context   = new SerializableMessage();

            var original = new AggregateNotFoundException <SerializableAggregateRoot>(
                context,
                aggregate);

            AggregateNotFoundException <SerializableAggregateRoot> deserialized = original.Clone();

            Assert.NotSame(original, deserialized);
            Assert.Equal(original.Aggregate, deserialized.Aggregate);
            Assert.Equal(original.Context, deserialized.Context);
        }
    public async Task <CartDetails> Handle(GetCartAtVersion request, CancellationToken cancellationToken)
    {
        var cart = await eventStore.AggregateStream <CartDetails>(
            request.CartId,
            cancellationToken,
            request.Version
            );

        if (cart == null)
        {
            throw AggregateNotFoundException.For <Cart>(request.CartId);
        }

        return(cart);
    }
Exemplo n.º 15
0
    public async Task Handle(CartConfirmed @event, CancellationToken cancellationToken)
    {
        var cart = await querySession.LoadAsync <Cart>(@event.CartId, cancellationToken)
                   ?? throw  AggregateNotFoundException.For <Cart>(@event.CartId);

        var externalEvent = CartFinalized.Create(
            @event.CartId,
            cart.ClientId,
            cart.ProductItems.ToList(),
            cart.TotalPrice,
            @event.ConfirmedAt
            );

        await eventBus.Publish(externalEvent);
    }
        public static HttpStatusCodeInfo Map(Exception exception)
        {
            var code = exception switch
            {
                UnauthorizedAccessException _ => HttpStatusCode.Unauthorized,
                NotImplementedException _ => HttpStatusCode.NotImplemented,
                InvalidOperationException _ => HttpStatusCode.Conflict,
                ArgumentException _ => HttpStatusCode.BadRequest,
                ValidationException _ => HttpStatusCode.BadRequest,
                AggregateNotFoundException _ => HttpStatusCode.NotFound,
                _ => HttpStatusCode.InternalServerError
            };

            return(new HttpStatusCodeInfo(code, exception.Message));
        }
    }
Exemplo n.º 17
0
        public void CanBeSerialized()
        {
            var innerException = new Exception("InnerMessage");
            var sut            = new AggregateNotFoundException(AggregateIdentifier, AggregateType, "Message", innerException);

            using (var stream = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, sut);
                stream.Position = 0;
                var result = (AggregateNotFoundException)formatter.Deserialize(stream);

                Assert.That(result.Identifier, Is.EqualTo(AggregateIdentifier));
                Assert.That(result.ClrType, Is.EqualTo(AggregateType));
                Assert.That(result.Message, Is.EqualTo(result.Message));
                Assert.That(result.InnerException.Message, Is.EqualTo(result.InnerException.Message));
            }
        }
        public void CanBeSerialized()
        {
            var innerException = new Exception("InnerMessage");
              var aggregateId = Guid.NewGuid();
              var aggregateType = typeof(Object);
              var sut = new AggregateNotFoundException(aggregateId, aggregateType, "Message", innerException);

              using (var stream = new MemoryStream()) {
            var formatter = new BinaryFormatter();
            formatter.Serialize(stream, sut);
            stream.Position = 0;
            var result = (AggregateNotFoundException)formatter.Deserialize(stream);

            Assert.That(sut.AggregateId, Is.EqualTo(aggregateId));
            Assert.That(sut.AggregateType, Is.EqualTo(aggregateType));
            Assert.That(sut.Message, Is.EqualTo(result.Message));
            Assert.That(sut.InnerException.Message, Is.EqualTo(result.InnerException.Message));
              }
        }
        public void UsingTheDefaultContstructorReturnsExceptionWithExpectedProperties()
        {
            var aggregateId = Guid.NewGuid();
              var aggregateType = typeof (Object);
              var sut = new AggregateNotFoundException(aggregateId, aggregateType);

              Assert.That(sut.AggregateId, Is.EqualTo(aggregateId));
              Assert.That(sut.AggregateType, Is.EqualTo(aggregateType));
              Assert.That(sut.Message, Is.EqualTo(
            "The Object aggregate with identifier " + aggregateId + " could not be found. Please make sure the callsite is indeed passing in an identifier for an Object aggregate."));
              Assert.That(sut.InnerException, Is.Null);
        }
Exemplo n.º 20
0
 public ErrorResponse(AggregateNotFoundException aggregateNotFoundException)
 {
     this.StatusCode = StatusCodes.Status404NotFound;
     this.Message    = aggregateNotFoundException.Message;
 }