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