Example #1
0
        public static ClassBookingDetail ToBookingViewModel(this GymClass gymClass, bool isDisplayDateFormat) {
            ClassBookingDetail cd = new ClassBookingDetail();
            if (gymClass == null) {
                return cd;
            }
            cd.Description = gymClass.Description;
            cd.GymClassId = gymClass.GymClassId;
            cd.MaxCapacity = gymClass.MaxCapacity;
            cd.MaxWaitList = gymClass.MaxWaitList;
            cd.GymClassTypeId = gymClass.GymClassTypeId;
            cd.GymClassTypeName = gymClass.Type.Name;
            cd.ClassDateTime = gymClass.ClassDateTime;

            if (isDisplayDateFormat) {
                cd.ClassDate = gymClass.ClassDateTime.ToString("dddd dd MMM");
                cd.ClassTime = gymClass.ClassDateTime.ToString("HH:mm");
            } else {
                cd.ClassTime = gymClass.ClassDateTime.ToString("HH:mm");
                cd.ClassDate = gymClass.ClassDateTime.ToString("dd/MM/yyyy");
            }
            cd.IsEditable = gymClass.ClassDateTime < DateTime.Now;
            cd.ClassBookings = new List<GymClassBooking>();
            foreach (var b in gymClass.Bookings.Where(bk => bk.Waiting == false).ToList()) {
                cd.ClassBookings.Add(b);
            }
            cd.ClassWaiting = new List<GymClassBooking>();
            foreach (var b in gymClass.Bookings.Where(bk => bk.Waiting == true).ToList()) {
                cd.ClassWaiting.Add(b);
            }
            cd.nBookings = gymClass.Bookings.Count();
            return cd;
        }
        public static GymClass ToModel(this ClassBookingDetail classDetail)
        {
            GymClass gymClass = new GymClass();

            gymClass.Description    = classDetail.Description;
            gymClass.GymClassId     = classDetail.GymClassId;
            gymClass.GymClassTypeId = classDetail.GymClassTypeId;
            gymClass.MaxCapacity    = classDetail.MaxCapacity;
            gymClass.MaxWaitList    = classDetail.MaxWaitList;
            if (classDetail.ClassDateTime != null && classDetail.ClassDateTime > DateTime.Now)
            {
                gymClass.ClassDateTime = classDetail.ClassDateTime;
            }
            else if (!String.IsNullOrEmpty(classDetail.ClassDate) && !String.IsNullOrEmpty(classDetail.ClassTime))
            {
                DateTime d;

                bool dateOk = DateTime.TryParseExact(classDetail.ClassDate + " " + classDetail.ClassTime, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);
                if (dateOk)
                {
                    gymClass.ClassDateTime = d;
                }
                else
                {
                    gymClass.ClassDateTime = DateTime.Now;
                }
            }
            return(gymClass);
        }