Fibinger Ádám
2019-03-12 12abae6da924cb33858fbc9948fdf7a181e2038c
Decoupling final version
1 files renamed
6 files modified
1 files deleted
1 files added
130 ■■■■ changed files
src/main/java/hu/unr/fiber/cardapi/CardController.java 50 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/hibernate/CardInteractor.java 39 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/hibernate/CardInvalidUpdateException.java 15 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardCreateInterface.java 1 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardDeleteInterface.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardInteractorInterface.java 3 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardInvalidUpdateException.java 16 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardNotFoundException.java 2 ●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/rest/RestCard.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/CardController.java
@@ -1,16 +1,20 @@
package hu.unr.fiber.cardapi;
import java.net.URI;
import java.util.List;
import hu.unr.fiber.cardapi.hibernate.CardNotFoundException;
import hu.unr.fiber.cardapi.interfaces.CardInvalidUpdateException;
import hu.unr.fiber.cardapi.interfaces.CardNotFoundException;
import hu.unr.fiber.cardapi.interfaces.CardInterface;
import hu.unr.fiber.cardapi.hibernate.CardInteractor;
import hu.unr.fiber.cardapi.rest.RestCard;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.util.UriComponentsBuilder;
@RestController
public class CardController {
@@ -58,7 +62,7 @@
        return ResponseEntity.ok().body("OK");
    }
/*
    @PostMapping(value = "/card/{id}")
    public ResponseEntity<String> updateCard(@PathVariable("id") long id, @RequestBody RestCard updatedCardEntity, UriComponentsBuilder ucBuilder) {
@@ -66,49 +70,24 @@
        try {
            this.cardService.update(id, updatedCardEntity);
        } catch (CardInvalidUpdateException e){
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
        } catch (CardNotFoundException e) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No such card with id: #" + id, e);
        }
        RestCard originalCardEntity = this.cardRepository.getOne(id);
        if (originalCardEntity.equals(updatedCardEntity)) {
            return ResponseEntity.accepted().body("Update has no changes.");
        }
        //card number updated, we have to check if its already exists
        if (!originalCardEntity.getNumber().equals(updatedCardEntity.getNumber()) && this.getCardByNumber(updatedCardEntity.getNumber()) != null) {
            logger.error("Unable to update card with id {}. A different RestCard with number {} already exist", originalCardEntity.getId(), updatedCardEntity.getNumber());
            return ResponseEntity.status(HttpStatus.CONFLICT).body("RestCard with number " + updatedCardEntity.getNumber() + " already exists.");
        }
        originalCardEntity.update(updatedCardEntity);
        this.cardRepository.flush();
        return ResponseEntity.accepted().body("OK");
    }
    private RestCard getCardByNumber(String number) {
        Long id = cardRepository.findIdByNumber(number);
        if (id == null) {
            return null;
        }
        return cardRepository.getOne(id);
    }
    @PostMapping(value = "/card")
    public ResponseEntity<String> createCard(@RequestBody RestCard card, UriComponentsBuilder ucBuilder) {
        logger.info("Creating RestCard : {}", card.getNumber());
        if (this.getCardByNumber(card.getNumber()) != null) {
            logger.error("Unable to create. A RestCard with number {} already exist", card.getNumber());
            return ResponseEntity.status(HttpStatus.CONFLICT).body("RestCard with number " + card.getNumber() + " already exists.");
        try {
            CardInterface c = this.cardService.create(card);
            card.setId(c.getId());
        } catch (CardInvalidUpdateException e) {
            throw new ResponseStatusException(HttpStatus.CONFLICT, e.getMessage());
        }
        this.cardRepository.saveAndFlush(card);
        URI newCardURI = ucBuilder.path("/card/{id}").buildAndExpand(card.getId()).toUri();
@@ -116,5 +95,4 @@
                .created(newCardURI)
                .body(newCardURI.toString());
    }
    */
}
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;
@@ -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);
src/main/java/hu/unr/fiber/cardapi/hibernate/CardInvalidUpdateException.java
File was deleted
src/main/java/hu/unr/fiber/cardapi/interfaces/CardCreateInterface.java
@@ -1,4 +1,5 @@
package hu.unr.fiber.cardapi.interfaces;
public interface CardCreateInterface {
    public CardInterface create(CardInterface card) throws Exception;
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardDeleteInterface.java
@@ -1,7 +1,5 @@
package hu.unr.fiber.cardapi.interfaces;
import hu.unr.fiber.cardapi.hibernate.CardNotFoundException;
public interface CardDeleteInterface {
    public void delete(long id) throws CardNotFoundException;
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardInteractorInterface.java
@@ -4,6 +4,7 @@
        CardByIdInterface,
        CardListBoundaryInterface,
        CardDeleteInterface,
        CardModifyInterface {
        CardModifyInterface,
        CardCreateInterface {
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardInvalidUpdateException.java
New file
@@ -0,0 +1,16 @@
package hu.unr.fiber.cardapi.interfaces;
public class CardInvalidUpdateException extends Exception {
    public static final int CARD_ID_CHANGE = 0;
    public static final int CARD_NUMBER_ALREADY_ASSIGNED = 1;
    protected int code;
    public CardInvalidUpdateException(String message, int code) {
        super(message);
        this.code = code;
    }
    public int getCode() {
        return code;
    }
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardNotFoundException.java
File was renamed from src/main/java/hu/unr/fiber/cardapi/hibernate/CardNotFoundException.java
@@ -1,4 +1,4 @@
package hu.unr.fiber.cardapi.hibernate;
package hu.unr.fiber.cardapi.interfaces;
public class CardNotFoundException extends Exception {
    public CardNotFoundException(String message) {
src/main/java/hu/unr/fiber/cardapi/rest/RestCard.java
@@ -1,5 +1,6 @@
package hu.unr.fiber.cardapi.rest;
import com.fasterxml.jackson.annotation.JsonCreator;
import hu.unr.fiber.cardapi.interfaces.CardInterface;
import java.util.Objects;
@@ -24,6 +25,7 @@
                .setNumber(number);
    }
    @JsonCreator
    public RestCard(long id, String name, String number, String cardHolder) {
        this.setId(id)
                .setName(name)