Fibinger Ádám
10 days ago 94fead6be729cfb23b657d853c5499709a3d27c4
Just keeping calm and keep refactoring
5 files deleted
11 files added
3 files renamed
1 files modified
382 ■■■■■ changed files
src/main/java/hu/unr/fiber/cardapi/CardController.java 88 ●●●● 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/hibernate/Card.java 10 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/hibernate/CardInteractor.java 83 ●●●●● 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/hibernate/CardNotFoundException.java 7 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/hibernate/CardRepository.java 2 ●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardByIdInterface.java 5 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardCreateInterface.java 4 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardDeleteInterface.java 7 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardInteractorInterface.java 9 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardInterface.java 23 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardListBoundaryInterface.java 7 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardModifyInterface.java 5 ●●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/interfaces/CardNotFoundExceptionInterface.java 4 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/rest/RestCard.java 30 ●●●● patch | view | raw | blame | history
src/main/java/hu/unr/fiber/cardapi/CardController.java
@@ -1,20 +1,16 @@
package hu.unr.fiber.cardapi;
import java.net.URI;
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 hu.unr.fiber.cardapi.hibernate.CardNotFoundException;
import hu.unr.fiber.cardapi.interfaces.CardInterface;
import hu.unr.fiber.cardapi.hibernate.CardInteractor;
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.util.UriComponentsBuilder;
import org.springframework.web.server.ResponseStatusException;
@RestController
public class CardController {
@@ -32,53 +28,49 @@
    }
    @GetMapping("/cards")
    public ResponseEntity<List<CardEntityInterface>> cards() {
    public ResponseEntity<List<CardInterface>> cards() {
        logger.info("GET /cards called, responded with items.");
        List<CardEntityInterface> cardList = cardService.getCardList();
        List<CardInterface> cardList = cardService.getCardList();
        return ResponseEntity.ok().body(cardList);
    }
    @GetMapping(value = "/card/{id}")
    public ResponseEntity<CardEntityInterface> getCard(@PathVariable("id") long id) {
    public ResponseEntity<CardInterface> getCard(@PathVariable("id") long id) {
        logger.info("GET /card/" + id + " called.");
        CardEntityInterface card = this.cardService.getCardById(id);
        if (card == null) {
            return ResponseEntity.notFound().build();
        try {
            CardInterface card = this.cardService.getCardById(id);
            return ResponseEntity.ok().body(card);
        } catch (CardNotFoundException e) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No such card with id: #" + id);
        }
        return ResponseEntity.ok().body(card);
    }
/*
    @DeleteMapping(value = "/card/{id}")
    public ResponseEntity<String> deleteCard(@PathVariable("id") long id) {
        logger.info("DELETE /card/" + id + " called");
        return Optional
                .ofNullable(this.cardRepository.getOne(id))
                .map(
                        card -> {
                            this.cardRepository.delete(card);
                            this.cardRepository.flush();
                            return ResponseEntity.ok().body("OK");
                        }
                )
                .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 CardEntity updatedCardEntity, UriComponentsBuilder ucBuilder) {
        logger.info("POST /card/" + id + " called, card update.");
        if (updatedCardEntity.validId() && (updatedCardEntity.getId() != id)) {
            return ResponseEntity
                    .badRequest()
                    .body("Id field cannot be modified.");
        try {
            this.cardService.delete(id);
        } catch (CardNotFoundException e) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No such card with id: #" + id, e);
        }
        CardEntity originalCardEntity = this.cardRepository.getOne(id);
        return ResponseEntity.ok().body("OK");
    }
/*
    @PostMapping(value = "/card/{id}")
    public ResponseEntity<String> updateCard(@PathVariable("id") long id, @RequestBody RestCard updatedCardEntity, UriComponentsBuilder ucBuilder) {
        logger.info("POST /card/" + id + " called, card update.");
        try {
            this.cardService.update(id, updatedCardEntity);
        } catch (CardInvalidUpdateException e){
        }
        RestCard originalCardEntity = this.cardRepository.getOne(id);
        if (originalCardEntity.equals(updatedCardEntity)) {
            return ResponseEntity.accepted().body("Update has no changes.");
@@ -86,8 +78,8 @@
        //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 CardEntity with number {} already exist", originalCardEntity.getId(), updatedCardEntity.getNumber());
            return ResponseEntity.status(HttpStatus.CONFLICT).body("CardEntity with number " + updatedCardEntity.getNumber() + " already exists.");
            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);
@@ -96,7 +88,7 @@
        return ResponseEntity.accepted().body("OK");
    }
    private CardEntity getCardByNumber(String number) {
    private RestCard getCardByNumber(String number) {
        Long id = cardRepository.findIdByNumber(number);
@@ -108,12 +100,12 @@
    }
    @PostMapping(value = "/card")
    public ResponseEntity<String> createCard(@RequestBody CardEntity card, UriComponentsBuilder ucBuilder) {
        logger.info("Creating CardEntity : {}", card.getNumber());
    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 CardEntity with number {} already exist", card.getNumber());
            return ResponseEntity.status(HttpStatus.CONFLICT).body("CardEntity with number " + card.getNumber() + " already exists.");
            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.");
        }
        this.cardRepository.saveAndFlush(card);
@@ -124,5 +116,5 @@
                .created(newCardURI)
                .body(newCardURI.toString());
    }
*/
    */
}
src/main/java/hu/unr/fiber/cardapi/entity/CardByIdInterface.java
File was deleted
src/main/java/hu/unr/fiber/cardapi/entity/CardEntityInterface.java
File was deleted
src/main/java/hu/unr/fiber/cardapi/entity/CardEntityNotFoundException.java
File was deleted
src/main/java/hu/unr/fiber/cardapi/entity/CardInteractor.java
File was deleted
src/main/java/hu/unr/fiber/cardapi/entity/CardListBoundaryInterface.java
File was deleted
src/main/java/hu/unr/fiber/cardapi/hibernate/Card.java
File was renamed from src/main/java/hu/unr/fiber/cardapi/entity/Card.java
@@ -1,11 +1,13 @@
package hu.unr.fiber.cardapi.entity;
package hu.unr.fiber.cardapi.hibernate;
import hu.unr.fiber.cardapi.interfaces.CardInterface;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "Card")
public class Card implements CardEntityInterface {
public class Card implements CardInterface {
    public static final long INVALID_ID = 0;
    @Id
@@ -89,10 +91,12 @@
                Objects.equals(cardHolder, card.cardHolder);
    }
    public void update(Card otherCard) {
    public CardInterface update(CardInterface otherCard) {
        this.setName(otherCard.getName());
        this.setNumber(otherCard.getNumber());
        this.setCardHolder(otherCard.getCardHolder());
        return this;
    }
    @Override
src/main/java/hu/unr/fiber/cardapi/hibernate/CardInteractor.java
New file
@@ -0,0 +1,83 @@
package hu.unr.fiber.cardapi.hibernate;
import java.util.ArrayList;
import java.util.List;
import hu.unr.fiber.cardapi.interfaces.CardInteractorInterface;
import hu.unr.fiber.cardapi.interfaces.CardInterface;
import hu.unr.fiber.cardapi.rest.RestCard;
import org.springframework.orm.jpa.JpaObjectRetrievalFailureException;
import org.springframework.stereotype.Component;
@Component
public class CardInteractor implements CardInteractorInterface {
    private CardRepository cardRepository;
    CardInteractor(CardRepository cardRepository) {
        this.cardRepository = cardRepository;
    }
    @Override
    public List<CardInterface> getCardList() {
        List<Card> cardList = this.cardRepository.findAll();
        List<CardInterface> restCardList = new ArrayList<>();
        for (Card card : cardList) {
            RestCard ce = new RestCard(card.getId());
            restCardList.add(ce.update(card));
        }
        return restCardList;
    }
    @Override
    public CardInterface getCardById(long id) throws CardNotFoundException {
        if (!this.cardRepository.existsById(id)) {
            throw new CardNotFoundException("No such card with id: #"+ id);
        }
        Card c = this.cardRepository.getOne(id);
        RestCard ce = new RestCard(c.getId());
        return ce.update(c);
    }
    @Override
    public void delete(long id) throws CardNotFoundException {
        try {
            Card c = this.cardRepository.getOne(id);
            this.cardRepository.delete(c);
            this.cardRepository.flush();
        } catch (JpaObjectRetrievalFailureException e) {
            throw new CardNotFoundException(e.getMessage());
        }
    }
    @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);
        }
        if (this.getCardByNumber(updatedCard.getNumber()) != null) {
            throw new CardInvalidUpdateException("Given card number already exists", updatedCard);
        }
        this.cardRepository.saveAndFlush((Card) updatedCard);
        return this.getCardById(updatedCard.getId());
    }
    private Card getCardByNumber(String number) {
        Long id = cardRepository.findIdByNumber(number);
        if (id == null) {
            return null;
        }
        return this.cardRepository.getOne(id);
    }
}
src/main/java/hu/unr/fiber/cardapi/hibernate/CardInvalidUpdateException.java
New file
@@ -0,0 +1,15 @@
package hu.unr.fiber.cardapi.hibernate;
import hu.unr.fiber.cardapi.interfaces.CardInterface;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public class CardInvalidUpdateException extends Exception {
    CardInterface card;
    public CardInvalidUpdateException(String message, CardInterface modifiedCard) {
        super(message);
        this.card = modifiedCard;
    }
}
src/main/java/hu/unr/fiber/cardapi/hibernate/CardNotFoundException.java
New file
@@ -0,0 +1,7 @@
package hu.unr.fiber.cardapi.hibernate;
public class CardNotFoundException extends Exception {
    public CardNotFoundException(String message) {
        super(message);
    }
}
src/main/java/hu/unr/fiber/cardapi/hibernate/CardRepository.java
File was renamed from src/main/java/hu/unr/fiber/cardapi/entity/CardRepository.java
@@ -1,4 +1,4 @@
package hu.unr.fiber.cardapi.entity;
package hu.unr.fiber.cardapi.hibernate;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
src/main/java/hu/unr/fiber/cardapi/interfaces/CardByIdInterface.java
New file
@@ -0,0 +1,5 @@
package hu.unr.fiber.cardapi.interfaces;
public interface CardByIdInterface {
    public CardInterface getCardById(long id) throws Exception;
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardCreateInterface.java
New file
@@ -0,0 +1,4 @@
package hu.unr.fiber.cardapi.interfaces;
public interface CardCreateInterface {
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardDeleteInterface.java
New file
@@ -0,0 +1,7 @@
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
New file
@@ -0,0 +1,9 @@
package hu.unr.fiber.cardapi.interfaces;
public interface CardInteractorInterface extends
        CardByIdInterface,
        CardListBoundaryInterface,
        CardDeleteInterface,
        CardModifyInterface {
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardInterface.java
New file
@@ -0,0 +1,23 @@
package hu.unr.fiber.cardapi.interfaces;
public interface CardInterface {
    public long getId();
    public boolean validId();
    public CardInterface setId(long id);
    public String getName();
    public CardInterface setName(String name);
    public String getNumber();
    public CardInterface setNumber(String number);
    public String getCardHolder();
    public CardInterface setCardHolder(String cardHolder);
    public CardInterface update(CardInterface otherCard);
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardListBoundaryInterface.java
New file
@@ -0,0 +1,7 @@
package hu.unr.fiber.cardapi.interfaces;
import java.util.List;
public interface CardListBoundaryInterface {
    public List<CardInterface> getCardList();
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardModifyInterface.java
New file
@@ -0,0 +1,5 @@
package hu.unr.fiber.cardapi.interfaces;
public interface CardModifyInterface {
    public CardInterface update(long id, CardInterface card) throws Exception;
}
src/main/java/hu/unr/fiber/cardapi/interfaces/CardNotFoundExceptionInterface.java
New file
@@ -0,0 +1,4 @@
package hu.unr.fiber.cardapi.interfaces;
public interface CardNotFoundExceptionInterface {
}
src/main/java/hu/unr/fiber/cardapi/rest/RestCard.java
File was renamed from src/main/java/hu/unr/fiber/cardapi/rest/CardEntity.java
@@ -1,11 +1,11 @@
package hu.unr.fiber.cardapi.rest;
import hu.unr.fiber.cardapi.entity.CardEntityInterface;
import hu.unr.fiber.cardapi.interfaces.CardInterface;
import java.util.Objects;
public class CardEntity implements CardEntityInterface {
public class RestCard implements CardInterface {
    public static final long INVALID_ID = 0;
    private long id = INVALID_ID;
@@ -14,17 +14,17 @@
    private String number;
    private String cardHolder;
    public CardEntity(long id) {
    public RestCard(long id) {
        this.id = id;
    }
    public CardEntity(long id, String name, String number) {
    public RestCard(long id, String name, String number) {
        this.setId(id)
                .setName(name)
                .setNumber(number);
    }
    public CardEntity(long id, String name, String number, String cardHolder) {
    public RestCard(long id, String name, String number, String cardHolder) {
        this.setId(id)
                .setName(name)
                .setNumber(number)
@@ -36,10 +36,10 @@
    }
    public boolean validId() {
        return (this.getId() != CardEntity.INVALID_ID);
        return (this.getId() != RestCard.INVALID_ID);
    }
    public CardEntity setId(long id) {
    public RestCard setId(long id) {
        this.id = id;
        return this;
    }
@@ -48,7 +48,7 @@
        return name;
    }
    public CardEntity setName(String name) {
    public RestCard setName(String name) {
        this.name = name;
        return this;
    }
@@ -57,7 +57,7 @@
        return number;
    }
    public CardEntity setNumber(String number) {
    public RestCard setNumber(String number) {
        this.number = number;
        return this;
    }
@@ -66,7 +66,7 @@
        return cardHolder;
    }
    public CardEntity setCardHolder(String cardHolder) {
    public RestCard setCardHolder(String cardHolder) {
        this.cardHolder = cardHolder;
        return this;
    }
@@ -74,17 +74,17 @@
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CardEntity card = (CardEntity) o;
        RestCard card = (RestCard) 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());
    public RestCard update(CardInterface otherCard) {
        this.setName(otherCard.getName());
        this.setNumber(otherCard.getNumber());
        this.setCardHolder(otherCard.getCardHolder());
        return this;
    }