commit | author | age
|
9b7a17
|
1 |
package hu.unr.fiber.cardapi; |
FÁ |
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
94fead
|
5 |
import hu.unr.fiber.cardapi.hibernate.CardNotFoundException; |
FÁ |
6 |
import hu.unr.fiber.cardapi.interfaces.CardInterface; |
|
7 |
import hu.unr.fiber.cardapi.hibernate.CardInteractor; |
9b7a17
|
8 |
import org.slf4j.Logger; |
FÁ |
9 |
import org.slf4j.LoggerFactory; |
|
10 |
import org.springframework.http.HttpStatus; |
|
11 |
import org.springframework.http.ResponseEntity; |
|
12 |
import org.springframework.web.bind.annotation.*; |
94fead
|
13 |
import org.springframework.web.server.ResponseStatusException; |
9b7a17
|
14 |
|
FÁ |
15 |
@RestController |
|
16 |
public class CardController { |
|
17 |
Logger logger = LoggerFactory.getLogger(CardController.class); |
464000
|
18 |
private CardInteractor cardService; |
9b7a17
|
19 |
|
464000
|
20 |
CardController(CardInteractor cS) { |
FÁ |
21 |
this.cardService = cS; |
9b7a17
|
22 |
} |
FÁ |
23 |
|
|
24 |
@GetMapping("/") |
|
25 |
public String index() { |
ca2732
|
26 |
logger.info("GET /index called."); |
FÁ |
27 |
return "Hateoas helyett: (/cards GET /card/{id} POST /card/{id} PUT /card/{id} DELETE /card/{id} )"; |
9b7a17
|
28 |
} |
FÁ |
29 |
|
|
30 |
@GetMapping("/cards") |
94fead
|
31 |
public ResponseEntity<List<CardInterface>> cards() { |
37e2b6
|
32 |
logger.info("GET /cards called, responded with items."); |
94fead
|
33 |
List<CardInterface> cardList = cardService.getCardList(); |
464000
|
34 |
return ResponseEntity.ok().body(cardList); |
9b7a17
|
35 |
} |
FÁ |
36 |
|
|
37 |
@GetMapping(value = "/card/{id}") |
94fead
|
38 |
public ResponseEntity<CardInterface> getCard(@PathVariable("id") long id) { |
ca2732
|
39 |
logger.info("GET /card/" + id + " called."); |
37e2b6
|
40 |
|
94fead
|
41 |
try { |
FÁ |
42 |
CardInterface card = this.cardService.getCardById(id); |
|
43 |
return ResponseEntity.ok().body(card); |
|
44 |
} catch (CardNotFoundException e) { |
|
45 |
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No such card with id: #" + id); |
37e2b6
|
46 |
} |
ca2732
|
47 |
} |
94fead
|
48 |
|
ca2732
|
49 |
@DeleteMapping(value = "/card/{id}") |
FÁ |
50 |
public ResponseEntity<String> deleteCard(@PathVariable("id") long id) { |
|
51 |
logger.info("DELETE /card/" + id + " called"); |
|
52 |
|
94fead
|
53 |
try { |
FÁ |
54 |
this.cardService.delete(id); |
|
55 |
} catch (CardNotFoundException e) { |
|
56 |
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No such card with id: #" + id, e); |
ca2732
|
57 |
} |
FÁ |
58 |
|
94fead
|
59 |
return ResponseEntity.ok().body("OK"); |
FÁ |
60 |
} |
|
61 |
/* |
|
62 |
@PostMapping(value = "/card/{id}") |
|
63 |
public ResponseEntity<String> updateCard(@PathVariable("id") long id, @RequestBody RestCard updatedCardEntity, UriComponentsBuilder ucBuilder) { |
|
64 |
|
|
65 |
logger.info("POST /card/" + id + " called, card update."); |
|
66 |
try { |
|
67 |
this.cardService.update(id, updatedCardEntity); |
|
68 |
} catch (CardInvalidUpdateException e){ |
|
69 |
|
|
70 |
} |
|
71 |
|
|
72 |
|
|
73 |
RestCard originalCardEntity = this.cardRepository.getOne(id); |
ca2732
|
74 |
|
464000
|
75 |
if (originalCardEntity.equals(updatedCardEntity)) { |
ca2732
|
76 |
return ResponseEntity.accepted().body("Update has no changes."); |
FÁ |
77 |
} |
|
78 |
|
|
79 |
//card number updated, we have to check if its already exists |
464000
|
80 |
if (!originalCardEntity.getNumber().equals(updatedCardEntity.getNumber()) && this.getCardByNumber(updatedCardEntity.getNumber()) != null) { |
94fead
|
81 |
logger.error("Unable to update card with id {}. A different RestCard with number {} already exist", originalCardEntity.getId(), updatedCardEntity.getNumber()); |
FÁ |
82 |
return ResponseEntity.status(HttpStatus.CONFLICT).body("RestCard with number " + updatedCardEntity.getNumber() + " already exists."); |
ca2732
|
83 |
} |
FÁ |
84 |
|
464000
|
85 |
originalCardEntity.update(updatedCardEntity); |
37e2b6
|
86 |
this.cardRepository.flush(); |
ca2732
|
87 |
|
FÁ |
88 |
return ResponseEntity.accepted().body("OK"); |
9b7a17
|
89 |
} |
FÁ |
90 |
|
94fead
|
91 |
private RestCard getCardByNumber(String number) { |
37e2b6
|
92 |
|
FÁ |
93 |
Long id = cardRepository.findIdByNumber(number); |
|
94 |
|
|
95 |
if (id == null) { |
|
96 |
return null; |
9b7a17
|
97 |
} |
FÁ |
98 |
|
37e2b6
|
99 |
return cardRepository.getOne(id); |
9b7a17
|
100 |
} |
FÁ |
101 |
|
|
102 |
@PostMapping(value = "/card") |
94fead
|
103 |
public ResponseEntity<String> createCard(@RequestBody RestCard card, UriComponentsBuilder ucBuilder) { |
FÁ |
104 |
logger.info("Creating RestCard : {}", card.getNumber()); |
9b7a17
|
105 |
|
FÁ |
106 |
if (this.getCardByNumber(card.getNumber()) != null) { |
94fead
|
107 |
logger.error("Unable to create. A RestCard with number {} already exist", card.getNumber()); |
FÁ |
108 |
return ResponseEntity.status(HttpStatus.CONFLICT).body("RestCard with number " + card.getNumber() + " already exists."); |
9b7a17
|
109 |
} |
FÁ |
110 |
|
37e2b6
|
111 |
this.cardRepository.saveAndFlush(card); |
9b7a17
|
112 |
|
ca2732
|
113 |
URI newCardURI = ucBuilder.path("/card/{id}").buildAndExpand(card.getId()).toUri(); |
FÁ |
114 |
|
|
115 |
return ResponseEntity |
|
116 |
.created(newCardURI) |
|
117 |
.body(newCardURI.toString()); |
9b7a17
|
118 |
} |
94fead
|
119 |
*/ |
9b7a17
|
120 |
} |