Пример #1
0
        public async Task <IActionResult> CreateAsync(CreateReservationsRequest request)
        {
            // Validate that the email is not already awaiting confirmation
            if (ReservationService.DoesEmailHavePendingConfirmation(request.Email))
            {
                return(this.MakeFailure("Email already has pending confirmations.", StatusCodes.Status400BadRequest));
            }

            // if ID is present && invalid
            if (!String.IsNullOrEmpty(request.EndorsementID) && !DataContext.Endorsements.Any((Endorsement e) => e.ID == request.EndorsementID))
            {
                return(this.MakeFailure("Invalid Endorsement ID.", StatusCodes.Status400BadRequest));
            }


            foreach (CreateReservationsRequest.Slot slot in request.Slots)
            {
                // Validate that the slot is valid
                if (!this.SlotIsValid(slot.Year, slot.MonthIndex, slot.DayIndex, slot.SlotIndex))
                {
                    return(this.MakeFailure("Date or slot is not valid.", StatusCodes.Status400BadRequest));
                }

                // TODO: Validate that slot is in the future

                // TODO: Validate that slot is not already reserved by the email
            }

            // TODO: Validate the country & district

            Confirmation confirmation = new Confirmation()
            {
                ID = ReservationService.GenerateConfirmationID(), Email = request.Email, Reservations = new List <Reservation>()
            };

            foreach (CreateReservationsRequest.Slot slot in request.Slots)
            {
                Reservation reservation = new Reservation()
                {
                    ID = ReservationService.GenerateReservationID(), Email = request.Email, IsConfirmed = false, EndorsementID = request.EndorsementID, Country = request.Country, District = request.District, Year = slot.Year, MonthIndex = slot.MonthIndex, DayIndex = slot.DayIndex, SlotIndex = slot.SlotIndex
                };
                ReservationService.AddReservation(reservation);
                confirmation.Reservations.Add(reservation);
            }
            ReservationService.AddConfirmation(confirmation);

            await ReservationService.SendConfirmationCode(request.Email, confirmation.ID, confirmation.Reservations);

            return(this.MakeSuccess());
        }