Source:
public JsonResult execute() throws EntityNotFoundException { String courseId = getNonNullRequestParamValue(Const.ParamsNames.COURSE_ID); if (!logic.isCoursePresent(courseId)) { throw new EntityNotFoundException(new EntityDoesNotExistException("The course does not exist: " + courseId)); } StudentsEnrollRequest enrollRequests = getAndValidateRequestBody(StudentsEnrollRequest.class); List<StudentAttributes> studentsToEnroll = new ArrayList<>(); enrollRequests.getStudentEnrollRequests().forEach(studentEnrollRequest -> { studentsToEnroll.add(StudentAttributes.builder(courseId, studentEnrollRequest.getEmail()) .withName(studentEnrollRequest.getName()) .withSectionName(studentEnrollRequest.getSection()) .withTeamName(studentEnrollRequest.getTeam()) .withComment(studentEnrollRequest.getComments()) .build()); }); List<StudentAttributes> existingStudents = logic.getStudentsForCourse(courseId); Set<String> existingStudentsEmail = existingStudents.stream().map(StudentAttributes::getEmail).collect(Collectors.toSet()); List<StudentAttributes> enrolledStudents = new ArrayList<>(); studentsToEnroll.forEach(student -> { if (existingStudentsEmail.contains(student.email)) { StudentAttributes.UpdateOptions updateOptions = StudentAttributes.updateOptionsBuilder(student.getCourse(), student.getEmail()) .withName(student.getName()) .withSectionName(student.getSection()) .withTeamName(student.getTeam()) .withComment(student.getComments()) .build(); try { StudentAttributes updatedStudent = logic.updateStudentCascade(updateOptions); enrolledStudents.add(updatedStudent); } catch (InvalidParametersException | EntityDoesNotExistException | EntityAlreadyExistsException exception) { return; } } else { try { StudentAttributes newStudent = logic.createStudent(student); enrolledStudents.add(newStudent); } catch (InvalidParametersException | EntityAlreadyExistsException exception) { return; } } }); return new JsonResult(new StudentsData(enrolledStudents)); }

Target:
public JsonResult execute() { String courseId = getNonNullRequestParamValue(Const.ParamsNames.COURSE_ID); StudentsEnrollRequest enrollRequests = getAndValidateRequestBody(StudentsEnrollRequest.class); List<StudentAttributes> studentsToEnroll = new ArrayList<>(); enrollRequests.getStudentEnrollRequests().forEach(studentEnrollRequest -> { studentsToEnroll.add(StudentAttributes.builder(courseId, studentEnrollRequest.getEmail()) .withName(studentEnrollRequest.getName()) .withSectionName(studentEnrollRequest.getSection()) .withTeamName(studentEnrollRequest.getTeam()) .withComment(studentEnrollRequest.getComments()) .build()); }); List<StudentAttributes> existingStudents = logic.getStudentsForCourse(courseId); validateTeamName(existingStudents, studentsToEnroll); Set<String> existingStudentsEmail = existingStudents.stream().map(StudentAttributes::getEmail).collect(Collectors.toSet()); List<StudentAttributes> enrolledStudents = new ArrayList<>(); studentsToEnroll.forEach(student -> { if (existingStudentsEmail.contains(student.email)) { StudentAttributes.UpdateOptions updateOptions = StudentAttributes.updateOptionsBuilder(student.getCourse(), student.getEmail()) .withName(student.getName()) .withSectionName(student.getSection()) .withTeamName(student.getTeam()) .withComment(student.getComments()) .build(); try { StudentAttributes updatedStudent = logic.updateStudentCascade(updateOptions); enrolledStudents.add(updatedStudent); } catch (InvalidParametersException | EntityDoesNotExistException | EntityAlreadyExistsException exception) { return; } } else { try { StudentAttributes newStudent = logic.createStudent(student); enrolledStudents.add(newStudent); } catch (InvalidParametersException | EntityAlreadyExistsException exception) { return; } } }); return new JsonResult(new StudentsData(enrolledStudents)); }