public ActionResult Delete(Team team)
        {
            // Make sure the user is logged in and that they have permission
            if (!IsUserLoggedIn)
            {
                return(RedirectToLogin());
            }
            if (!UserHasPermission(PermissionName.Team))
            {
                return(RedirectToPermissionDenied());
            }

            try
            {
                int rowsDeleted = TeamProcessor.DeleteTeam(team.TeamId);
                if (rowsDeleted <= 0)
                {
                    throw new DataException("Unable to Delete Team.");
                }

                return(RedirectToAction("Details", new RouteValueDictionary(
                                            new { controller = "ProjectOffering", action = "Details", Id = team.ProjectOfferingId })));
            }
            catch (Exception e)
            {
                // Show any errors
                ModelState.AddModelError("", e.Message);
            }

            //Find Team
            var teamModel       = TeamProcessor.GetTeamForTeamId(team.TeamId);
            var projectOffering = ProjectOfferingProcessor.SelectProjectOfferingForProjectOfferingId(teamModel.ProjectofferingId);

            return(View(new Team(teamModel, projectOffering)));
        }
        public ActionResult AddTeamMember(Enrollment _Enrollment)
        {
            // Make sure the user is logged in and that they have permission
            if (!IsUserLoggedIn)
            {
                return(RedirectToLogin());
            }
            if (!UserHasPermission(PermissionName.Team))
            {
                return(RedirectToPermissionDenied());
            }

            if (_Enrollment.TeamId <= 0)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            try
            {
                // Ensure a User has been selected
                //if( _Enrollment.UserId <= 0 )
                //   throw new DataException( "No Enrolled User Selected." );
                var enrollmentData = EnrollmentProcessor.SelectEnrollmentForEnrollmentId(_Enrollment.EnrollmentId);

                //Check if Student is already a member of this team
                var rowsFound = EnrollmentProcessor.SelectEnrollmentCountForTeamIdAndUserId(_Enrollment.TeamId, enrollmentData.UserId);
                if (rowsFound > 0)
                {
                    throw new DataException($"User is already a member of this Team.");
                }

                // Attempt to update enrollment with TeamId
                EnrollmentProcessor.UpdateEnrollmentWithTeamId(enrollmentData.EnrollmentId, _Enrollment.TeamId);

                return(Redirect(Request.UrlReferrer.ToString( )));
            }
            catch (Exception _Ex)
            {
                // Show Model Errors reload data and return to view.
                ModelState.AddModelError("", $"Unable to save changes due to Error: {_Ex.Message}");
            }

            // Get the team data
            var teamModel = TeamProcessor.GetTeamForTeamId(_Enrollment.TeamId);

            if (teamModel == null)
            {
                return(RedirectToUnitOfferingIndex( ));
            }

            var projectOffering = ProjectOfferingProcessor.SelectProjectOfferingForProjectOfferingId(teamModel.ProjectofferingId);

            var team = new Team(teamModel, projectOffering);

            ViewBag.EnrollmentId = new SelectList(team.GetAvailableEnrollments(team.ProjectOfferingId), "EnrollmentId", "Username", null);

            return(View(team));
        }
        // Edit
        public ActionResult Edit(int?id)
        {
            // Make sure the user is logged in and that they have permission
            if (!IsUserLoggedIn)
            {
                return(RedirectToLogin());
            }
            if (!UserHasPermission(PermissionName.Team))
            {
                return(RedirectToPermissionDenied());
            }

            if (id == null)
            {
                //If no id supplied return error code
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            int teamId = ( int )id;

            //Attempt to Get team for Id
            var teamModel = TeamProcessor.GetTeamForTeamId(teamId);

            if (teamModel == null)
            {
                return(RedirectToUnitOfferingIndex( ));
            }


            var projectOffering = ProjectOfferingProcessor.SelectProjectOfferingForProjectOfferingId(teamModel.ProjectofferingId);
            //var project = ProjectProcessor.GetProject( teamModel.UnitOfferingId );


            var team = new Team(teamModel, projectOffering);

            ViewBag.SupervisorId = new SelectList(team.GetAvailableSupervisors( ), "UserId", "Username", null);

            return(View(team));
        }
        // Details
        public ActionResult Details(int?id)
        {
            // Make sure the user is logged in and that they have permission
            if (!IsUserLoggedIn)
            {
                return(RedirectToLogin());
            }
            if (!UserHasPermission(PermissionName.Team))
            {
                return(RedirectToPermissionDenied());
            }

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            try
            {
                int teamId = ( int )id;
                // Get the team data
                var teamModel = TeamProcessor.GetTeamForTeamId(teamId);
                if (teamModel == null)
                {
                    return(RedirectToUnitOfferingIndex( ));
                }

                var projectOffering = ProjectOfferingProcessor.SelectProjectOfferingForProjectOfferingId(teamModel.ProjectofferingId);

                var team = new Team(teamModel, projectOffering);
                ViewBag.EnrollmentId = new SelectList(team.GetAvailableEnrollments(team.ProjectOfferingId), "EnrollmentId", "Username", null);

                return(View(team));
            }
            catch (Exception e)
            {
                return(RedirectToIndex(e));
            }
        }
        public ActionResult Delete(int?id)
        {
            // Make sure the user is logged in and that they have permission
            if (!IsUserLoggedIn)
            {
                return(RedirectToLogin());
            }
            if (!UserHasPermission(PermissionName.Team))
            {
                return(RedirectToPermissionDenied());
            }

            if (id == null)
            {
                // If no id provided show BadRequest Error
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            int teamId = ( int )id;

            // Get the team data
            var teamModel = TeamProcessor.GetTeamForTeamId(teamId);

            if (teamModel == null)
            {
                return(RedirectToUnitOfferingIndex( ));
            }

            var projectOffering = ProjectOfferingProcessor.SelectProjectOfferingForProjectOfferingId(teamModel.ProjectofferingId);

            //var project = ProjectProcessor.GetProject( teamModel.UnitOfferingId );

            //var enrollments = EnrollmentProcessor.LoadEnrollmentsForTeam( id );

            return(View(new Team(teamModel, projectOffering)));
        }