#161 Refactoring a Money Transfer Controller

Companies:
Rostelecom
Themes:
SpringSpring BootTransactionsCode Review

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

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in