public IHttpActionResult Post(ReservationRendition rendition)
        {
            DateTimeOffset requestedDate;
            if (!DateTimeOffset.TryParse(rendition.Date, out requestedDate))
                return this.BadRequest("Invalid date.");

            var min = requestedDate.Date;
            var max = requestedDate.Date.AddDays(1);

            using (var ctx = new ReservationsContext())
            {
                var reservedSeats = (from r in ctx.Reservations
                                     where min <= r.Date && r.Date < max
                                     select r.Quantity)
                                    .DefaultIfEmpty(0)
                                    .Sum();
                if (rendition.Quantity + reservedSeats > capacity)
                    return this.StatusCode(HttpStatusCode.Forbidden);

                ctx.Reservations.Add(new Reservation
                {
                    Date = requestedDate,
                    Name = rendition.Name,
                    Email = rendition.Email,
                    Quantity = rendition.Quantity
                });
                ctx.SaveChanges();
            }

            return this.Ok();
        }
Ejemplo n.º 2
0
 public void AddReservation(Reservation reservation)
 {
     using (var ctx = new ReservationsContext())
     {
         ctx.Reservations.Add(reservation);
         ctx.SaveChanges();
     }
 }
Ejemplo n.º 3
0
        public int GetTotalReservations(DateTime min, DateTime max)
        {
            using (var ctx = new ReservationsContext())
            {
                var reservedSeats = (from r in ctx.Reservations
                                     where min <= r.Date && r.Date < max
                                     select r.Quantity)
                                    .DefaultIfEmpty(0)
                                    .Sum();

                return(reservedSeats);
            }
        }
        public IHttpActionResult Post(ReservationRendition rendition)
        {
            DateTimeOffset requestedDate;

            if (!DateTimeOffset.TryParse(rendition.Date, out requestedDate))
            {
                return(this.BadRequest("Invalid date."));
            }

            var min = requestedDate.Date;
            var max = requestedDate.Date.AddDays(1);

            using (var ctx = new ReservationsContext())
            {
                var reservedSeats = (from r in ctx.Reservations
                                     where min <= r.Date && r.Date < max
                                     select r.Quantity)
                                    .DefaultIfEmpty(0)
                                    .Sum();
                if (rendition.Quantity + reservedSeats > capacity)
                {
                    return(this.StatusCode(HttpStatusCode.Forbidden));
                }

                ctx.Reservations.Add(new Reservation
                {
                    Date     = requestedDate,
                    Name     = rendition.Name,
                    Email    = rendition.Email,
                    Quantity = rendition.Quantity
                });
                ctx.SaveChanges();
            }

            return(this.Ok());
        }