Skip to content

Instantly share code, notes, and snippets.

@peterjurkovic
Last active April 10, 2018 11:25
Show Gist options
  • Save peterjurkovic/f228ca8482c61d07fbf8251dd2d4e95a to your computer and use it in GitHub Desktop.
Save peterjurkovic/f228ca8482c61d07fbf8251dd2d4e95a to your computer and use it in GitHub Desktop.
Code review exercise / refactoring
// UserService.java
public interface UserService {
User getByLoging(String login);
void create(User user);
}
// DefaultUserService.java
@Component
@Transactional
public class DefaultUserService implements UserService {
private User user;
private Logger log = LoggerFactory.getLogger(getClass());
@Autowired
private UserRepository userRepository;
@Autowired
private AccountHttpClient accountHttpClient;
@Autowired
private QuotaHttpClient quataHttpClient;
public User getByLoging(String login) {
return userRepository.findByLoging(login);
}
public void create(User user) {
this.user = user;
CompletableFuture<String> futureAccountRef = CompletableFuture
.supplyAsync( () -> createAccount() );
CompletableFuture<String> futureQuotaId = CompletableFuture
.supplyAsync( () -> iniciateQuota() );
try {
this.user.setAccountRef(futureAccountRef.get());
this.user.setQuotaId(futureQuotaId.get());
}catch (Exception e) {
log.error("Unexpectet error ocurred", e);
}
this.userRepository.update(this.user);
}
private String createAccount() {
String login = this.user.getLogin();
// some other code
return accountHttpClient.createAccount(login);
}
private String iniciateQuota() {
String login = this.user.getLogin();
// some other code
return quataHttpClient.iniciateQuota();
}
}
// MyConroller.java
@Controller
public class MyConroller {
Logger log = Logger.getLogger(getClass());
@Autowired
private UserService userService;
@RequestMapping("/createUser")
public String createPayment(HttpServletRequest request, Model model) {
String login = request.getParameter("login");
String password = request.getParameter("password");
log.info("Creating user {} with password {}", login, password);
User user = userService.getByLoging(login);
if(user != null) {
model.put("error", "Loging is already taken");
return "myView";
}
if(password == null || password.length() < 5) {
model.put("error", "password is too week");
return "myView";
}
user = new User(login, password);
userService.create(user);
model.put("created", true);
return "myView";
}
}
// User.java
public class User {
private String login;
private String password;
// getters setters
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment