@Test @Transactional public void testBRedeemGiftCode() { User recipient = _dbInjecter.injectUser("Recipient"); Code code = _codeRepository.findById(1L).orElse(null); Tree tree = _treeRepository.findById(1L).orElse(null); assertEquals(5, tree.getAmount()); assertEquals("Adam", tree.getOwner().getName()); Gift gift = _giftRepository.findById(1L).orElse(null); gift.setStatus(Status.UNREDEEMED); _giftRepository.save(gift); try { _giftService.redeemGiftCode(recipient, code.getCode()); } catch (IpatException e) { fail(String.format("No Exception expected, when redeeming a gift code.\nerrorCode: %s", e.getErrorInfos().get(0).getErrorCode())); } gift = _giftRepository.findById(1L).orElse(null); assertEquals(Status.REDEEMED, gift.getStatus()); tree = _treeRepository.findById(1L).orElse(null); assertEquals(5, tree.getAmount()); assertEquals("Recipient", tree.getOwner().getName()); }
@Transactional public void redeemGiftCode(User recipient, String giftCode) throws IpatException { Gift gift = _giftRepository.findGiftByCode(giftCode); IpatPreconditions.checkNotNull(gift, ErrorCodes.GIFT_IS_NULL); boolean isGiftOnlyCreatedButNotPaid = (gift.getStatus() == Status.NEW); IpatPreconditions.checkArgument(!isGiftOnlyCreatedButNotPaid, ErrorCodes.GIFT_NOT_PAID); boolean isGiftAlreadyRedeemed = (gift.getStatus() == Status.REDEEMED); IpatPreconditions.checkArgument(!isGiftAlreadyRedeemed, ErrorCodes.CODE_ALREADY_REDEEMED); Cart cartToGift = _cartRepository.findCartByCode(giftCode); IpatPreconditions.checkNotNull(cartToGift, ErrorCodes.CART_TO_GIFT_IS_NULL); gift.setRecipient(recipient); gift.setStatus(Status.REDEEMED); for (Tree cartTree : cartToGift.getTrees()) { cartTree.setOwner(recipient); } _cartRepository.save(cartToGift); _giftRepository.save(gift); }
GiftService { @Transactional public void redeemGiftCode(User recipient, String giftCode) throws IpatException { Gift gift = _giftRepository.findGiftByCode(giftCode); IpatPreconditions.checkNotNull(gift, ErrorCodes.GIFT_IS_NULL); boolean isGiftOnlyCreatedButNotPaid = (gift.getStatus() == Status.NEW); IpatPreconditions.checkArgument(!isGiftOnlyCreatedButNotPaid, ErrorCodes.GIFT_NOT_PAID); boolean isGiftAlreadyRedeemed = (gift.getStatus() == Status.REDEEMED); IpatPreconditions.checkArgument(!isGiftAlreadyRedeemed, ErrorCodes.CODE_ALREADY_REDEEMED); Cart cartToGift = _cartRepository.findCartByCode(giftCode); IpatPreconditions.checkNotNull(cartToGift, ErrorCodes.CART_TO_GIFT_IS_NULL); gift.setRecipient(recipient); gift.setStatus(Status.REDEEMED); for (Tree cartTree : cartToGift.getTrees()) { cartTree.setOwner(recipient); } _cartRepository.save(cartToGift); _giftRepository.save(gift); } }
GiftService { @Transactional public void redeemGiftCode(User recipient, String giftCode) throws IpatException { Gift gift = _giftRepository.findGiftByCode(giftCode); IpatPreconditions.checkNotNull(gift, ErrorCodes.GIFT_IS_NULL); boolean isGiftOnlyCreatedButNotPaid = (gift.getStatus() == Status.NEW); IpatPreconditions.checkArgument(!isGiftOnlyCreatedButNotPaid, ErrorCodes.GIFT_NOT_PAID); boolean isGiftAlreadyRedeemed = (gift.getStatus() == Status.REDEEMED); IpatPreconditions.checkArgument(!isGiftAlreadyRedeemed, ErrorCodes.CODE_ALREADY_REDEEMED); Cart cartToGift = _cartRepository.findCartByCode(giftCode); IpatPreconditions.checkNotNull(cartToGift, ErrorCodes.CART_TO_GIFT_IS_NULL); gift.setRecipient(recipient); gift.setStatus(Status.REDEEMED); for (Tree cartTree : cartToGift.getTrees()) { cartTree.setOwner(recipient); } _cartRepository.save(cartToGift); _giftRepository.save(gift); }  }
GiftService { @Transactional public void redeemGiftCode(User recipient, String giftCode) throws IpatException { Gift gift = _giftRepository.findGiftByCode(giftCode); IpatPreconditions.checkNotNull(gift, ErrorCodes.GIFT_IS_NULL); boolean isGiftOnlyCreatedButNotPaid = (gift.getStatus() == Status.NEW); IpatPreconditions.checkArgument(!isGiftOnlyCreatedButNotPaid, ErrorCodes.GIFT_NOT_PAID); boolean isGiftAlreadyRedeemed = (gift.getStatus() == Status.REDEEMED); IpatPreconditions.checkArgument(!isGiftAlreadyRedeemed, ErrorCodes.CODE_ALREADY_REDEEMED); Cart cartToGift = _cartRepository.findCartByCode(giftCode); IpatPreconditions.checkNotNull(cartToGift, ErrorCodes.CART_TO_GIFT_IS_NULL); gift.setRecipient(recipient); gift.setStatus(Status.REDEEMED); for (Tree cartTree : cartToGift.getTrees()) { cartTree.setOwner(recipient); } _cartRepository.save(cartToGift); _giftRepository.save(gift); }  @Transactional Long[] generateGift(User consignor, PlantBag plantBag); @Transactional void redeemGiftCode(User recipient, String giftCode); void createGiftPdf(long giftId, HttpServletResponse response); }
GiftService { @Transactional public void redeemGiftCode(User recipient, String giftCode) throws IpatException { Gift gift = _giftRepository.findGiftByCode(giftCode); IpatPreconditions.checkNotNull(gift, ErrorCodes.GIFT_IS_NULL); boolean isGiftOnlyCreatedButNotPaid = (gift.getStatus() == Status.NEW); IpatPreconditions.checkArgument(!isGiftOnlyCreatedButNotPaid, ErrorCodes.GIFT_NOT_PAID); boolean isGiftAlreadyRedeemed = (gift.getStatus() == Status.REDEEMED); IpatPreconditions.checkArgument(!isGiftAlreadyRedeemed, ErrorCodes.CODE_ALREADY_REDEEMED); Cart cartToGift = _cartRepository.findCartByCode(giftCode); IpatPreconditions.checkNotNull(cartToGift, ErrorCodes.CART_TO_GIFT_IS_NULL); gift.setRecipient(recipient); gift.setStatus(Status.REDEEMED); for (Tree cartTree : cartToGift.getTrees()) { cartTree.setOwner(recipient); } _cartRepository.save(cartToGift); _giftRepository.save(gift); }  @Transactional Long[] generateGift(User consignor, PlantBag plantBag); @Transactional void redeemGiftCode(User recipient, String giftCode); void createGiftPdf(long giftId, HttpServletResponse response);  }