public async Task <IActionResult> CreateStudent([FromBody] StudentViewModel model)
        {
            Result <int> result;

            if (model.ClassId == 0)
            {
                result = await _studentGateway.Create(model.FirstName, model.LastName, model.BirthDate, model.GitHubLogin);
            }
            else
            {
                result = await _studentGateway.Create(model.FirstName, model.LastName, model.BirthDate, model.GitHubLogin, model.ClassId);
            }
            return(this.CreateResult(result, o =>
            {
                o.RouteName = "GetStudent";
                o.RouteValues = id => new { id };
            }));
        }
        public async Task can_assign_class()
        {
            ClassGateway   classGateway  = new ClassGateway(TestHelpers.ConnectionString);
            StudentGateway sut           = new StudentGateway(TestHelpers.ConnectionString);
            string         firstName     = TestHelpers.RandomTestName();
            string         lastName      = TestHelpers.RandomTestName();
            DateTime       birthDate     = TestHelpers.RandomBirthDate(_random.Next(5, 10));
            Result <int>   studentResult = await sut.Create(firstName, lastName, birthDate);

            int studentId = studentResult.Content;

            int classId1;
            {
                string       className   = TestHelpers.RandomTestName();
                string       level       = TestHelpers.RandomLevel();
                Result <int> classResult = await classGateway.Create(className, level);

                Assert.That(classResult.Status, Is.EqualTo(Status.Created));
                classId1 = classResult.Content;

                await sut.AssignClass(studentId, classId1);

                Result <StudentClassData> studentClass = await sut.FindStudentClassById(studentId);

                CheckStudent(studentClass, firstName, lastName, birthDate, classId1);
            }

            {
                string       className   = TestHelpers.RandomTestName();
                string       level       = "CP";
                Result <int> classResult = await classGateway.Create(className, level);

                int classId2 = classResult.Content;
                await sut.AssignClass(studentId, classId2);

                Result <StudentClassData> studentClass = await sut.FindStudentClassById(studentId);

                CheckStudent(studentClass, firstName, lastName, birthDate, classId2);

                await sut.AssignClass(studentId, 0);

                studentClass = await sut.FindStudentClassById(studentId);

                CheckStudent(studentClass, firstName, lastName, birthDate, 0);

                await classGateway.Delete(classId2);
            }

            await sut.Delete(studentId);

            await classGateway.Delete(classId1);
        }
        public void can_assign_class()
        {
            ClassGateway classGateway = new ClassGateway(TestHelpers.ConnectionString);
            string       className1   = TestHelpers.RandomTestName();
            string       level1       = TestHelpers.RandomLevel();

            classGateway.Create(className1, level1);
            Class c1 = classGateway.FindByName(className1);

            StudentGateway sut       = new StudentGateway(TestHelpers.ConnectionString);
            string         firstName = TestHelpers.RandomTestName();
            string         lastName  = TestHelpers.RandomTestName();
            DateTime       birthDate = TestHelpers.RandomBirthDate(_random.Next(5, 10));

            sut.Create(firstName, lastName, birthDate, string.Empty, c1.ClassId);

            Student student;

            {
                student = sut.FindByName(firstName, lastName);
                CheckStudent(student, firstName, lastName, birthDate, c1.ClassId);
            }

            {
                string className2 = TestHelpers.RandomTestName();
                string level2     = "CP";
                classGateway.Create(className2, level2);
                Class c2 = classGateway.FindByName(className2);
                sut.AssignClass(student.StudentId, c2.ClassId);
                student = sut.FindById(student.StudentId);
                CheckStudent(student, firstName, lastName, birthDate, c2.ClassId);

                sut.AssignClass(student.StudentId, 0);
                student = sut.FindById(student.StudentId);
                CheckStudent(student, firstName, lastName, birthDate, 0);

                classGateway.Delete(c2.ClassId);
            }

            sut.Delete(student.StudentId);
            classGateway.Delete(c1.ClassId);
        }
        public async Task can_create_find_update_and_delete_student()
        {
            StudentGateway sut           = new StudentGateway(TestHelpers.ConnectionString);
            string         firstName     = TestHelpers.RandomTestName();
            string         lastName      = TestHelpers.RandomTestName();
            DateTime       birthDate     = TestHelpers.RandomBirthDate(_random.Next(5, 10));
            Result <int>   studentResult = await sut.Create(firstName, lastName, birthDate);

            Assert.That(studentResult.Status, Is.EqualTo(Status.Created));
            int studentId = studentResult.Content;
            Result <StudentData> student;

            {
                student = await sut.FindById(studentId);

                CheckStudent(student, firstName, lastName, birthDate);
            }

            {
                firstName = TestHelpers.RandomTestName();
                lastName  = TestHelpers.RandomTestName();
                birthDate = TestHelpers.RandomBirthDate(_random.Next(5, 10));
                Result r = await sut.Update(studentId, firstName, lastName, birthDate, null);

                Assert.That(r.Status, Is.EqualTo(Status.Ok));

                student = await sut.FindById(studentId);

                CheckStudent(student, firstName, lastName, birthDate);
            }

            {
                Result r = await sut.Delete(studentId);

                Assert.That(r.Status, Is.EqualTo(Status.Ok));
                student = await sut.FindById(studentId);

                Assert.That(student.Status, Is.EqualTo(Status.NotFound));
            }
        }
Example #5
0
        public Result <Student> CreateStudent(string firstName, string lastName, DateTime birthDate, string photo, string gitHubLogin)
        {
            if (!IsNameValid(firstName))
            {
                return(Result.Failure <Student>(Status.BadRequest, "The first name is not valid."));
            }
            if (!IsNameValid(lastName))
            {
                return(Result.Failure <Student>(Status.BadRequest, "The last name is not valid."));
            }
            if (_studentGateway.FindByName(firstName, lastName) != null)
            {
                return(Result.Failure <Student>(Status.BadRequest, "A student with this name already exists."));
            }
            if (!string.IsNullOrEmpty(gitHubLogin) && _studentGateway.FindByGitHubLogin(gitHubLogin) != null)
            {
                return(Result.Failure <Student>(Status.BadRequest, "A student with GitHub login already exists."));
            }
            _studentGateway.Create(firstName, lastName, birthDate, photo, gitHubLogin);
            Student student = _studentGateway.FindByName(firstName, lastName);

            return(Result.Success(Status.Created, student));
        }
        public void can_create_find_update_and_delete_student()
        {
            StudentGateway sut       = new StudentGateway(TestHelpers.ConnectionString);
            string         firstName = TestHelpers.RandomTestName();
            string         lastName  = TestHelpers.RandomTestName();
            DateTime       birthDate = TestHelpers.RandomBirthDate(_random.Next(5, 10));

            sut.Create(firstName, lastName, birthDate);
            Student student;

            {
                student = sut.FindByName(firstName, lastName);
                CheckStudent(student, firstName, lastName, birthDate);
            }

            {
                student = sut.FindById(student.StudentId);
                CheckStudent(student, firstName, lastName, birthDate);
            }

            {
                firstName = TestHelpers.RandomTestName();
                lastName  = TestHelpers.RandomTestName();
                birthDate = TestHelpers.RandomBirthDate(_random.Next(5, 10));
                sut.Update(student.StudentId, firstName, lastName, birthDate, null);

                student = sut.FindById(student.StudentId);
                CheckStudent(student, firstName, lastName, birthDate);
            }

            {
                sut.Delete(student.StudentId);
                student = sut.FindById(student.StudentId);
                Assert.That(student, Is.Null);
            }
        }