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);
                }
            }

            var ticketSale = new TicketSale(dto.ConferenceId, dto.Name, dto.Price, dto.Amount, dto.From, dto.To);

            _ticketSaleRepository.Add(ticketSale);
            await _ticketSaleRepository.UnitOfWork.SaveChangesAsync();

            if (ticketSale.Amount.HasValue)
            {
                var tickets = new List <Ticket>();
                for (var i = 0; i < ticketSale.Amount; i++)
                {
                    var ticket = _ticketsFactory.Generate(conference.Id, ticketSale.Price, ticketSale);
                    tickets.Add(ticket);
                }

                _ticketRepository.AddMany(tickets);
                await _ticketRepository.UnitOfWork.SaveChangesAsync();
            }
        }
示例#2
0
        public async Task PurchaseAsync(Guid conferenceId, Guid userId)
        {
            var conference = await _conferencesRepository.GetAsync(conferenceId);

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

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

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

            var utcNow     = _dateTimeService.GetDateTimeUtcNow();
            var ticketSale = await _ticketSaleRepository.GetCurrentForConferenceAsync(conferenceId, utcNow);

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

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

                return;
            }

            ticket = _ticketsFactory.Generate(conferenceId, ticketSale.Price, ticketSale);
            ticket.Purchase(userId, utcNow, ticketSale.Price);
            _ticketsRepository.Add(ticket);
            await _ticketsRepository.UnitOfWork.SaveChangesAsync();
        }