Skip to content

Refactor CourseSerivce

Currently the CourseService class has a few issues.

  1. The method findUser tries finding a user, else it creates a user. Methods called find should not change state in the database only read it. Hence we should rename it findOrCreateUser to make this explicit.
  2. This snippet:
			Role role = null;
			switch (csvUser.getRole()) {
				case ROLE_ASSISTANT:
					role = new Assistant(staff, course);
					break;
				case ROLE_TEACHER:
					role = new Teacher(staff, course);
					break;
				case ROLE_MANAGER:
					role = new Manager(staff, course);
					break;
				default:
					throw new IllegalStateException("Unexpected value: " + csvUser.getRole());
			}

Should become a method on CsvUserHelper and could be unit tested there.

  1. This save seems redundant since staff will be saved at the end of the method regardless.

  2. All the repository fields can be marked final, if we create a constructor assigning them and mark that constructor as @Autowired instead of the individual fields.

Edited by Liam Clark