Ejemplo n.º 1
0
        private ActionResult BookClassMember(int classId, int memberId)
        {
            GymClass cl = db.GymClass.Find(classId);

            if (cl == null)
            {
                ViewBag.Error = "Class not found";
                return(View("BookingError"));
            }
            int nMemberClassBookings = db.MemberClassBookings.Where(cb => cb.GymClassId == classId && cb.GymMemberId == memberId).Count();

            if (nMemberClassBookings > 0)
            {
                ViewBag.Error = "You are already booked on this class!";
                return(View("BookingError"));
            }
            int nAllBooked = db.MemberClassBookings.Where(bk => bk.GymClassId == cl.GymClassId).Count();
            int nBooked    = db.MemberClassBookings.Where(bk => bk.GymClassId == cl.GymClassId && !bk.Waiting).Count();

            if (nAllBooked < cl.MaxCapacity + cl.MaxWaitList)
            {
                GymClassBooking booking = new GymClassBooking();
                booking.GymClassId      = classId;
                booking.GymMemberId     = memberId;
                booking.Waiting         = nBooked >= cl.MaxCapacity;
                db.Entry(booking).State = EntityState.Added;
                db.SaveChanges();
            }
            else
            {
                ViewBag.Error = "Class is fully booked!";
                return(View("BookingError"));
            }
            return(RedirectToAction("Index", new { memberId = memberId }));
        }
Ejemplo n.º 2
0
        public static bool CancelMemberBooking(int classId, int memberId, GymContext db)
        {
            GymClass cl = db.GymClass.Find(classId);

            if (cl == null)
            {
                return(false);
            }
            var booking = db.MemberClassBookings.Where(bk => bk.GymClassId == cl.GymClassId && bk.GymMemberId == memberId).FirstOrDefault();

            if (booking == null)
            {
                return(false);
            }
            db.Entry(booking).State = EntityState.Deleted;
            var nBookings = db.MemberClassBookings.Where(cls => cls.GymClassId == classId && !cls.Waiting).Count();
            // Promote the next one from the waiting list
            GymClassBooking promotedBooking = null;

            if (!booking.Waiting && nBookings <= cl.MaxCapacity)
            {
                promotedBooking = db.MemberClassBookings.Where(bk => bk.GymClassId == cl.GymClassId && bk.Waiting == true).FirstOrDefault();
            }
            if (promotedBooking != null)
            {
                promotedBooking.Waiting         = false;
                db.Entry(promotedBooking).State = EntityState.Modified;
            }
            db.SaveChanges();
            return(true);
        }