public void can_create_find_update_and_delete_teacher() { TeacherGateway sut = new TeacherGateway(TestHelpers.ConnectionString); string firstName = TestHelpers.RandomTestName(); string lastName = TestHelpers.RandomTestName(); sut.Create(firstName, lastName); Teacher teacher; { teacher = sut.FindByName(firstName, lastName); CheckTeacher(teacher, firstName, lastName); } { teacher = sut.FindById(teacher.TeacherId); CheckTeacher(teacher, firstName, lastName); } { firstName = TestHelpers.RandomTestName(); lastName = TestHelpers.RandomTestName(); sut.Update(teacher.TeacherId, firstName, lastName); teacher = sut.FindById(teacher.TeacherId); CheckTeacher(teacher, firstName, lastName); } { sut.Delete(teacher.TeacherId); teacher = sut.FindById(teacher.TeacherId); Assert.That(teacher, Is.Null); } }
public Result <Teacher> UpdateTeacher(int teacherId, string firstName, string lastName) { if (!IsNameValid(firstName)) { return(Result.Failure <Teacher>(Status.BadRequest, "The first name is not valid.")); } if (!IsNameValid(lastName)) { return(Result.Failure <Teacher>(Status.BadRequest, "The last name is not valid.")); } Teacher teacher; if ((teacher = _teacherGateway.FindById(teacherId)) == null) { return(Result.Failure <Teacher>(Status.NotFound, "Teacher not found.")); } { Teacher t = _teacherGateway.FindByName(firstName, lastName); if (t != null && t.TeacherId != teacher.TeacherId) { return(Result.Failure <Teacher>(Status.BadRequest, "A teacher with this name already exists.")); } } _teacherGateway.Update(teacherId, firstName, lastName); teacher = _teacherGateway.FindById(teacherId); return(Result.Success(Status.Ok, teacher)); }
public async Task can_create_find_update_and_delete_teacher() { TeacherGateway sut = new TeacherGateway(TestHelpers.ConnectionString); string firstName = TestHelpers.RandomTestName(); string lastName = TestHelpers.RandomTestName(); var result = await sut.Create(firstName, lastName); Assert.That(result.Status, Is.EqualTo(Status.Created)); int teacherId = result.Content; Result <TeacherData> teacher; { teacher = await sut.FindById(teacherId); CheckTeacher(teacher, firstName, lastName); } { firstName = TestHelpers.RandomTestName(); lastName = TestHelpers.RandomTestName(); Result r = await sut.Update(teacherId, firstName, lastName); Assert.That(r.Status, Is.EqualTo(Status.Ok)); teacher = await sut.FindById(teacherId); CheckTeacher(teacher, firstName, lastName); } { await sut.Delete(teacherId); teacher = await sut.FindById(teacherId); Assert.That(teacher.Status, Is.EqualTo(Status.NotFound)); Assert.That(teacher.HasError, Is.True); } }
public async Task <IActionResult> GetTeacherById(int id) { Result <TeacherData> result = await _teacherGateway.FindById(id); return(this.CreateResult(result)); }