public async Task <IActionResult> Create([FromBody] CreateAuctionViewModel viewModel)
        {
            var command = new CreateAuctionCommand(viewModel.Name, viewModel.AuctionDate);
            var result  = await _commandBus.ExecuteAsync <CreateAuctionCommand, AuctionId>(command);

            return(CreatedAtRoute("GetAuctionById", new { id = result.Result.ToString() }, null));
        }
예제 #2
0
        public long Create(CreateAuctionCommand command)
        {
            long createdId = 0;

            _listener.Subscribe(new ActionEventHandler <AuctionOpened>(a => createdId = a.Id));
            _bus.Dispatch(command);
            return(createdId);
        }
        public async Task WhenAuctionDateIsInThePastThenInvalid()
        {
            var command = new CreateAuctionCommand("one", DateTimeOffset.UtcNow.AddDays(-1));

            var result = await _validator.ValidateAsync(command);

            Assert.False(result.IsValid);
        }
        protected override void PublishEvents(Auction auction, User user, CreateAuctionCommand createAuctionCommand, CorrelationId correlationId)
        {
            if (EventBusThrows)
            {
                throw new NotImplementedException();
            }

            base.PublishEvents(auction, user, createAuctionCommand, correlationId);
        }
예제 #5
0
        public void Handle(CreateAuctionCommand command)
        {
            var money       = new Money(command.StartingPrice, "USD");
            var id          = _repository.GetNextId();
            var participant = _participantService.GetById(command.SellerId);

            var auction = new Auction(id, command.EndDateTime,
                                      command.ProductId, participant, money, _service, _publisher);

            _repository.Add(auction);
        }
예제 #6
0
        public async Task <IActionResult> CreateAuction(CreateAuctionCommand command)
        {
            var id = Guid.NewGuid();

            command.Id = id;

            command.UserId = Guid.Parse(User.GetObjectId() ?? throw new InvalidOperationException());

            await _mediator.Send(command);

            return(AcceptedAtAction(nameof(Get), nameof(AuctionsController), new { id }));
        }
예제 #7
0
        private bool VerifyEvent(AuctionCreated auctionCreated, CreateAuctionCommand command)
        {
            var v1 = auctionCreated != null;
            var v2 = auctionCreated.AuctionId != Guid.Empty;
            var v3 = auctionCreated.AuctionArgs.Product.Name.Equals(command.Product.Name) &&
                     auctionCreated.AuctionArgs.Product.Description.Equals(command.Product.Description);
            var v4 = auctionCreated.AuctionArgs.BuyNowPrice.Equals(command.BuyNowPrice);
            var v5 = auctionCreated.AuctionArgs.StartDate.Value.CompareTo(command.StartDate) == 0;
            var v6 = auctionCreated.AuctionArgs.Creator.UserId.Equals(user.UserIdentity.UserId);

            return(v1 && v2 && v3 && v4 && v5 && v6);
        }
