Fibinger Ádám
2019-03-12 c4d8396bde66ac81731b267747c524c0d9fe98c6
commit | author | age
9b7a17 1 package hu.unr.fiber.cardapi;
2
12abae 3 import java.net.URI;
9b7a17 4 import java.util.List;
5
12abae 6 import hu.unr.fiber.cardapi.interfaces.CardInvalidUpdateException;
7 import hu.unr.fiber.cardapi.interfaces.CardNotFoundException;
94fead 8 import hu.unr.fiber.cardapi.interfaces.CardInterface;
9 import hu.unr.fiber.cardapi.hibernate.CardInteractor;
12abae 10 import hu.unr.fiber.cardapi.rest.RestCard;
9b7a17 11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.http.HttpStatus;
14 import org.springframework.http.ResponseEntity;
15 import org.springframework.web.bind.annotation.*;
94fead 16 import org.springframework.web.server.ResponseStatusException;
12abae 17 import org.springframework.web.util.UriComponentsBuilder;
9b7a17 18
19 @RestController
20 public class CardController {
21     Logger logger = LoggerFactory.getLogger(CardController.class);
464000 22     private CardInteractor cardService;
9b7a17 23
464000 24     CardController(CardInteractor cS) {
25         this.cardService = cS;
9b7a17 26     }
27
28     @GetMapping("/")
29     public String index() {
ca2732 30         logger.info("GET /index called.");
31         return "Hateoas helyett: (/cards GET /card/{id} POST /card/{id} PUT /card/{id} DELETE /card/{id} )";
9b7a17 32     }
33
34     @GetMapping("/cards")
94fead 35     public ResponseEntity<List<CardInterface>> cards() {
37e2b6 36         logger.info("GET /cards called, responded with items.");
94fead 37         List<CardInterface> cardList = cardService.getCardList();
464000 38         return ResponseEntity.ok().body(cardList);
9b7a17 39     }
40
41     @GetMapping(value = "/card/{id}")
94fead 42     public ResponseEntity<CardInterface> getCard(@PathVariable("id") long id) {
ca2732 43         logger.info("GET /card/" + id + " called.");
37e2b6 44
94fead 45         try {
46             CardInterface card = this.cardService.getCardById(id);
47             return ResponseEntity.ok().body(card);
48         } catch (CardNotFoundException e) {
49             throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No such card with id: #" + id);
37e2b6 50         }
ca2732 51     }
94fead 52
ca2732 53     @DeleteMapping(value = "/card/{id}")
54     public ResponseEntity<String> deleteCard(@PathVariable("id") long id) {
55         logger.info("DELETE /card/" + id + " called");
56
94fead 57         try {
58             this.cardService.delete(id);
59         } catch (CardNotFoundException e) {
60             throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No such card with id: #" + id, e);
ca2732 61         }
62
94fead 63         return ResponseEntity.ok().body("OK");
64     }
12abae 65
94fead 66     @PostMapping(value = "/card/{id}")
67     public ResponseEntity<String> updateCard(@PathVariable("id") long id, @RequestBody RestCard updatedCardEntity, UriComponentsBuilder ucBuilder) {
68
69         logger.info("POST /card/" + id + " called, card update.");
70         try {
71             this.cardService.update(id, updatedCardEntity);
12abae 72         } catch (CardInvalidUpdateException e) {
73             throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
74         } catch (CardNotFoundException e) {
75             throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No such card with id: #" + id, e);
94fead 76         }
ca2732 77
78         return ResponseEntity.accepted().body("OK");
9b7a17 79     }
80
81     @PostMapping(value = "/card")
94fead 82     public ResponseEntity<String> createCard(@RequestBody RestCard card, UriComponentsBuilder ucBuilder) {
83         logger.info("Creating RestCard : {}", card.getNumber());
9b7a17 84
12abae 85         try {
86             CardInterface c = this.cardService.create(card);
87             card.setId(c.getId());
88         } catch (CardInvalidUpdateException e) {
89             throw new ResponseStatusException(HttpStatus.CONFLICT, e.getMessage());
9b7a17 90         }
91
ca2732 92         URI newCardURI = ucBuilder.path("/card/{id}").buildAndExpand(card.getId()).toUri();
93
94         return ResponseEntity
95                 .created(newCardURI)
96                 .body(newCardURI.toString());
9b7a17 97     }
98 }