From c4d8396bde66ac81731b267747c524c0d9fe98c6 Mon Sep 17 00:00:00 2001 From: Fibinger Ádám <adam.fibinger@wup.hu> Date: Tue, 12 Mar 2019 19:46:19 +0100 Subject: [PATCH] Force example data to UTF-8 --- src/main/java/hu/unr/fiber/cardapi/hibernate/CardInteractor.java | 41 ++++++++++++++++++++++++++++++++++++----- 1 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/main/java/hu/unr/fiber/cardapi/hibernate/CardInteractor.java b/src/main/java/hu/unr/fiber/cardapi/hibernate/CardInteractor.java index e1beedf..b8e72bc 100644 --- a/src/main/java/hu/unr/fiber/cardapi/hibernate/CardInteractor.java +++ b/src/main/java/hu/unr/fiber/cardapi/hibernate/CardInteractor.java @@ -5,6 +5,8 @@ import hu.unr.fiber.cardapi.interfaces.CardInteractorInterface; import hu.unr.fiber.cardapi.interfaces.CardInterface; +import hu.unr.fiber.cardapi.interfaces.CardInvalidUpdateException; +import hu.unr.fiber.cardapi.interfaces.CardNotFoundException; import hu.unr.fiber.cardapi.rest.RestCard; import org.springframework.orm.jpa.JpaObjectRetrievalFailureException; import org.springframework.stereotype.Component; @@ -35,7 +37,7 @@ @Override public CardInterface getCardById(long id) throws CardNotFoundException { if (!this.cardRepository.existsById(id)) { - throw new CardNotFoundException("No such card with id: #"+ id); + throw new CardNotFoundException("No such card with id: #" + id); } Card c = this.cardRepository.getOne(id); @@ -57,19 +59,48 @@ @Override public CardInterface update(long id, CardInterface updatedCard) throws CardInvalidUpdateException, CardNotFoundException { + if (updatedCard.validId() && (updatedCard.getId() != id)) { - throw new CardInvalidUpdateException("Card ID cannot be changed! ", updatedCard); + throw new CardInvalidUpdateException("Card ID cannot be changed! ", + CardInvalidUpdateException.CARD_ID_CHANGE); } - if (this.getCardByNumber(updatedCard.getNumber()) != null) { - throw new CardInvalidUpdateException("Given card number already exists", updatedCard); + Card oldCard = this.getCardByNumber(updatedCard.getNumber()); + + if (oldCard == null) { + oldCard = this.cardRepository.getOne(id); + } else { + if (oldCard.getId() != id) { + //The given card Number is already assigned to an another card with different ID + String message = "Cannot update card number, given card number already assigned to card with ID #" + oldCard.getId(); + throw new CardInvalidUpdateException(message, CardInvalidUpdateException.CARD_NUMBER_ALREADY_ASSIGNED); + } } - this.cardRepository.saveAndFlush((Card) updatedCard); + oldCard.update(updatedCard); + + this.cardRepository.saveAndFlush(oldCard); return this.getCardById(updatedCard.getId()); } + + @Override + public CardInterface create(CardInterface card) throws CardInvalidUpdateException { + Card duplicateNumberedCard = this.getCardByNumber(card.getNumber()); + if (duplicateNumberedCard != null) { + throw new CardInvalidUpdateException("Card with the same number already exists!", + CardInvalidUpdateException.CARD_NUMBER_ALREADY_ASSIGNED); + } + + Card c = new Card(); + c.update(card); + + this.cardRepository.saveAndFlush(c); + + return c; + } + private Card getCardByNumber(String number) { Long id = cardRepository.findIdByNumber(number); -- Gitblit v1.8.0