Example #1
0
        public ReservationsProgram <int?> TryAccept(Reservation reservation)
        {
            return(ReservationsProgram
                   .IsReservationInFuture(reservation)
                   .SelectMany(isInFuture =>
            {
                if (!isInFuture)
                {
                    return ReservationsProgram <int?> .Pure(null);
                }

                return ReservationsProgram
                .ReadReservations(reservation.Date)
                .SelectMany(reservations =>
                {
                    var reservedSeats = reservations.Sum(r => r.Quantity);

                    if (Capacity < reservedSeats + reservation.Quantity)
                    {
                        return ReservationsProgram <int?> .Pure(null);
                    }

                    reservation.IsAccepted = true;

                    return ReservationsProgram
                    .Create(reservation)
                    .Select(x => new int?(x));
                });
            }));
        }
 public static T Interpret <T>(
     this ReservationsProgram <T> program,
     string connectionString)
 {
     return(program.Match(
                pure: x => x,
                free: i => i.Match(
                    isReservationInFuture: t =>
                    t.Item2(IsReservationInFuture(t.Item1))
                    .Interpret(connectionString),
                    readReservations: t =>
                    t.Item2(ReadReservations(t.Item1, connectionString))
                    .Interpret(connectionString),
                    create: t =>
                    t.Item2(Create(t.Item1, connectionString))
                    .Interpret(connectionString))));
 }