public async Task <ConferenceDto> GetAsync(Guid id)
        {
            var conference = await _conferenceRepository.GetAsync(id);

            return(conference is null
                ? null
                : Map(conference));
        }
Example #2
0
        public async Task <ConferenceDetailsDto> GetAsync(Guid id)
        {
            var conference = await _conferenceRepository.GetAsync(id);

            if (conference is null)
            {
                return(null);
            }
            var dto = Map <ConferenceDetailsDto>(conference);

            dto.Description = conference.Description;
            return(dto);
        }
Example #3
0
        public async Task <IEnumerable <TicketSaleInfoDto> > GetAllAsync(Guid conferenceId)
        {
            var conference = await _conferenceRepository.GetAsync(conferenceId);

            if (conference is null)
            {
                return(null);
            }

            var ticketSales = await _ticketSaleRepository.BrowseForConferenceAsync(conferenceId);

            return(ticketSales.Select(x => Map(x, conference)));
        }
Example #4
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);
            }
        }
Example #5
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));
        }
        public async Task <ConferenceDetailsDto> GetAsync(Guid id)
        {
            var conference = await _conferenceRepository.GetAsync(id);

            return(conference is not null?MapDetails(conference) : null);
        }