From 464000b3acc64e678a57141c2eb806a5b3d2a01c Mon Sep 17 00:00:00 2001 From: Fibinger Ádám <adam.fibinger@wup.hu> Date: Sun, 03 Mar 2019 12:41:12 +0100 Subject: [PATCH] Basic refactor and decoupling REST Controller from JPA --- src/main/java/hu/unr/fiber/cardapi/CardController.java | 111 ++++++++++++++++++++++++------------------------------- 1 files changed, 49 insertions(+), 62 deletions(-) diff --git a/src/main/java/hu/unr/fiber/cardapi/CardController.java b/src/main/java/hu/unr/fiber/cardapi/CardController.java index d3eb21f..33c51b2 100644 --- a/src/main/java/hu/unr/fiber/cardapi/CardController.java +++ b/src/main/java/hu/unr/fiber/cardapi/CardController.java @@ -1,11 +1,14 @@ package hu.unr.fiber.cardapi; import java.net.URI; -import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.Random; +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; @@ -16,14 +19,10 @@ @RestController public class CardController { Logger logger = LoggerFactory.getLogger(CardController.class); + private CardInteractor cardService; - protected List<Card> cardList = new ArrayList<>(); - - public CardController() { - cardList.add(new Card(1, "Első kártya", "1")); - cardList.add(new Card(2, "Második kártya", "2")); - cardList.add(new Card(5, "Harmadik kártya", "4")); - cardList.add(new Card(8, "Tízezer egyszázadik kártya", "10100")); + CardController(CardInteractor cS) { + this.cardService = cS; } @GetMapping("/") @@ -33,103 +32,91 @@ } @GetMapping("/cards") - public List<Card> cards() { - logger.info("GET /cards called, responded with " + cardList.size() + " items."); - return cardList; + public ResponseEntity<List<CardEntityInterface>> cards() { + logger.info("GET /cards called, responded with items."); + 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."); - return Optional - .ofNullable(this.getCardByID(id)) - .map(card -> ResponseEntity.ok().body(card)) //200 OK - .orElseGet(() -> ResponseEntity.notFound().build()); //404 Not found - } + CardEntityInterface card = this.cardService.getCardById(id); + + if (card == null) { + return ResponseEntity.notFound().build(); + } + + 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.getCardByID(id)) + .ofNullable(this.cardRepository.getOne(id)) .map( card -> { - this.cardList.remove(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)); + .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.getCardByID(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 getCardByID(long id) { - for (Card card : cardList) { - if (card.getId() == id) { - return card; - } + private CardEntity getCardByNumber(String number) { + + Long id = cardRepository.findIdByNumber(number); + + if (id == null) { + return null; } - return null; + return cardRepository.getOne(id); } - - private Card getCardByNumber(String number) { - for (Card card : cardList) { - if (card.getNumber().equals(number)) { - return card; - } - } - - return null; - } - @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."); } - long nextId = 1; - - while (this.getCardByID(nextId) != null) { - nextId++; - } - - card.setId(nextId); - - cardList.add(card); + this.cardRepository.saveAndFlush(card); URI newCardURI = ucBuilder.path("/card/{id}").buildAndExpand(card.getId()).toUri(); @@ -137,5 +124,5 @@ .created(newCardURI) .body(newCardURI.toString()); } - +*/ } \ No newline at end of file -- Gitblit v1.8.0