Example #1
0
        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 async Task <IActionResult> CreateTeacher([FromBody] TeacherViewModel model)
        {
            Result <int> result = await _teacherGateway.Create(model.FirstName, model.LastName);

            return(this.CreateResult(result, o =>
            {
                o.RouteName = "GetTeacher";
                o.RouteValues = id => new { id };
            }));
        }
        public void can_assign_teacher()
        {
            TeacherGateway teacherGateway = new TeacherGateway(TestHelpers.ConnectionString);
            string         firstName      = TestHelpers.RandomTestName();
            string         lastName       = TestHelpers.RandomTestName();

            teacherGateway.Create(firstName, lastName);
            Teacher teacher1 = teacherGateway.FindByName(firstName, lastName);

            ClassGateway sut   = new ClassGateway(TestHelpers.ConnectionString);
            string       name  = TestHelpers.RandomTestName();
            string       level = TestHelpers.RandomLevel();

            sut.Create(name, level, teacher1.TeacherId);

            Class c;

            {
                c = sut.FindByName(name);
                CheckClass(c, name, level, teacher1.TeacherId);
            }

            {
                string firstName2 = TestHelpers.RandomTestName();
                string lastName2  = TestHelpers.RandomTestName();
                teacherGateway.Create(firstName2, lastName2);
                Teacher teacher2 = teacherGateway.FindByName(firstName2, lastName2);
                sut.AssignTeacher(c.ClassId, teacher2.TeacherId);
                c = sut.FindById(c.ClassId);
                CheckClass(c, name, level, teacher2.TeacherId);

                sut.AssignTeacher(c.ClassId, 0);
                c = sut.FindById(c.ClassId);
                CheckClass(c, name, level, 0);

                teacherGateway.Delete(teacher2.TeacherId);
            }

            sut.Delete(c.ClassId);
            teacherGateway.Delete(teacher1.TeacherId);
        }
        public Result <Teacher> CreateTeacher(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."));
            }
            if (_teacherGateway.FindByName(firstName, lastName) != null)
            {
                return(Result.Failure <Teacher>(Status.BadRequest, "A teacher with this name already exists."));
            }

            _teacherGateway.Create(firstName, lastName);
            Teacher teacher = _teacherGateway.FindByName(firstName, lastName);

            return(Result.Success(Status.Created, 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);
            }
        }