Fibinger Ádám
2019-03-03 464000b3acc64e678a57141c2eb806a5b3d2a01c
Basic refactor and decoupling REST Controller from JPA
1 files deleted
6 files added
2 files renamed
1 files modified
291 ■■■■ changed files
src/main/java/hu/unr/fiber/cardapi/CardController.java 61 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/CardList.java 6 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/entity/Card.java 45 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/entity/CardByIdInterface.java 5 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/entity/CardEntityInterface.java 21 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/entity/CardEntityNotFoundException.java 7 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/entity/CardInteractor.java 43 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/entity/CardListBoundaryInterface.java 7 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/entity/CardRepository.java 2 ●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/rest/CardEntity.java 94 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/CardController.java
@@ -4,6 +4,11 @@
import java.util.List;
import java.util.Optional;
import hu.unr.fiber.cardapi.entity.CardEntityInterface;
import hu.unr.fiber.cardapi.entity.CardEntityNotFoundException;
import hu.unr.fiber.cardapi.entity.CardInteractor;
import hu.unr.fiber.cardapi.entity.CardRepository;
import hu.unr.fiber.cardapi.rest.CardEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
@@ -14,13 +19,11 @@
@RestController
public class CardController {
    Logger logger = LoggerFactory.getLogger(CardController.class);
    private CardInteractor cardService;
    CardController(CardRepository cR)
    {
        this.cardRepository = cR;
    CardController(CardInteractor cS) {
        this.cardService = cS;
    }
    CardRepository cardRepository;
    @GetMapping("/")
    public String index() {
@@ -29,24 +32,25 @@
    }
    @GetMapping("/cards")
    public List<Card> cards() {
    public ResponseEntity<List<CardEntityInterface>> cards() {
        logger.info("GET /cards called, responded with items.");
        return cardRepository.findAll();
        List<CardEntityInterface> cardList = cardService.getCardList();
        return ResponseEntity.ok().body(cardList);
    }
    @GetMapping(value = "/card/{id}")
    public ResponseEntity<Card> getCard(@PathVariable("id") long id) {
    public ResponseEntity<CardEntityInterface> getCard(@PathVariable("id") long id) {
        logger.info("GET /card/" + id + " called.");
        if (!this.cardRepository.existsById(id)) {
        CardEntityInterface card = this.cardService.getCardById(id);
        if (card == null) {
            return ResponseEntity.notFound().build();
        }
        Card card = this.cardRepository.getOne(id);
        return ResponseEntity.ok().body(card);
    }
/*
    @DeleteMapping(value = "/card/{id}")
    public ResponseEntity<String> deleteCard(@PathVariable("id") long id) {
        logger.info("DELETE /card/" + id + " called");
@@ -60,40 +64,39 @@
                            return ResponseEntity.ok().body("OK");
                        }
                )
                .orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).body("No card available with id: "+ id));
                .orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).body("No card available with id: " + id));
    }
    @PostMapping(value = "/card/{id}")
    public ResponseEntity<String> updateCard(@PathVariable("id") long id, @RequestBody Card updatedCard, UriComponentsBuilder ucBuilder) {
    public ResponseEntity<String> updateCard(@PathVariable("id") long id, @RequestBody CardEntity updatedCardEntity, UriComponentsBuilder ucBuilder) {
        logger.info("POST /card/"+ id + " called, card update.");
        logger.info("POST /card/" + id + " called, card update.");
        if (updatedCard.validId() && (updatedCard.getId() != id))
        {
        if (updatedCardEntity.validId() && (updatedCardEntity.getId() != id)) {
            return ResponseEntity
                    .badRequest()
                    .body("Id field cannot be modified.");
        }
        Card originalCard = this.cardRepository.getOne(id);
        CardEntity originalCardEntity = this.cardRepository.getOne(id);
        if (originalCard.equals(updatedCard)) {
        if (originalCardEntity.equals(updatedCardEntity)) {
            return ResponseEntity.accepted().body("Update has no changes.");
        }
        //card number updated, we have to check if its already exists
        if (!originalCard.getNumber().equals(updatedCard.getNumber()) && this.getCardByNumber(updatedCard.getNumber()) != null) {
            logger.error("Unable to update card with id {}. A different Card with number {} already exist", originalCard.getId(), updatedCard.getNumber());
            return ResponseEntity.status(HttpStatus.CONFLICT).body("Card with number " + updatedCard.getNumber() + " already exists.");
        if (!originalCardEntity.getNumber().equals(updatedCardEntity.getNumber()) && this.getCardByNumber(updatedCardEntity.getNumber()) != null) {
            logger.error("Unable to update card with id {}. A different CardEntity with number {} already exist", originalCardEntity.getId(), updatedCardEntity.getNumber());
            return ResponseEntity.status(HttpStatus.CONFLICT).body("CardEntity with number " + updatedCardEntity.getNumber() + " already exists.");
        }
        originalCard.update(updatedCard);
        originalCardEntity.update(updatedCardEntity);
        this.cardRepository.flush();
        return ResponseEntity.accepted().body("OK");
    }
    private Card getCardByNumber(String number) {
    private CardEntity getCardByNumber(String number) {
        Long id = cardRepository.findIdByNumber(number);
@@ -105,12 +108,12 @@
    }
    @PostMapping(value = "/card")
    public ResponseEntity<String> createCard(@RequestBody Card card, UriComponentsBuilder ucBuilder) {
        logger.info("Creating Card : {}", card.getNumber());
    public ResponseEntity<String> createCard(@RequestBody CardEntity card, UriComponentsBuilder ucBuilder) {
        logger.info("Creating CardEntity : {}", card.getNumber());
        if (this.getCardByNumber(card.getNumber()) != null) {
            logger.error("Unable to create. A Card with number {} already exist", card.getNumber());
            return ResponseEntity.status(HttpStatus.CONFLICT).body("Card with number "+ card.getNumber() + " already exists.");
            logger.error("Unable to create. A CardEntity with number {} already exist", card.getNumber());
            return ResponseEntity.status(HttpStatus.CONFLICT).body("CardEntity with number " + card.getNumber() + " already exists.");
        }
        this.cardRepository.saveAndFlush(card);
@@ -121,5 +124,5 @@
                .created(newCardURI)
                .body(newCardURI.toString());
    }
*/
}
src/main/java/hu/unr/fiber/cardapi/CardList.java
File was deleted
src/main/java/hu/unr/fiber/cardapi/entity/Card.java
File was renamed from src/main/java/hu/unr/fiber/cardapi/Card.java
@@ -1,14 +1,11 @@
package hu.unr.fiber.cardapi;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
package hu.unr.fiber.cardapi.entity;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "Card")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Card {
public class Card implements CardEntityInterface {
    public static final long INVALID_ID = 0;
    @Id
@@ -17,23 +14,29 @@
    private long id = INVALID_ID;
    private String name;
    //FIXME: @Pattern([0-9]+)
    @Column(unique=true)
    @Column(unique = true)
    private String number;
    private String cardHolder;
    public Card(){
    public Card() {
        super();
    }
    public Card(long id) {
        this.id = id;
        this.setId(id);
    }
    public Card(long id, String name, String number)
    {
        this.id = id;
        this.name = name;
        this.number = number;
    public Card(long id, String name, String number) {
        this.setId(id)
                .setName(name)
                .setNumber(number);
    }
    public Card(long id, String name, String number, String cardHolder) {
        this.setId(id)
                .setName(name)
                .setNumber(number)
                .setCardHolder(cardHolder);
    }
    public long getId() {
@@ -44,32 +47,36 @@
        return (this.getId() != Card.INVALID_ID);
    }
    public void setId(long id) {
    public Card setId(long id) {
        this.id = id;
        return this;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
    public Card setName(String name) {
        this.name = name;
        return this;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
    public Card setNumber(String number) {
        this.number = number;
        return this;
    }
    public String getCardHolder() {
        return cardHolder;
    }
    public void setCardHolder(String cardHolder) {
    public Card setCardHolder(String cardHolder) {
        this.cardHolder = cardHolder;
        return this;
    }
    public boolean equals(Object o) {
@@ -90,6 +97,6 @@
    @Override
    public String toString() {
        return "#" + this.number + " - " +  this.name + " @ " + this.cardHolder;
        return "#" + this.number + " - " + this.name + " @ " + this.cardHolder;
    }
}
}
src/main/java/hu/unr/fiber/cardapi/entity/CardByIdInterface.java
New file
@@ -0,0 +1,5 @@
package hu.unr.fiber.cardapi.entity;
public interface CardByIdInterface {
    public CardEntityInterface getCardById(long id);
}
src/main/java/hu/unr/fiber/cardapi/entity/CardEntityInterface.java
New file
@@ -0,0 +1,21 @@
package hu.unr.fiber.cardapi.entity;
public interface CardEntityInterface {
    public long getId();
    public boolean validId();
    public CardEntityInterface setId(long id);
    public String getName();
    public CardEntityInterface setName(String name);
    public String getNumber();
    public CardEntityInterface setNumber(String number);
    public String getCardHolder();
    public CardEntityInterface setCardHolder(String cardHolder);
}
src/main/java/hu/unr/fiber/cardapi/entity/CardEntityNotFoundException.java
New file
@@ -0,0 +1,7 @@
package hu.unr.fiber.cardapi.entity;
public class CardEntityNotFoundException extends Exception {
    CardEntityNotFoundException(String message) {
        super(message);
    }
}
src/main/java/hu/unr/fiber/cardapi/entity/CardInteractor.java
New file
@@ -0,0 +1,43 @@
package hu.unr.fiber.cardapi.entity;
import java.util.ArrayList;
import java.util.List;
import hu.unr.fiber.cardapi.rest.CardEntity;
import org.springframework.stereotype.Component;
@Component
public class CardInteractor implements CardListBoundaryInterface, CardByIdInterface {
    private CardRepository cardRepository;
    CardInteractor(CardRepository cardRepository) {
        this.cardRepository = cardRepository;
    }
    @Override
    public List<CardEntityInterface> getCardList() {
        List<Card> cardList = this.cardRepository.findAll();
        List<CardEntityInterface> restCardList = new ArrayList<>();
        for (Card card : cardList) {
            CardEntity ce = new CardEntity(card.getId());
            restCardList.add(ce.update(card));
        }
        return restCardList;
    }
    @Override
    public CardEntityInterface getCardById(long id) {
        if (!this.cardRepository.existsById(id)) {
            return null;
        }
        Card c = this.cardRepository.getOne(id);
        CardEntity ce = new CardEntity(c.getId());
        return ce.update(c);
    }
}
src/main/java/hu/unr/fiber/cardapi/entity/CardListBoundaryInterface.java
New file
@@ -0,0 +1,7 @@
package hu.unr.fiber.cardapi.entity;
import java.util.List;
public interface CardListBoundaryInterface {
    public List<CardEntityInterface> getCardList();
}
src/main/java/hu/unr/fiber/cardapi/entity/CardRepository.java
File was renamed from src/main/java/hu/unr/fiber/cardapi/CardRepository.java
@@ -1,4 +1,4 @@
package hu.unr.fiber.cardapi;
package hu.unr.fiber.cardapi.entity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
src/main/java/hu/unr/fiber/cardapi/rest/CardEntity.java
New file
@@ -0,0 +1,94 @@
package hu.unr.fiber.cardapi.rest;
import hu.unr.fiber.cardapi.entity.CardEntityInterface;
import java.util.Objects;
public class CardEntity implements CardEntityInterface {
    public static final long INVALID_ID = 0;
    private long id = INVALID_ID;
    private String name;
    //FIXME: @Pattern([0-9]+)
    private String number;
    private String cardHolder;
    public CardEntity(long id) {
        this.id = id;
    }
    public CardEntity(long id, String name, String number) {
        this.setId(id)
                .setName(name)
                .setNumber(number);
    }
    public CardEntity(long id, String name, String number, String cardHolder) {
        this.setId(id)
                .setName(name)
                .setNumber(number)
                .setCardHolder(cardHolder);
    }
    public long getId() {
        return id;
    }
    public boolean validId() {
        return (this.getId() != CardEntity.INVALID_ID);
    }
    public CardEntity setId(long id) {
        this.id = id;
        return this;
    }
    public String getName() {
        return name;
    }
    public CardEntity setName(String name) {
        this.name = name;
        return this;
    }
    public String getNumber() {
        return number;
    }
    public CardEntity setNumber(String number) {
        this.number = number;
        return this;
    }
    public String getCardHolder() {
        return cardHolder;
    }
    public CardEntity setCardHolder(String cardHolder) {
        this.cardHolder = cardHolder;
        return this;
    }
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CardEntity card = (CardEntity) o;
        return id == card.id &&
                name.equals(card.name) &&
                number.equals(card.number) &&
                Objects.equals(cardHolder, card.cardHolder);
    }
    public CardEntity update(CardEntityInterface otherCardEntity) {
        this.setName(otherCardEntity.getName());
        this.setNumber(otherCardEntity.getNumber());
        this.setCardHolder(otherCardEntity.getCardHolder());
        return this;
    }
    public String toString() {
        return "#" + this.number + " - " + this.name + " @ " + this.cardHolder;
    }
}