Esempio n. 1
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);
            }
        }
Esempio n. 2
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());
        }
Esempio n. 3
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));
        }
Esempio n. 4
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());
        }
Esempio n. 5
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());
        }
Esempio n. 6
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());
        }
Esempio n. 7
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());
        }
Esempio n. 8
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());
        }
Esempio n. 9
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();
            }
        }
Esempio n. 10
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));
        }
        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());
        }
        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());
        }
Esempio n. 13
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());
            }
        }
Esempio n. 14
0
        public ActionResult LecturersDetails()
        {
            List <Lecturer> l = new List <Lecturer>();

            using (EPortfolioDB db = new EPortfolioDB())
            {
                l = db.Lecturers.ToList();
                db.SaveChanges();
            }
            return(Json(l));
        }
Esempio n. 15
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));
        }
        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));
        }
Esempio n. 17
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());
        }
Esempio n. 18
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());
        }
Esempio n. 19
0
        // test updating models in the database
        public void TestUpdateModel()
        {
            // load environment variables from .env
            DotNetEnv.Env.Load();

            int projectId = 0;

            using (EPortfolioDB database = new EPortfolioDB())
            {
                // insert test project into db
                Project project = ProjectTest.GetSampleProject();
                database.Projects.Add(project);
                database.SaveChanges();
                projectId = project.ProjectId;

                // check that the project has not yet changed
                Assert.True(ProjectTest.CheckSampleProject(project));

                // Perform update on model
                project.Title = "Deep Learning Time Travel";
                database.SaveChanges();
            }

            // check that the project has actually been updated
            using (EPortfolioDB database = new EPortfolioDB())
            {
                Project obtainProject = database.Projects.Where(
                    (p) => p.ProjectId == projectId).First();
                Assert.False(ProjectTest.CheckSampleProject(obtainProject),
                             "Project update changes has not propogated to database");
                Assert.Equal("Deep Learning Time Travel", obtainProject.Title);

                // cleanup
                database.Projects.Remove(obtainProject);
                database.SaveChanges();
            }
        }
Esempio n. 20
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());
        }
Esempio n. 21
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());
        }
        public ActionResult RemoveSkillSet(int id, [FromQuery] int student)
        {
            using (EPortfolioDB database = new EPortfolioDB())
            {
                // obtain assignment as per the given request
                StudentSkillSet assignment = database.StudentSkillSets
                                             .Where(s => s.SkillSetId == id)
                                             .Where(s => s.StudentId == student)
                                             .FirstOrDefault();
                if (assignment == null)
                {
                    return(NotFound());
                }

                // remove assignment from database
                database.StudentSkillSets.Remove(assignment);
                database.SaveChanges();
            }

            return(Ok());
        }
Esempio n. 23
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));
        }
Esempio n. 24
0
        public ActionResult GetMentees(int id)
        {
            Console.WriteLine("get id:", id.ToString());
            // Retrieve the lecturer for id
            List <Student> studentList = new List <Student>();

            using (EPortfolioDB database = new EPortfolioDB())
            {
                studentList = database.Students
                              .Where(s => s.MentorId == id).ToList();
                database.SaveChanges();
            }

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

            return(Json(studentList));
        }
        public ActionResult AssignSkillSet(int id, [FromQuery] int student)
        {
            using (EPortfolioDB database = new EPortfolioDB())
            {
                // check if student skillset already exists in the database
                IQueryable <StudentSkillSet> matchingAssignments = database
                                                                   .StudentSkillSets
                                                                   .Where(s => s.SkillSetId == id)
                                                                   .Where(s => s.StudentId == student);
                if (matchingAssignments.Count() >= 1)
                {
                    return(Ok()); // skillset already assigned to student
                }
                // obtain models for the specified by the given request
                SkillSet skillSetModel = database.SkillSets
                                         .Where(s => s.SkillSetId == id).FirstOrDefault();
                Student studentModel = database.Students
                                       .Where(s => s.StudentId == student).FirstOrDefault();

                if (skillSetModel == null || studentModel == null)
                {
                    return(NotFound());
                }

                // assign the skillset to the student
                StudentSkillSet assignment = new StudentSkillSet
                {
                    Student  = studentModel,
                    SkillSet = skillSetModel
                };
                database.StudentSkillSets.Add(assignment);
                database.SaveChanges();
            }

            return(Ok());
        }
        //[Authenticate("Lecturer")]
        public ActionResult CreateSkillSet([FromBody] SkillSetFormModel formModel)
        {
            // check if contents of form model is valid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // write the given skillset to database
            int skillSetId = -1;

            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() >= 1)
                {
                    return(SkillSetNameConflict);
                }

                // create skillSet with form model values
                SkillSet skillSet = new SkillSet();
                formModel.Apply(skillSet);

                // add new skillset to database
                database.SkillSets.Add(skillSet);
                database.SaveChanges();
                skillSetId = skillSet.SkillSetId;
            }

            // respond with sucess message with inserted skillset id
            Object response = new { skillSetId = skillSetId };

            return(Json(response));
        }
