#161 Refactoring a Money Transfer Controller
Perform a code review:
@RestController
public class AccountController {
@Autowired
public AccountDAOImpl accountDAOImpl;
@Autowired
public KafkaNotificationService kafkaNotificationService;
@PutMapping("/transfer")
public Long transferMoney(@RequestParam Double money, @RequestParam long fromId, @RequestParam long toId) {
var accountFrom = accountDAOImpl.getAccountById(fromId);
var accountTo = accountDAOImpl.getAccountById(toId);
if (accountFrom.balance - money < 0) {
throw new RuntimeException();
}
accountFrom.balance = accountFrom.balance - money;
accountTo.balance = accountTo.balance + money;
long transactionId = accountDAOImpl.transfer(accountFrom, accountTo);
kafkaNotificationService.export(transactionId);
return transactionId;
}
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor