commit | author | age
|
94fead
|
1 |
package hu.unr.fiber.cardapi.hibernate; |
FÁ |
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.List; |
|
5 |
|
|
6 |
import hu.unr.fiber.cardapi.interfaces.CardInteractorInterface; |
|
7 |
import hu.unr.fiber.cardapi.interfaces.CardInterface; |
12abae
|
8 |
import hu.unr.fiber.cardapi.interfaces.CardInvalidUpdateException; |
FÁ |
9 |
import hu.unr.fiber.cardapi.interfaces.CardNotFoundException; |
94fead
|
10 |
import hu.unr.fiber.cardapi.rest.RestCard; |
FÁ |
11 |
import org.springframework.orm.jpa.JpaObjectRetrievalFailureException; |
|
12 |
import org.springframework.stereotype.Component; |
|
13 |
|
|
14 |
@Component |
|
15 |
public class CardInteractor implements CardInteractorInterface { |
|
16 |
|
|
17 |
private CardRepository cardRepository; |
|
18 |
|
|
19 |
CardInteractor(CardRepository cardRepository) { |
|
20 |
this.cardRepository = cardRepository; |
|
21 |
} |
|
22 |
|
|
23 |
@Override |
|
24 |
public List<CardInterface> getCardList() { |
|
25 |
|
|
26 |
List<Card> cardList = this.cardRepository.findAll(); |
|
27 |
List<CardInterface> restCardList = new ArrayList<>(); |
|
28 |
|
|
29 |
for (Card card : cardList) { |
|
30 |
RestCard ce = new RestCard(card.getId()); |
|
31 |
restCardList.add(ce.update(card)); |
|
32 |
} |
|
33 |
|
|
34 |
return restCardList; |
|
35 |
} |
|
36 |
|
|
37 |
@Override |
|
38 |
public CardInterface getCardById(long id) throws CardNotFoundException { |
|
39 |
if (!this.cardRepository.existsById(id)) { |
12abae
|
40 |
throw new CardNotFoundException("No such card with id: #" + id); |
94fead
|
41 |
} |
FÁ |
42 |
|
|
43 |
Card c = this.cardRepository.getOne(id); |
|
44 |
RestCard ce = new RestCard(c.getId()); |
|
45 |
|
|
46 |
return ce.update(c); |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
public void delete(long id) throws CardNotFoundException { |
|
51 |
try { |
|
52 |
Card c = this.cardRepository.getOne(id); |
|
53 |
this.cardRepository.delete(c); |
|
54 |
this.cardRepository.flush(); |
|
55 |
} catch (JpaObjectRetrievalFailureException e) { |
|
56 |
throw new CardNotFoundException(e.getMessage()); |
|
57 |
} |
|
58 |
} |
|
59 |
|
|
60 |
@Override |
|
61 |
public CardInterface update(long id, CardInterface updatedCard) throws CardInvalidUpdateException, CardNotFoundException { |
12abae
|
62 |
|
94fead
|
63 |
if (updatedCard.validId() && (updatedCard.getId() != id)) { |
12abae
|
64 |
throw new CardInvalidUpdateException("Card ID cannot be changed! ", |
FÁ |
65 |
CardInvalidUpdateException.CARD_ID_CHANGE); |
94fead
|
66 |
} |
FÁ |
67 |
|
12abae
|
68 |
Card oldCard = this.getCardByNumber(updatedCard.getNumber()); |
FÁ |
69 |
|
|
70 |
if (oldCard == null) { |
|
71 |
oldCard = this.cardRepository.getOne(id); |
|
72 |
} else { |
|
73 |
if (oldCard.getId() != id) { |
|
74 |
//The given card Number is already assigned to an another card with different ID |
|
75 |
String message = "Cannot update card number, given card number already assigned to card with ID #" + oldCard.getId(); |
|
76 |
throw new CardInvalidUpdateException(message, CardInvalidUpdateException.CARD_NUMBER_ALREADY_ASSIGNED); |
|
77 |
} |
94fead
|
78 |
} |
FÁ |
79 |
|
12abae
|
80 |
oldCard.update(updatedCard); |
FÁ |
81 |
|
|
82 |
this.cardRepository.saveAndFlush(oldCard); |
94fead
|
83 |
|
FÁ |
84 |
return this.getCardById(updatedCard.getId()); |
|
85 |
} |
|
86 |
|
12abae
|
87 |
|
FÁ |
88 |
@Override |
|
89 |
public CardInterface create(CardInterface card) throws CardInvalidUpdateException { |
|
90 |
Card duplicateNumberedCard = this.getCardByNumber(card.getNumber()); |
|
91 |
if (duplicateNumberedCard != null) { |
|
92 |
throw new CardInvalidUpdateException("Card with the same number already exists!", |
|
93 |
CardInvalidUpdateException.CARD_NUMBER_ALREADY_ASSIGNED); |
|
94 |
} |
|
95 |
|
|
96 |
Card c = new Card(); |
|
97 |
c.update(card); |
|
98 |
|
|
99 |
this.cardRepository.saveAndFlush(c); |
|
100 |
|
|
101 |
return c; |
|
102 |
} |
|
103 |
|
94fead
|
104 |
private Card getCardByNumber(String number) { |
FÁ |
105 |
|
|
106 |
Long id = cardRepository.findIdByNumber(number); |
|
107 |
|
|
108 |
if (id == null) { |
|
109 |
return null; |
|
110 |
} |
|
111 |
|
|
112 |
return this.cardRepository.getOne(id); |
|
113 |
} |
|
114 |
} |