Passed
Pull Request — main (#166)
by Yume
02:23
created

services/deck/redisRepository.go   A

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 39
dl 0
loc 85
rs 10
c 0
b 0
f 0

14 Methods

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