Ejemplo n.º 1
0
        public async Task AddAsync(TicketSaleDto dto)
        {
            var conference = await _conferenceRepository.GetAsync(dto.ConferenceId);

            if (conference is null)
            {
                throw new ConferenceNotFoundException(dto.ConferenceId);
            }

            if (conference.ParticipantsLimit.HasValue)
            {
                var ticketsCount = await _ticketRepository.CountForConferenceAsync(conference.Id);

                if (ticketsCount + dto.Amount > conference.ParticipantsLimit)
                {
                    throw new TooManyTicketsException(conference.Id);
                }
            }

            dto.Id = Guid.NewGuid();
            var ticketSale = new TicketSale
            {
                Id           = dto.Id,
                ConferenceId = dto.ConferenceId,
                From         = dto.From,
                To           = dto.To,
                Amount       = dto.Amount,
                Price        = dto.Price,
                Name         = dto.Name
            };
            await _ticketSaleRepository.AddAsync(ticketSale);

            _logger.LogInformation($"Added a ticket sale conference with ID: '{conference.Id}' ({dto.From} - {dto.To}).");

            if (ticketSale.Amount.HasValue)
            {
                _logger.LogInformation($"Generating {ticketSale.Amount} tickets for conference with ID: '{conference.Id}'...");
                var tickets = new List <Ticket>();
                for (var i = 0; i < ticketSale.Amount; i++)
                {
                    var ticket = _ticketGenerator.Generate(conference.Id, ticketSale.Id, ticketSale.Price);
                    tickets.Add(ticket);
                }

                await _ticketRepository.AddManyAsync(tickets);
            }
        }
Ejemplo n.º 2
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));
        }