@Test @Transactional public void testRedeemEventCode() { String codeString = prepareDbAndReturnCodeString(); assertEquals(1, _cartRepository.count()); assertEquals(1, _eventRepository.count()); assertEquals(1, _codeRepository.count()); assertEquals(CartState.GENERATED, _cartRepository.findById(1L).orElse(null).getCartState()); Tree tree = _treeRepository.findById(1L).orElse(null); assertEquals(5, tree.getAmount()); assertEquals("eventUser", tree.getOwner().getName()); User redeemer = _dbInjecter.injectUser("redeemer"); try { _eventService.redeemEventCode(redeemer, codeString); } catch (IpatException e) { fail(String.format("No Exception expected, when redeeming a gift code.\nerrorCode: %s", e.getErrorInfos().get(0).getErrorCode())); } assertEquals(CartState.VERIFIED, _cartRepository.findById(1L).orElse(null).getCartState()); tree = _treeRepository.findById(1L).orElse(null); assertEquals(5, tree.getAmount()); assertEquals("redeemer", tree.getOwner().getName()); }
@Transactional public void redeemEventCode(User recipient, String eventCode) throws IpatException { Cart cartForEventCode = _cartRepository.findCartByCode(eventCode); Team team = null; if (null == cartForEventCode) { Code code = _codeRepository.findByCode(eventCode); IpatPreconditions.checkNotNull(code, ErrorCodes.INVALID_CODE); IpatPreconditions.checkNotNull(code.getEvent(), ErrorCodes.EVENT_IS_NULL); cartForEventCode = _cartRepository.findOneCartByEventAndGenerated(code.getEvent().getId()); IpatPreconditions.checkNotNull(cartForEventCode, ErrorCodes.CART_TO_EVENT_CODE_IS_NULL); cartForEventCode.setCode(code); code.setCart(cartForEventCode); team = code.getEvent().getTeam(); } else { IpatPreconditions.checkNotNull(cartForEventCode, ErrorCodes.CART_TO_EVENT_CODE_IS_NULL); } boolean isCartAlreadyVerified = cartForEventCode.getCartState().equals(CartState.VERIFIED); IpatPreconditions.checkArgument(!isCartAlreadyVerified, ErrorCodes.CODE_ALREADY_REDEEMED); cartForEventCode.setCartState(CartState.VERIFIED); for (Tree cartTree : cartForEventCode.getTrees()) { cartTree.setOwner(recipient); } _cartRepository.save(cartForEventCode); if (null != team && null == recipient.getTeam()) { _teamService.joinTeam(recipient.getId(), team.getId()); } }
EventService { @Transactional public void redeemEventCode(User recipient, String eventCode) throws IpatException { Cart cartForEventCode = _cartRepository.findCartByCode(eventCode); Team team = null; if (null == cartForEventCode) { Code code = _codeRepository.findByCode(eventCode); IpatPreconditions.checkNotNull(code, ErrorCodes.INVALID_CODE); IpatPreconditions.checkNotNull(code.getEvent(), ErrorCodes.EVENT_IS_NULL); cartForEventCode = _cartRepository.findOneCartByEventAndGenerated(code.getEvent().getId()); IpatPreconditions.checkNotNull(cartForEventCode, ErrorCodes.CART_TO_EVENT_CODE_IS_NULL); cartForEventCode.setCode(code); code.setCart(cartForEventCode); team = code.getEvent().getTeam(); } else { IpatPreconditions.checkNotNull(cartForEventCode, ErrorCodes.CART_TO_EVENT_CODE_IS_NULL); } boolean isCartAlreadyVerified = cartForEventCode.getCartState().equals(CartState.VERIFIED); IpatPreconditions.checkArgument(!isCartAlreadyVerified, ErrorCodes.CODE_ALREADY_REDEEMED); cartForEventCode.setCartState(CartState.VERIFIED); for (Tree cartTree : cartForEventCode.getTrees()) { cartTree.setOwner(recipient); } _cartRepository.save(cartForEventCode); if (null != team && null == recipient.getTeam()) { _teamService.joinTeam(recipient.getId(), team.getId()); } } }
EventService { @Transactional public void redeemEventCode(User recipient, String eventCode) throws IpatException { Cart cartForEventCode = _cartRepository.findCartByCode(eventCode); Team team = null; if (null == cartForEventCode) { Code code = _codeRepository.findByCode(eventCode); IpatPreconditions.checkNotNull(code, ErrorCodes.INVALID_CODE); IpatPreconditions.checkNotNull(code.getEvent(), ErrorCodes.EVENT_IS_NULL); cartForEventCode = _cartRepository.findOneCartByEventAndGenerated(code.getEvent().getId()); IpatPreconditions.checkNotNull(cartForEventCode, ErrorCodes.CART_TO_EVENT_CODE_IS_NULL); cartForEventCode.setCode(code); code.setCart(cartForEventCode); team = code.getEvent().getTeam(); } else { IpatPreconditions.checkNotNull(cartForEventCode, ErrorCodes.CART_TO_EVENT_CODE_IS_NULL); } boolean isCartAlreadyVerified = cartForEventCode.getCartState().equals(CartState.VERIFIED); IpatPreconditions.checkArgument(!isCartAlreadyVerified, ErrorCodes.CODE_ALREADY_REDEEMED); cartForEventCode.setCartState(CartState.VERIFIED); for (Tree cartTree : cartForEventCode.getTrees()) { cartTree.setOwner(recipient); } _cartRepository.save(cartForEventCode); if (null != team && null == recipient.getTeam()) { _teamService.joinTeam(recipient.getId(), team.getId()); } }  }
EventService { @Transactional public void redeemEventCode(User recipient, String eventCode) throws IpatException { Cart cartForEventCode = _cartRepository.findCartByCode(eventCode); Team team = null; if (null == cartForEventCode) { Code code = _codeRepository.findByCode(eventCode); IpatPreconditions.checkNotNull(code, ErrorCodes.INVALID_CODE); IpatPreconditions.checkNotNull(code.getEvent(), ErrorCodes.EVENT_IS_NULL); cartForEventCode = _cartRepository.findOneCartByEventAndGenerated(code.getEvent().getId()); IpatPreconditions.checkNotNull(cartForEventCode, ErrorCodes.CART_TO_EVENT_CODE_IS_NULL); cartForEventCode.setCode(code); code.setCart(cartForEventCode); team = code.getEvent().getTeam(); } else { IpatPreconditions.checkNotNull(cartForEventCode, ErrorCodes.CART_TO_EVENT_CODE_IS_NULL); } boolean isCartAlreadyVerified = cartForEventCode.getCartState().equals(CartState.VERIFIED); IpatPreconditions.checkArgument(!isCartAlreadyVerified, ErrorCodes.CODE_ALREADY_REDEEMED); cartForEventCode.setCartState(CartState.VERIFIED); for (Tree cartTree : cartForEventCode.getTrees()) { cartTree.setOwner(recipient); } _cartRepository.save(cartForEventCode); if (null != team && null == recipient.getTeam()) { _teamService.joinTeam(recipient.getId(), team.getId()); } }  @Transactional void redeemEventCode(User recipient, String eventCode); }
EventService { @Transactional public void redeemEventCode(User recipient, String eventCode) throws IpatException { Cart cartForEventCode = _cartRepository.findCartByCode(eventCode); Team team = null; if (null == cartForEventCode) { Code code = _codeRepository.findByCode(eventCode); IpatPreconditions.checkNotNull(code, ErrorCodes.INVALID_CODE); IpatPreconditions.checkNotNull(code.getEvent(), ErrorCodes.EVENT_IS_NULL); cartForEventCode = _cartRepository.findOneCartByEventAndGenerated(code.getEvent().getId()); IpatPreconditions.checkNotNull(cartForEventCode, ErrorCodes.CART_TO_EVENT_CODE_IS_NULL); cartForEventCode.setCode(code); code.setCart(cartForEventCode); team = code.getEvent().getTeam(); } else { IpatPreconditions.checkNotNull(cartForEventCode, ErrorCodes.CART_TO_EVENT_CODE_IS_NULL); } boolean isCartAlreadyVerified = cartForEventCode.getCartState().equals(CartState.VERIFIED); IpatPreconditions.checkArgument(!isCartAlreadyVerified, ErrorCodes.CODE_ALREADY_REDEEMED); cartForEventCode.setCartState(CartState.VERIFIED); for (Tree cartTree : cartForEventCode.getTrees()) { cartTree.setOwner(recipient); } _cartRepository.save(cartForEventCode); if (null != team && null == recipient.getTeam()) { _teamService.joinTeam(recipient.getId(), team.getId()); } }  @Transactional void redeemEventCode(User recipient, String eventCode);  }