Esempio n. 27
0
        public ActionResult Createproject([FromBody] ProjectFormModel formModel)
        {
            // check if authenticated
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // write the given project to database
            int projectId = -1;

            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);
                }

                // create project with form model values
                Project project = new Project();
                formModel.Apply(project);

                // add new project to database
                database.Projects.Add(project);
                database.SaveChanges();
                projectId = project.ProjectId;
            }

            // respond with sucess message with inserted project id
            Object response = new { projectId = projectId };

            return(Json(response));
        }
Esempio n. 28
0
        public void TestComplexModel()
        {
            // load environment variables from .env
            DotNetEnv.Env.Load();

            /* Insert a complex model with foreign model relationships
             * <- means depends foreign model
             * Lecturer <- Student <- ProjectMembers -> Project
             */
            int lecturerId = -1;

            using (EPortfolioDB database = new EPortfolioDB())
            {
                // Lecturer model
                Lecturer lecturer = LecturerTest.GetSampleLecturer();
                database.Lecturers.Add(lecturer);

                // Student model
                Student student = StudentTest.GetSampleStudent();
                student.Mentor = lecturer;
                database.Students.Add(student);

                // Project model
                Project project = ProjectTest.GetSampleProject();
                database.Projects.Add(project);

                // ProjectMember model
                ProjectMember projectMember = new ProjectMember
                {
                    Member  = student,
                    Project = project,
                    Role    = "Member"
                };
                database.ProjectMembers.Add(projectMember);

                database.SaveChanges();
                lecturerId = lecturer.LecturerId;
            }

            // query complex model
            using (EPortfolioDB database = new EPortfolioDB())
            {
                Lecturer lecturer = database.Lecturers
                                    .Where(l => l.LecturerId == lecturerId)
                                    .Include(l => l.Students)
                                    .ThenInclude(s => s.ProjectMembers)
                                    .ThenInclude(pm => pm.Project)
                                    .Single();

                Assert.True(LecturerTest.CheckSampleLecturer(lecturer),
                            "lecturer obtained from DB inconsistent with one inserted " +
                            "into the db");

                Student student = lecturer.Students.First();
                Assert.True(StudentTest.CheckSampleStudent(student),
                            "student obtained from DB inconsistent with one inserted " +
                            "into the db");


                ProjectMember projectMember = lecturer.Students.First()
                                              .ProjectMembers.First();
                Assert.Equal("Member", projectMember.Role);

                Project project = projectMember.Project;
                Assert.True(ProjectTest.CheckSampleProject(project),
                            "project obtained from DB inconsistent with one inserted " +
                            "into the db");

                // cleanup
                database.ProjectMembers.Remove(projectMember);
                database.Projects.Remove(project);
                database.Students.Remove(student);
                database.Lecturers.Remove(lecturer);
                database.SaveChanges();
            }
        }