예제 #1
0
        /*******Begin code to modify********/

        /// <summary>
        /// Create a new user of the LMS with the specified information.
        /// Assigns the user a unique uID consisting of a 'u' followed by 7 digits.
        /// </summary>
        /// <param name="fName">First Name</param>
        /// <param name="lName">Last Name</param>
        /// <param name="DOB">Date of Birth</param>
        /// <param name="SubjectAbbrev">The department the user belongs to (professors and students only)</param>
        /// <param name="SubjectAbbrev">The user's role: one of "Administrator", "Professor", "Student"</param>
        /// <returns>A unique uID that is not be used by anyone else</returns>
        public string CreateNewUser(string fName, string lName, DateTime DOB, string SubjectAbbrev, string role)
        {
            // generate a new UID
            String uID = GenerateNewUID();

            using (Team89LMSContext db = new Team89LMSContext())
            {
                // get dept ID for professor and student roles
                var querydID = (from d in db.Department
                                where d.Subject.Equals(SubjectAbbrev)
                                select d.DId).Distinct();
                switch (role)
                {
                case "Administrator":
                    Administrator admin = new Administrator();
                    admin.UId       = uID;
                    admin.FirstName = fName;
                    admin.LastName  = lName;
                    admin.Dob       = DOB;

                    db.Administrator.Add(admin);
                    db.SaveChanges();
                    break;

                case "Professor":
                    Professor prof = new Professor();
                    prof.UId       = uID;
                    prof.FirstName = fName;
                    prof.LastName  = lName;
                    prof.Dob       = DOB;
                    prof.DId       = querydID.ToArray()[0];

                    db.Professor.Add(prof);
                    db.SaveChanges();
                    break;

                case "Student":
                    Student stu = new Student();
                    stu.UId       = uID;
                    stu.FirstName = fName;
                    stu.LastName  = lName;
                    stu.Dob       = DOB;
                    stu.DId       = querydID.ToArray()[0];

                    db.Student.Add(stu);
                    db.SaveChanges();
                    break;
                }
            }

            return(uID);
        }
예제 #2
0
        /*******Begin code to modify********/


        /// <summary>
        /// Returns a JSON array of all the students in a class.
        /// Each object in the array should have the following fields:
        /// "fname" - first name
        /// "lname" - last name
        /// "uid" - user ID
        /// "dob" - date of birth
        /// "grade" - the student's grade in this class
        /// </summary>
        /// <param name="subject">The course subject abbreviation</param>
        /// <param name="num">The course number</param>
        /// <param name="season">The season part of the semester for the class the assignment belongs to</param>
        /// <param name="year">The year part of the semester for the class the assignment belongs to</param>
        /// <returns>The JSON array</returns>
        public IActionResult GetStudentsInClass(string subject, int num, string season, int year)
        {
            using (Team89LMSContext db = new Team89LMSContext())
            {
                var query = from p in db.Department
                            join g in db.Courses on p.DId equals g.DId
                            join h in db.Classes on g.CId equals h.CId
                            join x in db.Enrolled on h.ClassId equals x.ClassId
                            join w in db.Student on x.UId equals w.UId
                            where h.SemesterSeason.Equals(season) && h.SemesterYear == year && g.Number.Equals(num) && p.Subject.Equals(subject)
                            select new
                {
                    fname = w.FirstName,
                    lname = w.LastName,
                    uid   = w.UId,
                    dob   = w.Dob,
                    grade = x.Grade
                };
                return(Json(query.ToArray()));
            }
        }
예제 #3
0
        /// <summary>
        /// Finds the highest uID from the Student, Administrator, and Professor tables and returns a uID 1 more than it
        /// </summary>
        /// <returns>A new uID that's 1 larger than the current highest uID</returns>
        private string GenerateNewUID()
        {
            using (Team89LMSContext db = new Team89LMSContext())
            {
                // get highest student uID
                var querysID = from s in db.Student
                               orderby s.UId descending
                               select s.UId;

                string highestStudent = (!querysID.Any()) ? "" : querysID.ToArray()[0];

                // get highest admin uID
                var queryaID = from a in db.Administrator
                               orderby a.UId descending
                               select a.UId;

                string highestAdmin = (!queryaID.Any()) ? "" : queryaID.ToArray()[0];

                // get highest prof uID
                var querypID = from p in db.Professor
                               orderby p.UId descending
                               select p.UId;
                string highestProf = (!querypID.Any()) ? "" : querypID.ToArray()[0];

                // convert IDs to ints
                int student, admin, prof;
                student = (highestStudent == "") ? 0 : Int32.Parse(highestStudent.Substring(1));
                admin   = (highestAdmin == "") ? 0 : Int32.Parse(highestAdmin.Substring(1));
                prof    = (highestProf == "") ? 0 : Int32.Parse(highestProf.Substring(1));

                // find the max of the three IDs
                int maxID = Math.Max(student, Math.Max(admin, prof));

                // increment and format the new uID
                string newUID = "u" + String.Format("{0,22:D7}", maxID + 1);

                // remove whitespace from the string and return it
                return(String.Concat(newUID.Where(c => !Char.IsWhiteSpace(c))));
            }
        }
예제 #4
0
        /*
         * WARNING: This is the quick and easy way to make the controller
         *          use a different LibraryContext - good enough for our purposes.
         *          The "right" way is through Dependency Injection via the constructor
         *          (look this up if interested).
         */

        public void UseLMSContext(Team89LMSContext ctx)
        {
            db = ctx;
        }
예제 #5
0
 public CommonController()
 {
     db = new Team89LMSContext();
 }