public async Task CreateAsync(CreateGradeBindingModel gradeBindingModel, string teacherName) { EdumanUser Student = this.context.Users.FirstOrDefault(u => u.FirstName == gradeBindingModel.StudentFirstName && u.LastName == gradeBindingModel.StudentLastName && u.IsConfirmed); EdumanUser Teacher = this.context.Users.FirstOrDefault(u => u.UserName == teacherName); if (Student == null || !(await userManager.IsInRoleAsync(Student, "Student"))) { throw new Exception("The User is either non-existent or is not a student"); } if (gradeBindingModel.Subject.Contains(" ")) { gradeBindingModel.Subject = gradeBindingModel.Subject.Split()[0] + gradeBindingModel.Subject.Split()[1]; } Grade gradeModel = new Grade { DateCreated = DateTime.Now, Description = gradeBindingModel.Description, StudentId = Student.Id, Subject = gradeBindingModel.Subject, TeacherId = Teacher.Id, Value = gradeBindingModel.Value }; this.context.Grades.Add(gradeModel); this.context.SaveChanges(); }
public async Task Details_DifferentUser_ShouldReturnNoData() { await roleManager.CreateAsync(new IdentityRole("Student")); await roleManager.CreateAsync(new IdentityRole("Teacher")); await roleManager.CreateAsync(new IdentityRole("Principal")); EdumanUser student = new EdumanUser { UserName = "******", FirstName = "Student", LastName = "TestName", IsConfirmed = true, Id = "TestStudentId", School = "TestSchool" }; EdumanUser teacher = new EdumanUser { UserName = "******", IsConfirmed = true, Id = "TestTeacherId", FirstName = "TeacherFirstName", LastName = "TeacherLastName" }; this.userManager.CreateAsync(student).GetAwaiter(); this.userManager.CreateAsync(teacher).GetAwaiter(); userManager.AddToRoleAsync(student, "Student").GetAwaiter(); userManager.AddToRoleAsync(teacher, "Teacher").GetAwaiter(); this.Context.SaveChanges(); CreateGradeBindingModel model = new CreateGradeBindingModel() { StudentFirstName = "Student", StudentLastName = "TestName", Description = "TestGradeDescription", Subject = "Maths", Value = 6 }; await gradeService.CreateAsync(model, "teacherName"); var id = (gradeService.GetAllAsync("TestStudentId").GetAwaiter().GetResult().FirstOrDefault()).Id; var gradeDetails = await gradeService.GetGradeDetails(id); // Assert Assert.AreEqual("Student", gradeDetails.StudentFirstName); Assert.AreEqual("TestName", gradeDetails.StudentLastName); Assert.AreEqual("TestGradeDescription", gradeDetails.Description); Assert.AreEqual("TeacherFirstName", gradeDetails.TeacherFirstName); Assert.AreEqual("TeacherLastName", gradeDetails.TeacherLastName); Assert.AreEqual(6, gradeDetails.Value); Assert.AreEqual("Maths", gradeDetails.Subject); }
public async Task All_DifferentUser_ShouldReturnNoData() { await roleManager.CreateAsync(new IdentityRole("Student")); await roleManager.CreateAsync(new IdentityRole("Teacher")); await roleManager.CreateAsync(new IdentityRole("Principal")); EdumanUser student = new EdumanUser { UserName = "******", FirstName = "Student", LastName = "TestName", IsConfirmed = true, Id = "TestStudentId" }; EdumanUser anotherStudent = new EdumanUser { UserName = "******", FirstName = "Student1", LastName = "TestName1", IsConfirmed = true, Id = "TestStudentId1" }; EdumanUser teacher = new EdumanUser { UserName = "******", IsConfirmed = true, Id = "TestTeacherId" }; this.userManager.CreateAsync(student).GetAwaiter(); this.userManager.CreateAsync(anotherStudent).GetAwaiter(); this.userManager.CreateAsync(teacher).GetAwaiter(); userManager.AddToRoleAsync(student, "Student").GetAwaiter(); userManager.AddToRoleAsync(anotherStudent, "Student").GetAwaiter(); userManager.AddToRoleAsync(teacher, "Teacher").GetAwaiter(); this.Context.SaveChanges(); CreateGradeBindingModel model = new CreateGradeBindingModel() { StudentFirstName = "Student1", StudentLastName = "TestName1", Description = "TestGradeDescription", Subject = "Maths", Value = 6 }; await gradeService.CreateAsync(model, "teacherName"); var count = (gradeService.GetAllAsync("TestStudentId").GetAwaiter().GetResult()).Count; // Assert Assert.AreEqual(0, count); }
public async Task <IActionResult> Create(CreateGradeBindingModel createGradeBindingModel) { try { await gradeService.CreateAsync(createGradeBindingModel, this.User.Identity.Name); } catch (Exception e) { if (e.Message == "The User is either non-existent or is not a student") { return(this.View("~/Views/Shared/NonExistentStudentPage.cshtml")); } } return(this.RedirectToAction("Index", "Home")); }
public async Task Create_WithNonStudentUser_ShouldNotIncludeIntoDatabase() { await roleManager.CreateAsync(new IdentityRole("Student")); await roleManager.CreateAsync(new IdentityRole("Teacher")); await roleManager.CreateAsync(new IdentityRole("Principal")); EdumanUser student = new EdumanUser { UserName = "******", FirstName = "Student", LastName = "TestName", IsConfirmed = true, Id = "TestStudentId" }; EdumanUser teacher = new EdumanUser { UserName = "******", IsConfirmed = true, Id = "TestTeacherId" }; this.userManager.CreateAsync(student).GetAwaiter(); this.userManager.CreateAsync(teacher).GetAwaiter(); userManager.AddToRoleAsync(student, "Principal").GetAwaiter(); userManager.AddToRoleAsync(teacher, "Teacher").GetAwaiter(); this.Context.SaveChanges(); CreateGradeBindingModel model = new CreateGradeBindingModel() { StudentFirstName = "Student", StudentLastName = "TestName", Description = "TestGradeDescription", Subject = "Maths", Value = 6 }; // Assert var ex = Assert.ThrowsAsync <Exception>(() => gradeService.CreateAsync(model, "teacherName")); Assert.AreEqual(ex.Message, "The User is either non-existent or is not a student"); }