Exemple #1
0
        public async Task PurchaseAsync(Guid conferenceId, Guid userId)
        {
            var conference = await _conferenceRepository.GetAsync(conferenceId);

            if (conference is null)
            {
                throw new ConferenceNotFoundException(conferenceId);
            }

            var ticket = await _ticketRepository.GetAsync(conferenceId, userId);

            if (ticket is not null)
            {
                throw new TicketAlreadyPurchasedException(conferenceId, userId);
            }

            var now        = _clock.CurrentDate();
            var ticketSale = await _ticketSaleRepository.GetCurrentForConferenceAsync(conferenceId, now);

            if (ticketSale is null)
            {
                throw new TicketSaleUnavailableException(conferenceId);
            }

            if (ticketSale.Amount.HasValue)
            {
                await PurchaseAvailableAsync(ticketSale, userId, ticketSale.Price);

                return;
            }

            ticket = _generator.Generate(conferenceId, ticketSale.Id, ticketSale.Price);
            ticket.Purchase(userId, _clock.CurrentDate(), ticketSale.Price);
            await _ticketRepository.AddAsync(ticket);

            _logger.LogInformation($"Ticket with ID: '{ticket.Id}' was generated for the conference: " +
                                   $"'{conferenceId}' by user: '******'.");
            await _messageBroker.PublishAsync(new TicketPurchased(ticket.Id, conferenceId, userId));
        }
Exemple #2
0
 public async Task AddAsync(Domain.Models.Ticket ticket)
 {
     await _ticketRepository.AddAsync(ticket);
 }