Skip to content

Commit

Permalink
Add Transactional annotation for atomic operations
Browse files Browse the repository at this point in the history
  • Loading branch information
bifrurcated committed Jan 12, 2024
1 parent 6301596 commit e1f6c99
Showing 1 changed file with 3 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.bifurcated.wallet.errors.WalletNotFoundError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;
import java.util.UUID;
Expand All @@ -20,12 +21,14 @@ public WalletService(WalletRepo walletRepo) {
this.walletRepo = walletRepo;
}

@Transactional
public Wallet addAmount(UUID id, Float amount) {
var wallet = walletRepo.findById(id).orElseThrow(WalletNotFoundError::new);
wallet.setAmount(wallet.getAmount() + amount);
return walletRepo.save(wallet);
}

@Transactional
public Wallet reduceAmount(UUID id, Float amount) {
var wallet = walletRepo.findById(id).orElseThrow(WalletNotFoundError::new);
var reduce = wallet.getAmount() - amount;
Expand Down

0 comments on commit e1f6c99

Please sign in to comment.