/// <summary> /// Edit record of student mapped to studentid. /// </summary> /// <param name="studentid">ID of student to edit info of</param> /// <param name="name">New name of student</param> /// <param name="email">New email of student</param> /// <param name="gradelevel">New grade level of student</param> public async Task EditStudentInfo(int?studentid, string name, string email, byte gradelevel) { //Grab student from database based on studentid var studentToUpdate = _context.Students.FirstOrDefault(s => s.StudentId == studentid); //Save student's current email as string for changing their user data string studentCurrentEmail = studentToUpdate.Email; //Split name into firstname and lastname string studentfirstname = name.Substring(0, name.IndexOf(' ')); string studentlastname = name.Substring(name.IndexOf(' ') + 1, name.Length - name.IndexOf(' ') - 1); //Update info from page studentToUpdate.StudentFirstName = studentfirstname; studentToUpdate.StudentLastName = studentlastname; studentToUpdate.Email = email; studentToUpdate.GradeLevel = Convert.ToByte(gradelevel); //Change student data in local database instance _context.Update(studentToUpdate); /* * //Grab user with same email as student * var userStudentToUpdate = _context.Users.FirstOrDefault(u => u.Email == studentCurrentEmail); * * //Update user email to inputted email * userStudentToUpdate.Email = email; * * //Save user data in local database instance * _context.Update(userStudentToUpdate); */ //Flush changes to database await _context.SaveChangesAsync(); }
/// <summary> /// Updates Grade record mapped to gradeid /// </summary> /// <param name="gradeid">ID of Grade record to update</param> /// <param name="gradevalue">New value of Grade record</param> public async Task SubmitGrade(int gradeid, string gradevalue) { //Grab grade object from database var gradeToUpdate = _context.Grades.FirstOrDefault(g => g.GradeId == gradeid); //Update gradevalue for this grade gradeToUpdate.GradeValue = gradevalue; //Explicitly setting gradeid to avoid duplicates gradeToUpdate.GradeId = gradeid; //Save and flush changes to database _context.Update(gradeToUpdate); await _context.SaveChangesAsync(); }