Passed
Push — main ( d8fe53...15d6f0 )
by Yume
01:34 queued 25s
created

deck.getOwnedKey   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package deck
2
3
import (
4
	"context"
5
6
	"github.com/memnix/memnix-rest/config"
7
	"github.com/memnix/memnix-rest/pkg/utils"
8
	"github.com/redis/go-redis/v9"
9
)
10
11
func getBaseKey() string {
12
	return "deck:id:"
13
}
14
15
func getOwnedKey() string {
16
	return "deck:owned:"
17
}
18
19
func getLearningKey() string {
20
	return "deck:learning:"
21
}
22
23
func withID(getter func() string, id uint) string {
24
	return getter() + utils.ConvertUIntToStr(id)
25
}
26
27
// RedisRepository is the interface for the redis repository.
28
type RedisRepository struct {
29
	RedisConn *redis.Client // RedisConn is the redis connection.
30
}
31
32
// GetByID gets the deck by id.
33
func (r RedisRepository) GetByID(id uint) (string, error) {
34
	return r.RedisConn.Get(context.Background(), withID(getBaseKey, id)).Result()
35
}
36
37
// SetByID sets the deck by id.
38
func (r RedisRepository) SetByID(id uint, deck string) error {
39
	return r.RedisConn.Set(context.Background(), withID(getBaseKey, id), deck, config.RedisDefaultExpireTime).Err()
40
}
41
42
// DeleteByID deletes the deck by id.
43
func (r RedisRepository) DeleteByID(id uint) error {
44
	return r.RedisConn.Del(context.Background(), withID(getBaseKey, id)).Err()
45
}
46
47
// SetOwnedByUser sets the decks owned by the user.
48
func (r RedisRepository) SetOwnedByUser(userID uint, decks string) error {
49
	return r.RedisConn.Set(context.Background(), withID(getOwnedKey, userID), decks, config.RedisOwnedExpireTime).Err()
50
}
51
52
// GetOwnedByUser gets the decks owned by the user.
53
func (r RedisRepository) GetOwnedByUser(userID uint) (string, error) {
54
	return r.RedisConn.Get(context.Background(), withID(getOwnedKey, userID)).Result()
55
}
56
57
// DeleteOwnedByUser deletes the decks owned by the user.
58
func (r RedisRepository) DeleteOwnedByUser(userID uint) error {
59
	return r.RedisConn.Del(context.Background(), withID(getOwnedKey, userID)).Err()
60
}
61
62
// SetLearningByUser sets the decks learning by the user.
63
func (r RedisRepository) SetLearningByUser(userID uint, decks string) error {
64
	return r.RedisConn.Set(context.Background(), withID(getLearningKey, userID), decks, config.RedisOwnedExpireTime).Err()
65
}
66
67
// GetLearningByUser gets the decks learning by the user.
68
func (r RedisRepository) GetLearningByUser(userID uint) (string, error) {
69
	return r.RedisConn.Get(context.Background(), withID(getLearningKey, userID)).Result()
70
}
71
72
// DeleteLearningByUser deletes the decks learning by the user.
73
func (r RedisRepository) DeleteLearningByUser(userID uint) error {
74
	return r.RedisConn.Del(context.Background(), withID(getLearningKey, userID)).Err()
75
}
76
77
// NewRedisRepository returns a new redis repository.
78
func NewRedisRepository(redisConn *redis.Client) IRedisRepository {
79
	return RedisRepository{
80
		RedisConn: redisConn,
81
	}
82
}
83