From c4d8396bde66ac81731b267747c524c0d9fe98c6 Mon Sep 17 00:00:00 2001
From: Fibinger Ádám <adam.fibinger@wup.hu>
Date: Tue, 12 Mar 2019 19:46:19 +0100
Subject: [PATCH] Force example data to UTF-8

---
 src/main/java/hu/unr/fiber/cardapi/CardController.java |  102 ++++++++++++++++++---------------------------------
 1 files changed, 36 insertions(+), 66 deletions(-)

diff --git a/src/main/java/hu/unr/fiber/cardapi/CardController.java b/src/main/java/hu/unr/fiber/cardapi/CardController.java
index 33c51b2..141cd21 100644
--- a/src/main/java/hu/unr/fiber/cardapi/CardController.java
+++ b/src/main/java/hu/unr/fiber/cardapi/CardController.java
@@ -2,18 +2,18 @@
 
 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.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
@@ -32,91 +32,62 @@
     }
 
     @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));
+        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 CardEntity updatedCardEntity, UriComponentsBuilder ucBuilder) {
+    public ResponseEntity<String> updateCard(@PathVariable("id") long id, @RequestBody RestCard 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.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);
         }
-
-        CardEntity 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 CardEntity with number {} already exist", originalCardEntity.getId(), updatedCardEntity.getNumber());
-            return ResponseEntity.status(HttpStatus.CONFLICT).body("CardEntity with number " + updatedCardEntity.getNumber() + " already exists.");
-        }
-
-        originalCardEntity.update(updatedCardEntity);
-        this.cardRepository.flush();
 
         return ResponseEntity.accepted().body("OK");
     }
 
-    private CardEntity 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 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.");
+        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();
 
@@ -124,5 +95,4 @@
                 .created(newCardURI)
                 .body(newCardURI.toString());
     }
-*/
 }
\ No newline at end of file

--
Gitblit v1.8.0