2019-03-12 | Fibinger Ádám | |
2019-03-11 | Fibinger Ádám | |
2019-03-03 | Fibinger Ádám |
src/main/java/hu/unr/fiber/cardapi/CardController.java
@@ -2,25 +2,28 @@ import java.net.URI; import java.util.List; import java.util.Optional; 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 { 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,91 +32,62 @@ } @GetMapping("/cards") public List<Card> cards() { public ResponseEntity<List<CardInterface>> cards() { logger.info("GET /cards called, responded with items."); return cardRepository.findAll(); List<CardInterface> cardList = cardService.getCardList(); return ResponseEntity.ok().body(cardList); } @GetMapping(value = "/card/{id}") public ResponseEntity<Card> getCard(@PathVariable("id") long id) { public ResponseEntity<CardInterface> getCard(@PathVariable("id") long id) { logger.info("GET /card/" + id + " called."); if (!this.cardRepository.existsById(id)) { 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); } 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"); 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)); try { this.cardService.delete(id); } catch (CardNotFoundException e) { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No such card with id: #" + id, e); } return ResponseEntity.ok().body("OK"); } @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 RestCard updatedCardEntity, UriComponentsBuilder ucBuilder) { logger.info("POST /card/"+ id + " called, card update."); if (updatedCard.validId() && (updatedCard.getId() != id)) { return ResponseEntity .badRequest() .body("Id field cannot be modified."); logger.info("POST /card/" + id + " called, card update."); 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); } Card originalCard = this.cardRepository.getOne(id); if (originalCard.equals(updatedCard)) { 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."); } originalCard.update(updatedCard); this.cardRepository.flush(); return ResponseEntity.accepted().body("OK"); } private Card 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 Card card, UriComponentsBuilder ucBuilder) { logger.info("Creating Card : {}", 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 Card with number {} already exist", card.getNumber()); return ResponseEntity.status(HttpStatus.CONFLICT).body("Card 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(); @@ -121,5 +95,4 @@ .created(newCardURI) .body(newCardURI.toString()); } } src/main/java/hu/unr/fiber/cardapi/CardList.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/Card.java @@ -1,14 +1,13 @@ package hu.unr.fiber.cardapi; package hu.unr.fiber.cardapi.hibernate; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import hu.unr.fiber.cardapi.interfaces.CardInterface; import javax.persistence.*; import java.util.Objects; @Entity @Table(name = "Card") @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) public class Card { public class Card implements CardInterface { public static final long INVALID_ID = 0; @Id @@ -17,23 +16,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 +49,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) { @@ -82,14 +91,16 @@ 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 public String toString() { return "#" + this.number + " - " + this.name + " @ " + this.cardHolder; return "#" + this.number + " - " + this.name + " @ " + this.cardHolder; } } } src/main/java/hu/unr/fiber/cardapi/hibernate/CardInteractor.java
New file @@ -0,0 +1,114 @@ 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.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; @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! ", CardInvalidUpdateException.CARD_ID_CHANGE); } 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); } } 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); if (id == null) { return null; } return this.cardRepository.getOne(id); } } src/main/java/hu/unr/fiber/cardapi/hibernate/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.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,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
New file @@ -0,0 +1,5 @@ package hu.unr.fiber.cardapi.interfaces; 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,10 @@ package hu.unr.fiber.cardapi.interfaces; public interface CardInteractorInterface extends CardByIdInterface, CardListBoundaryInterface, CardDeleteInterface, CardModifyInterface, CardCreateInterface { } 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/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/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/CardNotFoundException.java
New file @@ -0,0 +1,7 @@ package hu.unr.fiber.cardapi.interfaces; public class CardNotFoundException extends Exception { public CardNotFoundException(String message) { super(message); } } 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
New file @@ -0,0 +1,96 @@ package hu.unr.fiber.cardapi.rest; import com.fasterxml.jackson.annotation.JsonCreator; import hu.unr.fiber.cardapi.interfaces.CardInterface; import java.util.Objects; public class RestCard implements CardInterface { 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 RestCard(long id) { this.id = id; } public RestCard(long id, String name, String number) { this.setId(id) .setName(name) .setNumber(number); } @JsonCreator public RestCard(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() != RestCard.INVALID_ID); } public RestCard setId(long id) { this.id = id; return this; } public String getName() { return name; } public RestCard setName(String name) { this.name = name; return this; } public String getNumber() { return number; } public RestCard setNumber(String number) { this.number = number; return this; } public String getCardHolder() { return cardHolder; } public RestCard 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; RestCard card = (RestCard) o; return id == card.id && name.equals(card.name) && number.equals(card.number) && Objects.equals(cardHolder, card.cardHolder); } public RestCard update(CardInterface otherCard) { this.setName(otherCard.getName()); this.setNumber(otherCard.getNumber()); this.setCardHolder(otherCard.getCardHolder()); return this; } public String toString() { return "#" + this.number + " - " + this.name + " @ " + this.cardHolder; } }