@Test public void purchase() { final EbeanServer ebeanServer = new EbeanTesting().getEbeanServer(); PlayerProfileRepository playerProfileRepository = new CachingPlayerRepository(ebeanServer); PlayerPurchaseService service = new PlayerPurchaseService(playerProfileRepository); UUID uuid = UUID.randomUUID(); PlayerProfile playerProfile = new PlayerProfile(uuid, "Name"); playerProfile.deposit(100); playerProfileRepository.save(playerProfile); playerProfileRepository.put(uuid, playerProfile); service.purchase(uuid, new Purchasable() { @Override public int getId() { return 1; } @Override public long getPrice() { return 5; } @Override public String getDescription() { return "desc"; } }); Optional<PlayerProfile> optionalResult = playerProfileRepository.find(new UUIDSpecification(uuid), PlayerAssociation.PURCHASES); assertTrue(optionalResult.isPresent()); PlayerProfile profile = optionalResult.get(); assertTrue(profile.getPurchases().hasPurchase(1)); }
@Transactional public void purchase(UUID purchaser, Purchasable purchasable) { PlayerProfile playerProfile = playerRepository.get(purchaser); if (playerProfile.getPurchases().hasPurchase(purchasable.getId())) { throw new AlreadyPurchasedException(); } playerProfile.withdraw(purchasable.getPrice()); playerProfile.getPurchases().addPurchase(purchasable.getId(), new Purchase(purchasable.getPrice(), purchasable.getId(), purchasable.getDescription(), playerProfile)); playerRepository.update(playerProfile); }
PlayerPurchaseService { @Transactional public void purchase(UUID purchaser, Purchasable purchasable) { PlayerProfile playerProfile = playerRepository.get(purchaser); if (playerProfile.getPurchases().hasPurchase(purchasable.getId())) { throw new AlreadyPurchasedException(); } playerProfile.withdraw(purchasable.getPrice()); playerProfile.getPurchases().addPurchase(purchasable.getId(), new Purchase(purchasable.getPrice(), purchasable.getId(), purchasable.getDescription(), playerProfile)); playerRepository.update(playerProfile); } }
PlayerPurchaseService { @Transactional public void purchase(UUID purchaser, Purchasable purchasable) { PlayerProfile playerProfile = playerRepository.get(purchaser); if (playerProfile.getPurchases().hasPurchase(purchasable.getId())) { throw new AlreadyPurchasedException(); } playerProfile.withdraw(purchasable.getPrice()); playerProfile.getPurchases().addPurchase(purchasable.getId(), new Purchase(purchasable.getPrice(), purchasable.getId(), purchasable.getDescription(), playerProfile)); playerRepository.update(playerProfile); }  }
PlayerPurchaseService { @Transactional public void purchase(UUID purchaser, Purchasable purchasable) { PlayerProfile playerProfile = playerRepository.get(purchaser); if (playerProfile.getPurchases().hasPurchase(purchasable.getId())) { throw new AlreadyPurchasedException(); } playerProfile.withdraw(purchasable.getPrice()); playerProfile.getPurchases().addPurchase(purchasable.getId(), new Purchase(purchasable.getPrice(), purchasable.getId(), purchasable.getDescription(), playerProfile)); playerRepository.update(playerProfile); }  @Transactional void purchase(UUID purchaser, Purchasable purchasable); }
PlayerPurchaseService { @Transactional public void purchase(UUID purchaser, Purchasable purchasable) { PlayerProfile playerProfile = playerRepository.get(purchaser); if (playerProfile.getPurchases().hasPurchase(purchasable.getId())) { throw new AlreadyPurchasedException(); } playerProfile.withdraw(purchasable.getPrice()); playerProfile.getPurchases().addPurchase(purchasable.getId(), new Purchase(purchasable.getPrice(), purchasable.getId(), purchasable.getDescription(), playerProfile)); playerRepository.update(playerProfile); }  @Transactional void purchase(UUID purchaser, Purchasable purchasable);  }