public ActionResult Create(RoomAllocation roomallocation)
        {
            if (ModelState.IsValid)
            {
                if (roomallocation.StartTime >= roomallocation.EndTime)
                {
                    GetSelectValue(roomallocation);
                    @ViewData["error"] = "Start Time should be less than End Time";
                    return View(roomallocation);
                }

                var res =
                    db.RoomAllocations.Where(
                        (a =>
                            (a.RoomId == roomallocation.RoomId && a.DayId == roomallocation.DayId &&
                             (a.StartTime < roomallocation.EndTime && roomallocation.StartTime < a.EndTime))))
                        .Include(a => a.Department)
                        .Include(a => a.Course);

                if (res.Any())
                {
                    @ViewBag.roomAllocationTable = res;
                    View();
                }
                else
                {
                    db.RoomAllocations.Add(roomallocation);
                    db.SaveChanges();
                    return RedirectToAction("ViewClassSchedule");
                }
            }

            GetSelectValue(roomallocation);
            return View(roomallocation);
        }
 public ActionResult Edit(RoomAllocation roomallocation)
 {
     if (ModelState.IsValid)
     {
         db.Entry(roomallocation).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Code", roomallocation.DepartmentId);
     ViewBag.CourseId = new SelectList(db.Courses, "Id", "Code", roomallocation.CourseId);
     ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "Name", roomallocation.RoomId);
     ViewBag.DayId = new SelectList(db.Days, "DayId", "Name", roomallocation.DayId);
     return View(roomallocation);
 }
 private void GetSelectValue(RoomAllocation roomallocation)
 {
     ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Code", roomallocation.DepartmentId);
     ViewBag.CourseId = new SelectList(db.Courses, "Id", "Code", roomallocation.CourseId);
     ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "Name", roomallocation.RoomId);
     ViewBag.DayId = new SelectList(db.Days, "DayId", "Name", roomallocation.DayId);
 }
        public ActionResult Create(RoomAllocation roomallocation)
        {
            Room room = db.Rooms.Find(roomallocation.RoomId);
            Course course = db.Courses.Find(roomallocation.CourseId);
            Day day = db.Days.Find(roomallocation.DayId);

            if (ModelState.IsValid)
            {

                int givenfrom, givenEnd;

                try
                {
                    givenfrom = ConvertTimetoInt(roomallocation.StartTime);
                }
                catch (Exception anException)
                {
                    if (TempData["ErrorMessage3"] == null)
                    {
                        TempData["ErrorMessage1"] = anException.Message;
                    }
                    TempData["ErrorMessage4"] = TempData["ErrorMessage3"];
                    ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code", course.DepartmentId);
                    ViewBag.CourseId = new SelectList(db.Courses.Where(c => c.DepartmentId == course.DepartmentId), "CourseId", "Code", roomallocation.CourseId);
                    ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "Name", roomallocation.RoomId);
                    ViewBag.DayId = new SelectList(db.Days, "DayId", "Name", roomallocation.DayId);
                    return View(roomallocation);

                }

                try
                {
                    givenEnd = ConvertTimetoInt(roomallocation.EndTime);
                }
                catch (Exception anException)
                {
                    if (TempData["ErrorMessage3"] == null)
                    {
                        TempData["ErrorMessage2"] = anException.Message;
                    }
                    TempData["ErrorMessage5"] = TempData["ErrorMessage3"];
                    ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code", course.DepartmentId);
                    ViewBag.CourseId = new SelectList(db.Courses.Where(c => c.DepartmentId == course.DepartmentId), "CourseId", "Code", roomallocation.CourseId);
                    ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "Name", roomallocation.RoomId);
                    ViewBag.DayId = new SelectList(db.Days, "DayId", "Name", roomallocation.DayId);
                    return View(roomallocation);
                }

                if (givenEnd < givenfrom)
                {
                    TempData["Message"] = "Class Should Start Before End (24 hours)";
                    ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code", course.DepartmentId);
                    ViewBag.CourseId = new SelectList(db.Courses.Where(c => c.DepartmentId == course.DepartmentId), "CourseId", "Code", roomallocation.CourseId);
                    ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "Name", roomallocation.RoomId);
                    ViewBag.DayId = new SelectList(db.Days, "DayId", "Name", roomallocation.DayId);
                    return View(roomallocation);
                }
                if (givenEnd == givenfrom)
                {
                    TempData["Message"] = "Class Should Start Before End (24 hours)";
                    ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code", course.DepartmentId);
                    ViewBag.CourseId = new SelectList(db.Courses.Where(c => c.DepartmentId == course.DepartmentId), "CourseId", "Code", roomallocation.CourseId);
                    ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "Name", roomallocation.RoomId);
                    ViewBag.DayId = new SelectList(db.Days, "DayId", "Name", roomallocation.DayId);
                    return View(roomallocation);
                }

                if ((givenfrom < 0) || (givenEnd >= (24 * 60)))
                {
                    TempData["Message"] = " 24 hours--format HH:MM";
                    ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code", course.DepartmentId);
                    ViewBag.CourseId = new SelectList(db.Courses.Where(c => c.DepartmentId == course.DepartmentId), "CourseId", "Code", roomallocation.CourseId);
                    ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "Name", roomallocation.RoomId);
                    ViewBag.DayId = new SelectList(db.Days, "DayId", "Name", roomallocation.DayId);
                    return View(roomallocation);
                }

                List<RoomAllocation> overLapList = new List<RoomAllocation>();

                var DayRoomAllocationList =
                db.RoomAllocations.Include(c => c.Course).Include(d => d.Day).Include(r => r.Room).Where(r => r.RoomId == roomallocation.RoomId && r.DayId == roomallocation.DayId)
                    .ToList();

                foreach (var aDayroomAllocation in DayRoomAllocationList)
                {
                    //int OverlapFrom = 0;
                    //int OverlapEnd = 0;
                    //RoomAllocation overlap =new RoomAllocation();

                    int DbFrom = ConvertTimetoInt(aDayroomAllocation.StartTime);
                    int DbEnd = ConvertTimetoInt(aDayroomAllocation.EndTime);

                    if (
                            ((DbFrom <= givenfrom) && (givenfrom < DbEnd))
                            || ((DbFrom < givenEnd) && (givenEnd <= DbEnd))
                            || ((givenfrom <= DbFrom) && (DbEnd <= givenEnd))
                        )
                    {
                        overLapList.Add(aDayroomAllocation);
                    }

                }
                if (overLapList.Count == 0)
                {

                    db.RoomAllocations.Add(roomallocation);
                    db.SaveChanges();
                    TempData["Message"] = "Room : " + room.Name + "Allocated "
                    + " for course : " + course.Code + " From " + roomallocation.StartTime
                    + " to " + roomallocation.EndTime + " on " + day.Name;
                }
                else
                {
                    string message = "Room : " + room.Name + " Overlapped With : ";
                    foreach (var anOverlappedRoom in overLapList)
                    {
                        int dbFrom = ConvertTimetoInt(anOverlappedRoom.StartTime);
                        int dbEnd = ConvertTimetoInt(anOverlappedRoom.EndTime);

                        string overlapStart, overlapEnd;

                        if ((dbFrom <= givenfrom) && (givenfrom < dbEnd))
                            overlapStart = roomallocation.StartTime;
                        else
                            overlapStart = anOverlappedRoom.StartTime;

                        if ((dbFrom < givenEnd) && (givenEnd <= dbEnd))
                            overlapEnd = roomallocation.EndTime;
                        else
                            overlapEnd = anOverlappedRoom.EndTime;
                        message += " Course : " + anOverlappedRoom.Course.Code + " Start Time : "
                            + anOverlappedRoom.StartTime + " End Time : "
                            + anOverlappedRoom.EndTime + " Overlapped from : ";
                        message += overlapStart + " To " + overlapEnd;
                    }

                    TempData["Message"] = message + " on " + day.Name;

                    ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code");
                    ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code", course.DepartmentId);
                    ViewBag.CourseId = new SelectList(db.Courses.Where(c => c.DepartmentId == course.DepartmentId), "CourseId", "Code", roomallocation.CourseId);
                    ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "Name", roomallocation.RoomId);
                    ViewBag.DayId = new SelectList(db.Days, "DayId", "Name", roomallocation.DayId);
                    return View(roomallocation);
                }
                //}

                return RedirectToAction("Create");
            }

            ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Code", roomallocation.DepartmentId);
            ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Code", roomallocation.CourseId);
            ViewBag.RoomId = new SelectList(db.Rooms, "RoomId", "Name", roomallocation.RoomId);
            ViewBag.DayId = new SelectList(db.Days, "DayId", "Name", roomallocation.DayId);
            return View(roomallocation);
        }