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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package hu.unr.fiber.cardapi.hibernate;
 
import java.util.ArrayList;
import java.util.List;
 
import hu.unr.fiber.cardapi.interfaces.CardInteractorInterface;
import hu.unr.fiber.cardapi.interfaces.CardInterface;
import hu.unr.fiber.cardapi.interfaces.CardInvalidUpdateException;
import hu.unr.fiber.cardapi.interfaces.CardNotFoundException;
import hu.unr.fiber.cardapi.rest.RestCard;
import org.springframework.orm.jpa.JpaObjectRetrievalFailureException;
import org.springframework.stereotype.Component;
 
@Component
public class CardInteractor implements CardInteractorInterface {
 
    private CardRepository cardRepository;
 
    CardInteractor(CardRepository cardRepository) {
        this.cardRepository = cardRepository;
    }
 
    @Override
    public List<CardInterface> getCardList() {
 
        List<Card> cardList = this.cardRepository.findAll();
        List<CardInterface> restCardList = new ArrayList<>();
 
        for (Card card : cardList) {
            RestCard ce = new RestCard(card.getId());
            restCardList.add(ce.update(card));
        }
 
        return restCardList;
    }
 
    @Override
    public CardInterface getCardById(long id) throws CardNotFoundException {
        if (!this.cardRepository.existsById(id)) {
            throw new CardNotFoundException("No such card with id: #" + id);
        }
 
        Card c = this.cardRepository.getOne(id);
        RestCard ce = new RestCard(c.getId());
 
        return ce.update(c);
    }
 
    @Override
    public void delete(long id) throws CardNotFoundException {
        try {
            Card c = this.cardRepository.getOne(id);
            this.cardRepository.delete(c);
            this.cardRepository.flush();
        } catch (JpaObjectRetrievalFailureException e) {
            throw new CardNotFoundException(e.getMessage());
        }
    }
 
    @Override
    public CardInterface update(long id, CardInterface updatedCard) throws CardInvalidUpdateException, CardNotFoundException {
 
        if (updatedCard.validId() && (updatedCard.getId() != id)) {
            throw new CardInvalidUpdateException("Card ID cannot be changed! ",
                    CardInvalidUpdateException.CARD_ID_CHANGE);
        }
 
        Card oldCard = this.getCardByNumber(updatedCard.getNumber());
 
        if (oldCard == null) {
            oldCard = this.cardRepository.getOne(id);
        } else {
            if (oldCard.getId() != id) {
                //The given card Number is already assigned to an another card with different ID
                String message = "Cannot update card number, given card number already assigned to card with ID #" + oldCard.getId();
                throw new CardInvalidUpdateException(message, CardInvalidUpdateException.CARD_NUMBER_ALREADY_ASSIGNED);
            }
        }
 
        oldCard.update(updatedCard);
 
        this.cardRepository.saveAndFlush(oldCard);
 
        return this.getCardById(updatedCard.getId());
    }
 
 
    @Override
    public CardInterface create(CardInterface card) throws CardInvalidUpdateException {
        Card duplicateNumberedCard = this.getCardByNumber(card.getNumber());
        if (duplicateNumberedCard != null) {
            throw new CardInvalidUpdateException("Card with the same number already exists!",
                    CardInvalidUpdateException.CARD_NUMBER_ALREADY_ASSIGNED);
        }
 
        Card c = new Card();
        c.update(card);
 
        this.cardRepository.saveAndFlush(c);
 
        return c;
    }
 
    private Card getCardByNumber(String number) {
 
        Long id = cardRepository.findIdByNumber(number);
 
        if (id == null) {
            return null;
        }
 
        return this.cardRepository.getOne(id);
    }
}