internal static async Task <Booking> GetNewlyCreatedBookingForTest(Event evnt, BookingRepository repository)
        {
            var candidate = BookingCandidateRepositoryTest.GetBookingCandidateForTest();
            var result    = await repository.CreateFromCandidateAsync(evnt, candidate, 1);

            return(await repository.FindByReferenceAsync(result.Reference));
        }
Пример #2
0
        public async Task <IHttpActionResult> ToBooking(string c)
        {
            try
            {
                Event evnt = await _eventRepository.GetActiveAsync();

                if (null == evnt)
                {
                    return(NotFound());
                }
                if (evnt.IsLocked)
                {
                    return(BadRequest("The event is locked and can no longer accept bookings."));
                }

                Guid             candidateId = ParseGuid(c);
                BookingCandidate candidate;
                int placeInQueue;

                using (var db = DbUtil.Open())
                {
                    candidate = await _candidateRepository.FindByIdAsync(db, candidateId);

                    if (null == candidate)
                    {
                        return(NotFound());
                    }

                    placeInQueue = await _candidateRepository.FindPlaceInQueueAsync(db, candidateId);

                    if (0 == placeInQueue)
                    {
                        _log.Warn($"An attempt was made to create a booking for candidate ID = {candidateId} with no place in the queue.");
                        return(NotFound());
                    }
                }

                BookingResult result = await _bookingRepository.CreateFromCandidateAsync(evnt, candidate, placeInQueue);

                _log.Info("Created booking {0} from candidate {1} at position {2}.", result.Reference, candidate.Id, placeInQueue);

                await SendBookingCreatedMailAsync(evnt, candidate, result);

                return(Ok(result));
            }
            catch (BookingException)
            {
                // An attempt was made to create a second booking from the same candidate
                return(Conflict());
            }
            catch (FormatException)
            {
                return(BadRequest());
            }
            catch (Exception ex)
            {
                _log.Error(ex, "An unexpected exception occurred while creating a booking.");
                throw;
            }
        }