Ejemplo n.º 1
0
        public ActionResult DeleteStudent(int id)
        {
            using (EPortfolioDB database = new EPortfolioDB())
            {
                // Find the student specified by formModel
                Student student = database.Students
                                  .Where(s => s.StudentId == id)
                                  .Include(s => s.StudentSkillSets)
                                  .FirstOrDefault();
                if (student == null)
                {
                    return(NotFound());
                }

                // cascade delete the students skillsets
                database.StudentSkillSets.RemoveRange(student.StudentSkillSets);

                // check authorized to perform deletion
                Session session = AuthService.ExtractSession(HttpContext);
                if (session.MetaData["UserRole"] != "Lecturer" && // any lecturer
                    session.EmailAddr != student.EmailAddr)       // this student
                {
                    return(Unauthorized());
                }

                // remove the student from db
                database.Students.Remove(student);
                database.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 2
0
        public ActionResult UpdateStudent(
            int id, [FromBody] StudentUpdateFormModel formModel)
        {
            // check if contents of form model is valid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            using (EPortfolioDB database = new EPortfolioDB())
            {
                // Find the student specified by formModel
                Student student = database.Students
                                  .Where(s => s.StudentId == id)
                                  .FirstOrDefault();
                if (student == null)
                {
                    return(NotFound());
                }

                // Check authorized to perform update
                Session session = AuthService.ExtractSession(HttpContext);
                if (session.MetaData["UserRole"] != "Lecturer" && // any lecturer
                    session.EmailAddr != student.EmailAddr)       // this student
                {
                    return(Unauthorized());
                }

                // perform Update using data in form model
                formModel.Apply(student);
                database.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 3
0
        public void TestDeleteModel()
        {
            int projectId = -1; // -1 -> null value

            using (EPortfolioDB database = new EPortfolioDB())
            {
                // Perform insertion of the test project
                Project project = ProjectTest.GetSampleProject();
                database.Projects.Add(project);
                database.SaveChanges();
                projectId = project.ProjectId;

                // delete the test project
                database.Projects.Remove(project);
                database.SaveChanges();
            }

            // Check for presence of record in the database
            using (EPortfolioDB database = new EPortfolioDB())
            {
                int nMatches = database.Projects
                               .Where(p => p.ProjectId == projectId).Count();
                Assert.Equal(0, nMatches);
            }
        }
Ejemplo n.º 4
0
        public ActionResult GetStudent(int id)
        {
            // Retrieve the Student for id
            Student student = null;

            using (EPortfolioDB database = new EPortfolioDB())
            {
                student = database.Students
                          .Where(s => s.StudentId == id)
                          .FirstOrDefault();
            }

            // check if student has been found for targetId
            if (student == null)
            {
                return(NotFound());
            }

            // Check authorized to perform view student
            Session session = AuthService.ExtractSession(HttpContext);

            if (session.MetaData["UserRole"] != "Lecturer" && // any lecturer
                session.EmailAddr != student.EmailAddr)       // this student
            {
                return(Unauthorized());
            }

            return(Json(student));
        }
Ejemplo n.º 5
0
        public ActionResult DeleteProject(int id)
        {
            using (EPortfolioDB database = new EPortfolioDB())
            {
                // Find the project specified by formModel
                Project project = database.Projects
                                  .Where(s => s.ProjectId == id)
                                  .Include(s => s.ProjectMembers)
                                  .FirstOrDefault();
                if (project == null)
                {
                    return(NotFound());
                }

                // cascade delete any ProjectMember assignments
                IQueryable <ProjectMember> assignments = database.ProjectMembers
                                                         .Where(s => s.ProjectId == id);
                database.ProjectMembers.RemoveRange(assignments);

                // remove the Project from db
                database.Projects.Remove(project);
                database.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 6
0
        public ActionResult GetLecturers([FromQuery] int?student,
                                         [FromQuery] int?names)
        {
            using (EPortfolioDB db = new EPortfolioDB())
            {
                IQueryable <Lecturer> matchingLecturers = db.Lecturers;

                if (student != null)
                {
                    matchingLecturers = matchingLecturers
                                        .Include(l => l.Students)
                                        .Where(l => l.Students.Any(ll => ll.StudentId == student));
                }

                // construct response with matching lecturers
                if (names != null && names.Value == 1) // id + names
                {
                    List <Object> results = matchingLecturers
                                            .Select(l => new
                    {
                        Id   = l.LecturerId,
                        Name = l.Name
                    } as Object).ToList();

                    return(Json(results));
                }
                else // only ids
                {
                    List <int> results = matchingLecturers
                                         .Select(l => l.LecturerId).ToList();
                    return(Json(results));
                }
            }
        }
Ejemplo n.º 7
0
        public ActionResult GetStudentPortfolio(int id)
        {
            // Retrieve the Student for id
            Student student = null;

            using (EPortfolioDB database = new EPortfolioDB())
            {
                student = database.Students
                          .Where(s => s.StudentId == id)
                          .FirstOrDefault();
            }

            // check if student has been found for targetId
            if (student == null)
            {
                return(NotFound());
            }

            // extract portfolio information from student
            Object portfolio = new
            {
                StudentId    = student.StudentId,
                Name         = student.Name,
                Course       = student.Course,
                Photo        = student.Photo,
                Description  = student.Description,
                Achievement  = student.Achievement,
                ExternalLink = student.ExternalLink,
                EmailAddr    = student.EmailAddr,
                MentorId     = student.MentorId
            };

            return(Json(portfolio));
        }
Ejemplo n.º 8
0
        public ActionResult UpdateProject(
            int id, [FromBody] ProjectFormModel formModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            using (EPortfolioDB database = new EPortfolioDB())
            {
                // check if project name does not conflict with existing project
                if (database.Projects
                    .Where(s => s.Title == formModel.Title)
                    .Count() >= 1)
                {
                    return(ProjectTitleConflict);
                }

                // Find the project specified by formModel
                Project project = database.Projects
                                  .Where(s => s.ProjectId == id)
                                  .Single();

                // perform Update using data in form model
                formModel.Apply(project);
                database.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 9
0
        public ActionResult CreateStudent(
            [FromBody] StudentCreateFormModel formModel)
        {
            // check if contents of form model is valid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // check if existing user already has email address
            if (AuthService.FindUser(formModel.EmailAddr) != null)
            {
                return(EmailAddrConflict);
            }

            // write the given student to database
            int studentId = -1;

            using (EPortfolioDB database = new EPortfolioDB())
            {
                // create student with form model values
                Student student = formModel.Create();

                // add new student to database
                database.Students.Add(student);
                database.SaveChanges();
                studentId = student.StudentId;
            }

            // respond with success message with inserted student id
            Object response = new { Id = studentId };

            return(Json(response));
        }
Ejemplo n.º 10
0
        public ActionResult DeleteLecturer(int id)
        {
            using (EPortfolioDB database = new EPortfolioDB())
            {
                // Find the lecturer specified by formModel
                Lecturer lecturer = database.Lecturers
                                    .Where(l => l.LecturerId == id)
                                    .Single();

                if (lecturer == null)
                {
                    return(NotFound());
                }

                Session session = AuthService.ExtractSession(HttpContext);
                if (session.MetaData["UserRole"] != "Lecturer" && // any lecturer
                    session.EmailAddr != lecturer.EmailAddr)      // this student
                {
                    return(Unauthorized());
                }


                database.Lecturers.Remove(lecturer);
                database.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 11
0
        public ActionResult Query([FromQuery] string name, [FromQuery] int?limit, int?student)
        {
            // obtain the projects that match the query
            List <int> matchIds = null;

            using (EPortfolioDB database = new EPortfolioDB())
            {
                IQueryable <Project> matchingProjects = database.Projects;
                // apply filters (if any) in url parameters
                if (!string.IsNullOrWhiteSpace(name))
                {
                    matchingProjects = matchingProjects
                                       .Where(s => s.Title == name);
                }
                if (limit != null && limit.Value >= 0)
                {
                    matchingProjects = matchingProjects
                                       .Take(limit.Value);
                }
                if (student != null)
                {
                    // filter by project id
                    matchingProjects = matchingProjects
                                       .Include(s => s.ProjectMembers)
                                       .Where(s => s.ProjectMembers
                                              .Any(pm => pm.StudentId == student));
                }

                // convert matching project to there corresponding ids
                matchIds = matchingProjects.Select(s => s.ProjectId).ToList();
            }

            return(Json(matchIds));
        }
Ejemplo n.º 12
0
        public ActionResult ChangeLecturerPassword(
            int id, [FromBody] LecturerPasswordFormModel formModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            using (EPortfolioDB database = new EPortfolioDB())
            {
                // Find the lecturer specified by formModel
                Lecturer lecturer = database.Lecturers
                                    .Where(s => s.LecturerId == id)
                                    .Single();
                if (lecturer == null)
                {
                    return(NotFound());
                }


                Session session = AuthService.ExtractSession(HttpContext);
                if (session.MetaData["UserRole"] != "Lecturer" && // any lecturer
                    session.EmailAddr != lecturer.EmailAddr)      // this student
                {
                    return(Unauthorized());
                }

                // perform Update using data in form model
                formModel.Apply(lecturer);
                database.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 13
0
        //[Authenticate("Lecturer")]
        public ActionResult CreateLecture([FromBody] LecturerCreateFormModel fm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // check if existing user already has email address
            if (AuthService.FindUser(fm.EmailAddr) != null)
            {
                return(EmailAddrConflict);
            }


            int lecturerId = -1;

            using (EPortfolioDB db = new EPortfolioDB())
            {
                Lecturer lecturer = fm.Create();
                db.Lecturers.Add(lecturer);
                db.SaveChanges();

                lecturerId = lecturer.LecturerId;
            }

            Object response = new { lecturerId = lecturerId };

            return(Json(response));
        }
Ejemplo n.º 14
0
        public void TestInsertModel()
        {
            // load environment variables from .env
            DotNetEnv.Env.Load();

            // Perform insertion of the project model
            int projectId = -1; // -1 -> null value

            using (EPortfolioDB database = new EPortfolioDB())
            {
                Project project = ProjectTest.GetSampleProject();
                database.Projects.Add(project);
                database.SaveChanges();
                projectId = project.ProjectId;
            }

            // Check for presence of record in the database
            using (EPortfolioDB database = new EPortfolioDB())
            {
                Project obtainProject = database.Projects.Where(
                    (p) => p.ProjectId == projectId).First();
                Assert.True(ProjectTest.CheckSampleProject(obtainProject),
                            "Project obtained from database inconsistent with project" +
                            " inserted into database");

                // cleanup
                database.Projects.Remove(obtainProject);
                database.SaveChanges();
            }
        }
Ejemplo n.º 15
0
        public ActionResult DeleteSkillSet(int id)
        {
            using (EPortfolioDB database = new EPortfolioDB())
            {
                // cascade delete any StudentSkillSet assignments
                IQueryable <StudentSkillSet> assignments = database.StudentSkillSets
                                                           .Where(s => s.SkillSetId == id);
                database.StudentSkillSets.RemoveRange(assignments);

                // Find the skillset specified by formModel
                SkillSet skillSet = database.SkillSets
                                    .Where(s => s.SkillSetId == id)
                                    .FirstOrDefault();
                if (skillSet == null)
                {
                    return(NotFound());
                }

                // remove the skillSet from db
                database.SkillSets.Remove(skillSet);
                database.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 16
0
        public ActionResult UpdateSkillSet(
            int id, [FromBody] SkillSetFormModel formModel)
        {
            // check if contents of form model is valid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            using (EPortfolioDB database = new EPortfolioDB())
            {
                // check if skillset name does not conflict with existing skillset
                if (database.SkillSets
                    .Where(s => s.SkillSetName == formModel.SkillSetName)
                    .Count() >= 2) // all 1 match since updating
                {
                    return(SkillSetNameConflict);
                }

                // Find the skillset specified by formModel
                SkillSet skillSet = database.SkillSets
                                    .Where(s => s.SkillSetId == id)
                                    .FirstOrDefault();
                if (skillSet == null)
                {
                    return(NotFound());
                }

                // perform Update using data in form model
                formModel.Apply(skillSet);
                database.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 17
0
        public ActionResult PostSuggestion([FromBody] SuggestionFormModel formModel)
        {
            EPortfolioDB context         = new EPortfolioDB();
            bool         ifStudentExist  = false; //check if StudentID is valid
            bool         ifLecturerExist = false; //check if LecturerID is valid
            int          suggestionId    = -1;
            Suggestion   s = new Suggestion();

            formModel.Apply(s);
            TryValidateModel(s);
            if (ModelState.IsValid)
            {
                //Validate if the StudentId and LecturerId is existed
                foreach (Lecturer i in context.Lecturers)
                {
                    if (s.LecturerId == i.LecturerId)
                    {
                        ifLecturerExist = true;
                        break;
                    }
                    else
                    {
                        ifLecturerExist = false;
                    }
                }
                foreach (Student i in context.Students)
                {
                    if (s.StudentId == i.StudentId)
                    {
                        ifStudentExist = true;
                        break;
                    }
                    else
                    {
                        ifStudentExist = false;
                    }
                }
                using (EPortfolioDB db = new EPortfolioDB())
                {
                    //If both StudentId & LecturerID is existed DB will save changes
                    if (ifStudentExist == true && ifLecturerExist == true)
                    {
                        db.Suggestions.Add(s);
                        db.SaveChanges();
                        suggestionId = s.SuggestionId;
                    }
                }
                Object response = new { suggestionId = suggestionId };
                return(Json(response));
            }
            else
            {
                return(NotFound());
            }
        }
Ejemplo n.º 18
0
        public ActionResult LecturersDetails()
        {
            List <Lecturer> l = new List <Lecturer>();

            using (EPortfolioDB db = new EPortfolioDB())
            {
                l = db.Lecturers.ToList();
                db.SaveChanges();
            }
            return(Json(l));
        }
Ejemplo n.º 19
0
        public ActionResult AllProjects(int id)
        {
            List <Project> projectList = new List <Project>();

            using (EPortfolioDB db = new EPortfolioDB())
            {
                projectList = db.Projects.ToList();
                db.SaveChanges();
            }
            return(Json(projectList));
        }
Ejemplo n.º 20
0
        public ActionResult AllSkillSets(int id)
        {
            List <SkillSet> skillSetList = new List <SkillSet>();

            using (EPortfolioDB db = new EPortfolioDB())
            {
                skillSetList = db.SkillSets.ToList();
                db.SaveChanges();
            }
            return(Json(skillSetList));
        }
Ejemplo n.º 21
0
        public ActionResult Acknowledge(int id)
        {
            using (EPortfolioDB db = new EPortfolioDB())
            {
                Suggestion status = db.Suggestions.FirstOrDefault(s => s.SuggestionId == id);

                status.Status = "Y";
                db.Update <Suggestion>(status);
                db.SaveChanges();
            }
            return(Ok());
        }
Ejemplo n.º 22
0
        public ActionResult AssignProjectSet(int id, [FromQuery] int student)
        {
            using (EPortfolioDB database = new EPortfolioDB())
            {
                // find if student is already assigned into project
                IQueryable <ProjectMember> matchingAssignments = database
                                                                 .ProjectMembers
                                                                 .Where(s => s.ProjectId == id)
                                                                 .Where(s => s.StudentId == student);
                if (matchingAssignments.Count() >= 1)
                {
                    return(Ok()); // already assigned: nothing to do
                }
                // find if the project leader is already assigned into project.
                ProjectMember roleleader = database
                                           .ProjectMembers
                                           .Where(s => s.ProjectId == id)
                                           .Where(s => s.Role == "Leader").FirstOrDefault();

                // determine if project already has leader
                // if already has leader, assign as normal member
                // otherwise assign as project leader
                string role = "";
                if (roleleader == null)
                {
                    role = "Leader";
                }
                else
                {
                    role = "Member";
                }

                //Create and save the project member to database
                Project projectModel = database.Projects
                                       .Where(s => s.ProjectId == id).Single();
                Student studentModel = database.Students
                                       .Where(s => s.StudentId == student).Single();

                ProjectMember assignment = new ProjectMember
                {
                    Member  = studentModel,
                    Project = projectModel,
                    Role    = role
                };
                database.ProjectMembers.Add(assignment);
                database.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 23
0
        public ActionResult RemoveProject(int id, [FromQuery] int student)
        {
            using (EPortfolioDB database = new EPortfolioDB())
            {
                ProjectMember assignment = database.ProjectMembers
                                           .Where(s => s.ProjectId == id)
                                           .Where(s => s.StudentId == student)
                                           .Single();


                database.ProjectMembers.Remove(assignment);
                database.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 24
0
        //[ValidateAntiForgeryToken]
        public ActionResult DeleteSuggestion(int id)
        {
            using (EPortfolioDB database = new EPortfolioDB())
            {
                // Find the lecturer specified by formModel
                Suggestion suggestion = database.Suggestions
                                        .Where(l => l.SuggestionId == id)
                                        .Single();

                // remove the skillSet from db
                database.Suggestions.Remove(suggestion);
                database.SaveChanges();
            }

            return(Ok());
        }
Ejemplo n.º 25
0
        public ActionResult GetStudents([FromQuery] int?skillset,
                                        [FromQuery] int?names, [FromQuery] int?project)
        {
            using (EPortfolioDB db = new EPortfolioDB())
            {
                IQueryable <Student> matchingStudents = db.Students;

                // apply filters if specified
                if (skillset != null)
                {
                    // filter by skillset id
                    matchingStudents = matchingStudents
                                       .Include(s => s.StudentSkillSets)
                                       .Where(s => s.StudentSkillSets
                                              .Any(ss => ss.SkillSetId == skillset));
                }

                if (project != null)
                {
                    // filter by project id
                    matchingStudents = matchingStudents
                                       .Include(s => s.ProjectMembers)
                                       .Where(s => s.ProjectMembers
                                              .Any(pm => pm.ProjectId == project));
                }

                // construct response with matching students
                if (names != null && names.Value == 1) // id + names of students
                {
                    List <Object> results = matchingStudents
                                            .Select(s => new
                    {
                        Id   = s.StudentId,
                        Name = s.Name
                    } as Object).ToList();

                    return(Json(results));
                }
                else // ids only
                {
                    List <int> results = matchingStudents
                                         .Select(s => s.StudentId).ToList();
                    return(Json(results));
                }
            }
        }
Ejemplo n.º 26
0
        public ActionResult GetSkillSet(int id)
        {
            // Retrieve the Skillset for id
            SkillSet skillset = null;

            using (EPortfolioDB database = new EPortfolioDB())
            {
                skillset = database.SkillSets
                           .Where(s => s.SkillSetId == id)
                           .FirstOrDefault();
            }

            // check if skill has been found for targetId
            if (skillset == null)
            {
                return(NotFound());
            }

            return(Json(skillset));
        }
Ejemplo n.º 27
0
        public ActionResult GetSuggestionByStudent(int id)
        {
            Console.WriteLine("get id:", id.ToString());
            // Retrieve the suggestion for id
            List <Suggestion> suggestion = null;

            using (EPortfolioDB database = new EPortfolioDB())
            {
                suggestion = database.Suggestions
                             .Where(l => l.StudentId == id)
                             .ToList();
            }

            if (suggestion == null)
            {
                return(NotFound());
            }

            return(Json(suggestion));
        }
Ejemplo n.º 28
0
        public ActionResult GetLectureById(int id)
        {
            Console.WriteLine("get id:", id.ToString());
            // Retrieve the lecturer for id
            Lecturer lecturer = null;

            using (EPortfolioDB database = new EPortfolioDB())
            {
                lecturer = database.Lecturers
                           .Where(l => l.LecturerId == id)
                           .FirstOrDefault();
            }

            // check if skill has been found for targetId
            if (lecturer == null)
            {
                return(NotFound());
            }

            return(Json(lecturer));
        }
Ejemplo n.º 29
0
        public ActionResult GetProject(int id)
        {
            Console.WriteLine("get id:", id.ToString());
            // Retrieve the Project for id
            Project project = null;

            using (EPortfolioDB database = new EPortfolioDB())
            {
                project = database.Projects
                          .Where(s => s.ProjectId == id)
                          .FirstOrDefault();
            }

            // check if project has been found for targetId
            if (project == null)
            {
                return(NotFound());
            }

            return(Json(project));
        }
Ejemplo n.º 30
0
        public ActionResult GetMember(int id)
        {
            Console.WriteLine("get id:", id.ToString());
            // Retrieve the lecturer for id
            List <ProjectMember> projectList = new List <ProjectMember>();

            using (EPortfolioDB database = new EPortfolioDB())
            {
                projectList = database.ProjectMembers
                              .Where(s => s.ProjectId == id).ToList();

                database.SaveChanges();
            }

            if (projectList == null)
            {
                return(NotFound());
            }

            return(Json(projectList));
        }