예제 #1
0
        public string EditPersonalInfo(
            long studentId, string firstName, string lastName, long nameSuffixId,
            string email, long favoriteCourseId)
        {
            Student student = _repository.GetById(studentId);

            if (student == null)
            {
                return("Student not found");
            }

            Course favoriteCourse = Course.FromId(favoriteCourseId);

            if (favoriteCourse == null)
            {
                return("Course not found");
            }

            Suffix suffix = Suffix.FromId(nameSuffixId);

            if (suffix == null)
            {
                return("Suffix not found");
            }

            Result <Email> emailResult = Email.Create(email);

            if (emailResult.IsFailure)
            {
                return(emailResult.Error);
            }

            Result <Name> nameResult = Name.Create(firstName, lastName, suffix);

            if (nameResult.IsFailure)
            {
                return(nameResult.Error);
            }

            student.EditPersonalInfo(nameResult.Value, emailResult.Value, favoriteCourse);

            // for debugging purposes only:
            // var studentEntityState = _context.Entry(student).State;
            // var favoriteCourseEntityState = _context.Entry(student.FavoriteCourse).State;
            // var enrollmentEntityState = _context.Entry(student.Enrollments[0]).State;
            // var courseEntityState = _context.Entry(student.Enrollments[0].Course).State;

            _context.SaveChanges();

            return("OK");
        }
예제 #2
0
        public string RegisterStudent(
            string firstName, string lastName, long nameSuffixId, string email,
            long favoriteCourseId, Grade favoriteCourseGrade)
        {
            Course favoriteCourse = Course.FromId(favoriteCourseId);

            if (favoriteCourse == null)
            {
                return("Course not found");
            }

            Suffix suffix = Suffix.FromId(nameSuffixId);

            if (suffix == null)
            {
                return("Suffix not found");
            }

            Result <Email> emailResult = Email.Create(email);

            if (emailResult.IsFailure)
            {
                return(emailResult.Error);
            }

            Result <Name> nameResult = Name.Create(firstName, lastName, suffix);

            if (nameResult.IsFailure)
            {
                return(nameResult.Error);
            }

            var student = new Student(nameResult.Value, emailResult.Value,
                                      favoriteCourse, favoriteCourseGrade);

            _repository.Save(student);
            _context.SaveChanges();

            return("OK");
        }