예제 #8
0
        CreateAuctionCommand GetCreateCommand()
        {
            var categories = new List <string>()
            {
                "Fake category", "Fake subcategory", "Fake subsubcategory 0"
            };
            var cmd = new CreateAuctionCommand(20.0m, product, DateTime.UtcNow.AddMinutes(20),
                                               DateTime.UtcNow.AddDays(12),
                                               categories, Tag.From(new[] { "tag1" }), "test name", false);

            cmd.SignedInUser         = user.UserIdentity;
            cmd.AuctionCreateSession = session;
            return(cmd);
        }
        public void Handle_when_repository_throws_exception_throws()
        {
            testCommandHandler.AuctionRepositoryThrows = true;
            var command = new CreateAuctionCommand(Decimal.One,
                                                   new Product("test product name", "example description", Condition.New),
                                                   startDate, endDate, categories, Tag.From(new [] { "tag1" }), "test name", false);

            command.SignedInUser         = user.UserIdentity;
            command.AuctionCreateSession = user.UserIdentity.GetAuctionCreateSession();


            Assert.Throws <Exception>(() =>
            {
                testCommandHandler.Handle(command, CancellationToken.None)
                .Wait();
            });

            Thread.Sleep(120_000);

            called[testCommandHandler.AddedAuction.AggregateId].Should()
            .BeFalse();
        }
        public void Handle_when_called_adds_auction_to_repository_and_schedules_end()
        {
            var command = new CreateAuctionCommand(Decimal.One,
                                                   new Product("test product name", "example description", Condition.New),
                                                   startDate, endDate,
                                                   categories, Tag.From(new [] { "tag1" }), "test name", false);

            command.SignedInUser         = user.UserIdentity;
            command.AuctionCreateSession = user.UserIdentity.GetAuctionCreateSession();
            testCommandHandler.Handle(command, CancellationToken.None)
            .Wait();

            Thread.Sleep(120_000);

            var added = testDepedencies.AuctionRepository.FindAuction(testCommandHandler.AddedAuction.AggregateId);

            called[added.AggregateId].Should()
            .BeTrue();
            added.StartDate.Value.Year.Should().Be(startDate.Year);
            added.StartDate.Value.Month.Should().Be(startDate.Month);
            added.StartDate.Value.Day.Should().Be(startDate.Day);
            added.StartDate.Value.Hour.Should().Be(startDate.Hour);
            added.StartDate.Value.Minute.Should().Be(startDate.Minute);
        }
        public void METHOD()
        {
            var services = TestDepedencies.Instance.Value;
            var user     = new User();

            user.Register("testUserName");
            user.MarkPendingEventsAsHandled();
            var product       = new Product("test product name", "example description", Condition.New);
            var sem           = new SemaphoreSlim(0, 1);
            var sem2          = new SemaphoreSlim(0, 1);
            var correlationId = new CorrelationId("test_correlationId");
            var categories    = new List <string>()
            {
                "Fake category", "Fake subcategory", "Fake subsubcategory 0"
            };

            var userRepository = new Mock <IUserRepository>();

            userRepository.Setup(f => f.FindUser(It.IsAny <UserIdentity>()))
            .Returns(user);

            var command = new CreateAuctionCommand(20.0m, product, DateTime.UtcNow.AddMinutes(10),
                                                   DateTime.UtcNow.AddDays(12),
                                                   categories, Tag.From(new[] { "tag1" }), "test auction name", false);

            command.SignedInUser = user.UserIdentity;

            IAppEvent <AuctionCreated> publishedEvent = null;


            var eventHandler = new Mock <TestAuctionCreatedHandler>(
                services.AppEventBuilder,
                services.DbContext,
                Mock.Of <IRequestStatusService>()
                );

            eventHandler.CallBase = true;
            eventHandler
            .Setup(f => f.Consume(It.IsAny <IAppEvent <AuctionCreated> >())
                   )
            .Callback((IAppEvent <AuctionCreated> ev) =>
            {
                publishedEvent = ev;
                sem.Release();
            })
            .CallBase();
            services.SetupEventBus(eventHandler.Object);

            var implProv = new Mock <IImplProvider>();

            implProv
            .Setup(f => f.Get <IAuctionRepository>())
            .Returns(services.AuctionRepository);
            RollbackHandlerRegistry.ImplProvider = implProv.Object;
            var testRollbackHandler = new Mock <TestCreateAuctionRollbackHandler>(implProv.Object);

            testRollbackHandler.CallBase = true;
            testRollbackHandler
            .Setup(f => f.Rollback(It.IsAny <IAppEvent <Event> >()))
            .CallBase();
            testRollbackHandler.Object.AfterAction = () => { sem2.Release(); };


            var session = user.UserIdentity.GetAuctionCreateSession();

            command.AuctionCreateSession = session;

            var handlerDepedencies = new CreateAuctionCommandHandlerDepedencies()
            {
                auctionRepository       = services.AuctionRepository,
                auctionSchedulerService = services.SchedulerService,
                eventBusService         = services.EventBus,
                logger = Mock.Of <ILogger <CreateAuctionCommandHandler> >(),
                auctionCreateSessionService = services.GetAuctionCreateSessionService(session),
                auctionImageRepository      = services.AuctionImageRepository,
                categoryBuilder             = new CategoryBuilder(services.CategoryTreeService),
                userRepository = userRepository.Object
            };
            var commandHandler = new TestCreateAuctionCommandHandler(handlerDepedencies, testRollbackHandler.Object);

            commandHandler.Handle(command, CancellationToken.None);
            if (!sem.Wait(TimeSpan.FromSeconds(60)))
            {
                Assert.Fail();
            }
            ;


            var createdAuciton = services.AuctionRepository.FindAuction(publishedEvent.Event.AuctionId);

            if (!sem2.Wait(TimeSpan.FromSeconds(60)))
            {
                Assert.Fail();
            }
            ;
            var auctionAfterRollback = services.AuctionRepository.FindAuction(publishedEvent.Event.AuctionId);

            testRollbackHandler.Verify(f => f.Rollback(It.IsAny <IAppEvent <Event> >()), Times.Once());
            createdAuciton.Should()
            .NotBe(null);
            auctionAfterRollback.Should()
            .Be(null);
        }
예제 #12
0
 public long Post(CreateAuctionCommand command)
 {
     return(this._facade.Create(command));
 }
예제 #13
0
 public async Task <IActionResult> Create(CreateAuctionCommand command)
 {
     return(Ok(await Mediator.Send(command)));
 }