Exemplo n.º 1
0
        public async Task <IActionResult> DepositMessage()
        {
            var greetingAsync = new GreetingAsyncEvent("Deposit Hello from the web");
            var greeting      = new GreetingEvent("Deposit Hello from the web");

            await _commandProcessor.DepositPostAsync(greetingAsync);

            _context.Greetings.Add(greeting);
            _context.GreetingsAsync.Add(greetingAsync);
            await _context.SaveChangesAsync();

            _commandProcessor.DepositPost(greeting);
            await _commandProcessor.DepositPostAsync(greetingAsync);

            return(View("Index"));
        }
        public override async Task <AddNewAccountCommand> HandleAsync(AddNewAccountCommand command, CancellationToken cancellationToken = new CancellationToken())
        {
            Guid eventId;

            using (var uow = new AccountContext(_options))
            {
                using (var trans = uow.Database.BeginTransaction())
                {
                    var accountRepository = new AccountRepositoryAsync(new EFUnitOfWork(uow));

                    var account = new Account();
                    account.AccountId      = command.Id;
                    account.Name           = new Name(account, command.Name.FirstName, command.Name.LastName);
                    account.ContactDetails = new ContactDetails(account, command.ContactDetails.Email, command.ContactDetails.TelephoneNumber);
                    account.CardDetails    = new CardDetails(account, command.CardDetails.CardNumber, command.CardDetails.CardSecurityCode);
                    account.Addresses      = command.Addresses;

                    await accountRepository.AddAsync(account);

                    eventId = _commandProcessor.DepositPost(new AccountEvent
                    {
                        Id        = Guid.NewGuid(),
                        AccountId = account.AccountId.ToString(),
                        Addresses = account.Addresses.Select(
                            addr => new AddressEvent
                        {
                            AddressType       = addr.AddressType.ToString(),
                            FistLineOfAddress = addr.FistLineOfAddress,
                            State             = addr.State,
                            ZipCode           = addr.ZipCode
                        }).ToList(),
                        Name = new NameEvent {
                            FirstName = account.Name.FirstName, LastName = account.Name.LastName
                        },
                        CardDetails = new CardDetailsEvent
                        {
                            CardNumber       = account.CardDetails.CardNumber,
                            CardSecurityCode = account.CardDetails.CardSecurityCode
                        },
                        ContactDetails = new ContactDetailsEvent
                        {
                            Email           = account.ContactDetails.Email,
                            TelephoneNumber = account.ContactDetails.TelephoneNumber
                        },
                        Version = account.Version
                    });

                    trans.Commit();
                }
            }

            _commandProcessor.ClearOutbox(eventId);


            return(await base.HandleAsync(command, cancellationToken));
        }
Exemplo n.º 3
0
        public override async Task <BookGuestRoomOnAccount> HandleAsync(BookGuestRoomOnAccount command, CancellationToken cancellationToken = new CancellationToken())
        {
            Guid messageId;

            using (var uow = new BookingContext(_options))
            {
                using (var trans = uow.Database.BeginTransaction())
                {
                    var repository = new RoomBookingRepositoryAsync(new EFUnitOfWork(uow));

                    var roomBooking = new RoomBooking(
                        command.BookingId,
                        command.DateOfFirstNight,
                        command.NumberOfNights,
                        command.NumberOfGuests,
                        command.Type,
                        command.Price,
                        command.AccountId);

                    await repository.AddAsync(roomBooking, cancellationToken);

                    messageId = _messagePublisher.DepositPost(new GuestRoomBookingMade
                    {
                        BookingId        = roomBooking.RoomBookingId.ToString(),
                        DateOfFirstNight = roomBooking.DateOfFirstNight,
                        NumberOfGuests   = roomBooking.NumberOfGuests,
                        NumberOfNights   = roomBooking.NumberOfNights,
                        Type             = roomBooking.RoomType,
                        Price            = roomBooking.Price,
                        AccountId        = roomBooking.AccountId,
                    });

                    trans.Commit();
                }
            }

            _messagePublisher.ClearOutbox(messageId);

            return(await base.HandleAsync(command, cancellationToken));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> SaveAndRollbackMessage()
        {
            var transaction = await _context.Database.BeginTransactionAsync();

            // try
            // {
            var greetingAsync = new GreetingAsyncEvent("Hello from the web - 1");
            var greeting      = new GreetingEvent("Hello from the web - 1");

            _context.Greetings.Add(greeting);
            _context.GreetingsAsync.Add(greetingAsync);

            await _context.SaveChangesAsync();

            _commandProcessor.Post(greeting);
            await _commandProcessor.PostAsync(greetingAsync);

            //throw new Exception("Something went wrong");
            // }
            // catch (Exception e)
            // {
            await transaction.RollbackAsync();

            // }

            var greetingAsync2 = new GreetingAsyncEvent("Hello from the web - 2");
            var greeting2      = new GreetingEvent("Hello from the web - 2");

            _context.Greetings.Add(greeting2);
            _context.GreetingsAsync.Add(greetingAsync2);

            _commandProcessor.DepositPost(greeting2);

            await _context.SaveChangesAsync();

            await _commandProcessor.DepositPostAsync(greetingAsync2);

            return(View("Index"));
        }
Exemplo n.º 5
0
 public Guid DepositPost <T>(T request)
     where T : class, IRequest
 {
     return(_commandProcessor.DepositPost(request));
 }