public ActionResult UnallocateClassRoom(AllocateClassRoom allocateClassRoom)
        {
            var unallocateClassRoom = unallocateClassRoomManager.UnallocateClassRoom(allocateClassRoom);
            ViewBag.Message = unallocateClassRoom;
            return View();

        }
        public int SaveAllocatedRoom(AllocateClassRoom allocateClassRoom)
        {

            connection.ConnectionString = connectionString;

            string query = "INSERT INTO AllocateClass(DepartmentId,CourseId,RoomId,Day,TimeStart,TimeEnd) VALUES(@DepartmentId,@CourseId,@RoomId,@Day,@TimeStart,@TimeEnd)";

            SqlCommand command = new SqlCommand(query, connection);
            command.Parameters.Clear();
            command.Parameters.Add("DepartmentId", SqlDbType.Int);
            command.Parameters["DepartmentId"].Value = allocateClassRoom.DepartmentId;

            command.Parameters.Add("CourseId", SqlDbType.Int);
            command.Parameters["CourseId"].Value = allocateClassRoom.CourseId;
            command.Parameters.Add("RoomId", SqlDbType.Int);
            command.Parameters["RoomId"].Value = allocateClassRoom.RoomId;
            command.Parameters.Add("Day", SqlDbType.VarChar);
            command.Parameters["Day"].Value = allocateClassRoom.Day;
            command.Parameters.Add("TimeStart", SqlDbType.DateTime);
            command.Parameters["TimeStart"].Value = allocateClassRoom.TimeStart;
            command.Parameters.Add("TimeEnd", SqlDbType.DateTime);
            command.Parameters["TimeEnd"].Value = allocateClassRoom.TimeEnd;
            


            connection.Open();
            int rowAffected = command.ExecuteNonQuery();
            connection.Close();
            return rowAffected;
        }
        public string UnallocateClassRoom(AllocateClassRoom allocateClassRoom)
        {
            if (unallocateClassRoomGateway.UnallocateClassRoom(allocateClassRoom) > 0)
            {
                return " Unallocation successfully";

            }
            else
            {
                return "Unallocation fail!!!";
            }
        }
        public bool CheckTimeCollission(AllocateClassRoom allocateClassRoom)
        {
            connection.ConnectionString = connectionString;
            string query = "SELECT TimeStart,TimeEnd FROM AllocateClass WHERE Day=@Day AND RoomId=@RoomId";
            SqlCommand command = new SqlCommand();
            command.CommandText = query;
            command.Connection = connection;
            command.Parameters.Add("Day", SqlDbType.VarChar);
            command.Parameters["Day"].Value = allocateClassRoom.Day;
            command.Parameters.Add("RoomId", SqlDbType.Int);
            command.Parameters["RoomId"].Value = allocateClassRoom.RoomId;

            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            bool IstimeValid1 = false;
            // check if the query return value 
            if (reader.HasRows)
            {
                while (reader.Read())
                {

                    DateTime start = (DateTime)reader["TimeStart"];
                    DateTime end = (DateTime)reader["TimeEnd"];
                    TimeSpan ts = new TimeSpan(0, 0, 5);
                    //offsetting 5 seconds
                    DateTime start2 = DateTime.Now;
                    DateTime end2 = DateTime.Now;
                    start2 = start + ts;
                    end2 = end - ts;
                    IstimeValid1 = TimeChecker(allocateClassRoom.TimeStart, allocateClassRoom.TimeEnd, start2, end2);
                    //IstimeValid2 = TimeChecker(, start, end);
                }

                reader.Close();
                connection.Close();
                if (IstimeValid1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                reader.Close();
                connection.Close();
                return true;
            }
            
        }
 public string SaveClassAllocation(AllocateClassRoom allocateClassRoom)
 {
     if (allocateClassRoomGateway.CheckTimeCollission(allocateClassRoom))
     {
         if (allocateClassRoomGateway.SaveAllocatedRoom(allocateClassRoom) > 0)
         {
             return "Class Room Allocated";
         }
         else
         {
             return " Class room is not allocated";
         }
     }
 else{
         return "Time Collision";
     }
 }
        public int UnallocateClassRoom(AllocateClassRoom allocateClassRoom)
        {
            connection.ConnectionString = connectionString;



            string query = "Update AllocateClass Set RoomId=Null, Day=Null, TimeStart=Null, TimeEnd=Null";

            SqlCommand command = new SqlCommand(query, connection);

            connection.Open();
            int rowAffected = command.ExecuteNonQuery();
            connection.Close();



            return rowAffected;
        }
        public ActionResult AllocateClassRoom(AllocateClassRoom allocateClassRoom)
        {
            ViewBag.Departments = GetDepartmentForSelectList();
            ViewBag.Rooms = GetRoomsForSelectList();
            ViewBag.Days = GetDaysForSelectList();
            //data manipulation
            DateTime start = new DateTime(2016, 1, 1, allocateClassRoom.TimeStart.Hour, allocateClassRoom.TimeStart.Minute, allocateClassRoom.TimeStart.Second);
            DateTime end = new DateTime(2016, 1, 1, allocateClassRoom.TimeEnd.Hour, allocateClassRoom.TimeEnd.Minute, allocateClassRoom.TimeEnd.Second);
            allocateClassRoom.TimeStart = start;
            allocateClassRoom.TimeEnd = end;
            ViewBag.TimeClash=allocateClassRoomManager.SaveClassAllocation(allocateClassRoom);

            return View();
        }