|
1
|
|
|
package deck |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"github.com/fxamacker/cbor/v2" |
|
5
|
|
|
"github.com/memnix/memnix-rest/domain" |
|
6
|
|
|
) |
|
7
|
|
|
|
|
8
|
|
|
type UseCase struct { |
|
9
|
|
|
IRepository |
|
10
|
|
|
IRedisRepository |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
func NewUseCase(repo IRepository, redis IRedisRepository) IUseCase { |
|
14
|
|
|
return &UseCase{IRepository: repo, IRedisRepository: redis} |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
// GetByID returns the deck with the given id. |
|
18
|
|
|
func (u *UseCase) GetByID(id uint) (domain.Deck, error) { |
|
19
|
|
|
var deckObject domain.Deck |
|
20
|
|
|
|
|
21
|
|
|
if cacheHit, _ := u.IRedisRepository.GetByID(id); cacheHit != "" { |
|
22
|
|
|
if err := cbor.Unmarshal([]byte(cacheHit), &deckObject); err == nil { |
|
23
|
|
|
return deckObject, nil |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
deckObject, err := u.IRepository.GetByID(id) |
|
28
|
|
|
if err != nil { |
|
29
|
|
|
return domain.Deck{}, err |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if marshalledDeck, err := cbor.Marshal(deckObject); err == nil { |
|
33
|
|
|
_ = u.IRedisRepository.SetByID(id, string(marshalledDeck)) |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return deckObject, nil |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
func (u *UseCase) Create(deck *domain.Deck) error { |
|
40
|
|
|
if marshalledDeck, err := cbor.Marshal(deck); err == nil { |
|
41
|
|
|
_ = u.IRedisRepository.SetByID(deck.ID, string(marshalledDeck)) |
|
42
|
|
|
} |
|
43
|
|
|
return u.IRepository.Create(deck) |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
func (u *UseCase) CreateFromUser(user domain.User, deck *domain.Deck) error { |
|
47
|
|
|
if marshalledDeck, err := cbor.Marshal(deck); err == nil { |
|
48
|
|
|
_ = u.IRedisRepository.SetByID(deck.ID, string(marshalledDeck)) |
|
49
|
|
|
} |
|
50
|
|
|
return u.IRepository.CreateFromUser(user, deck) |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// GetByUser returns the decks of the given user. |
|
54
|
|
|
func (u *UseCase) GetByUser(user domain.User) ([]domain.Deck, error) { |
|
55
|
|
|
var ownedDecks []domain.Deck |
|
56
|
|
|
|
|
57
|
|
|
if cacheHit, _ := u.IRedisRepository.GetOwnedByUser(user.ID); cacheHit != "" { |
|
58
|
|
|
if err := cbor.Unmarshal([]byte(cacheHit), &ownedDecks); err == nil { |
|
59
|
|
|
return ownedDecks, nil |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
ownedDecks, err := u.IRepository.GetByUser(user) |
|
64
|
|
|
if err != nil { |
|
65
|
|
|
return []domain.Deck{}, err |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if marshalledDeck, err := cbor.Marshal(ownedDecks); err == nil { |
|
69
|
|
|
_ = u.IRedisRepository.SetOwnedByUser(user.ID, string(marshalledDeck)) |
|
70
|
|
|
} |
|
71
|
|
|
return ownedDecks, nil |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// GetByLearner returns the decks of the given learner. |
|
75
|
|
|
func (u *UseCase) GetByLearner(user domain.User) ([]domain.Deck, error) { |
|
76
|
|
|
var learningDecks []domain.Deck |
|
77
|
|
|
|
|
78
|
|
|
if cacheHit, _ := u.IRedisRepository.GetLearningByUser(user.ID); cacheHit != "" { |
|
79
|
|
|
if err := cbor.Unmarshal([]byte(cacheHit), &learningDecks); err == nil { |
|
80
|
|
|
return learningDecks, nil |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
learnedDecks, err := u.IRepository.GetByLearner(user) |
|
85
|
|
|
if err != nil { |
|
86
|
|
|
return []domain.Deck{}, err |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if marshalledDeck, err := cbor.Marshal(learnedDecks); err == nil { |
|
90
|
|
|
_ = u.IRedisRepository.SetLearningByUser(user.ID, string(marshalledDeck)) |
|
91
|
|
|
} |
|
92
|
|
|
return learnedDecks, nil |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// GetPublic returns the public decks. |
|
96
|
|
|
func (u *UseCase) GetPublic() ([]domain.Deck, error) { |
|
97
|
|
|
return u.IRepository.GetPublic() |
|
98
|
|
|
} |
|
99
|
|
|
|