public CommonController()
 {
     db = new Team77LMSContext();
 }
        /*
         * 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).
         */

        // TODO: Uncomment and change 'X' after you have scaffoled

        public void UseLMSContext(Team77LMSContext ctx)
        {
            db = ctx;
        }
Exemple #3
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)
        {
            string uID = "";

            using (db = new Team77LMSContext())
            {
                var query =
                    (from p in db.Users
                     orderby p.UId descending
                     select p.UId).Take(1);

                /*
                 * System.Diagnostics.Debug.WriteLine("----------");
                 * System.Diagnostics.Debug.WriteLine(query.ToArray()[0]);
                 * System.Diagnostics.Debug.WriteLine(query.ToArray()[1]);
                 * System.Diagnostics.Debug.WriteLine(query.ToArray()[2]);
                 * System.Diagnostics.Debug.WriteLine("----------");
                 */

                if (query.ToArray().Count() != 0)
                {
                    // System.Diagnostics.Debug.WriteLine("0");
                    uID = query.ToArray()[0];
                }
                else
                {
                    uID = "first";
                }
            }
            if (uID != "first")
            {
                uID = uID.Substring(1, 7);
                int temp = Int32.Parse(uID) + 1;
                uID = 'u' + temp.ToString();
                while (uID.Count() < 8)
                {
                    uID = uID.Insert(1, "0");
                }
            }
            else
            {
                uID = "u0000000";
            }

            using (db = new Team77LMSContext())
            {
                Users user = new Users();
                user.UId   = uID;
                user.FName = fName;
                user.LName = lName;
                user.Dob   = DOB;
                db.Users.Add(user);

                if (role == "Administrator")
                {
                    Administrators admin = new Administrators();
                    admin.UId = uID;
                    db.Administrators.Add(admin);
                }
                else if (role == "Professor")
                {
                    Professor pf = new Professor();
                    pf.UId     = uID;
                    pf.Subject = SubjectAbbrev;
                    db.Professor.Add(pf);
                }
                else if (role == "Student")
                {
                    Students st = new Students();
                    st.UId        = uID;
                    st.Subject    = SubjectAbbrev;
                    user.Students = st;
                    db.Students.Add(st);
                    System.Diagnostics.Debug.WriteLine("3");
                }
                db.SaveChanges();
            }

            return(uID);
        }