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

services/user/usecase.go   A

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 26
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A user.UseCase.GetByID 0 18 3
A user.NewUseCase 0 2 1
A user.UseCase.GetName 0 4 1
1
package user
2
3
import (
4
	"context"
5
6
	"github.com/memnix/memnix-rest/domain"
7
	"github.com/memnix/memnix-rest/infrastructures"
8
	"github.com/memnix/memnix-rest/pkg/utils"
9
)
10
11
// UseCase is the user use case.
12
type UseCase struct {
13
	IRepository
14
	IRedisRepository
15
	IRistrettoRepository
16
}
17
18
// GetName returns the name of the user with the given id.
19
func (u UseCase) GetName(ctx context.Context, id string) string {
20
	uintID, _ := utils.ConvertStrToUInt(id)
21
22
	return u.IRepository.GetName(ctx, uintID)
23
}
24
25
// GetByID returns the user with the given id.
26
func (u UseCase) GetByID(ctx context.Context, id uint) (domain.User, error) {
27
	_, span := infrastructures.GetTracerInstance().Tracer().Start(ctx, "GetUserByID")
28
	defer span.End()
29
30
	var userObject domain.User
31
32
	if risrettoHit, err := u.IRistrettoRepository.Get(ctx, id); err == nil {
33
		return risrettoHit, nil
34
	}
35
36
	userObject, err := u.IRepository.GetByID(ctx, id)
37
	if err != nil {
38
		return domain.User{}, err
39
	}
40
41
	_ = u.IRistrettoRepository.Set(ctx, userObject)
42
43
	return userObject, nil
44
}
45
46
// NewUseCase returns a new user use case.
47
func NewUseCase(repo IRepository, redis IRedisRepository, ristretto IRistrettoRepository) IUseCase {
48
	return &UseCase{IRepository: repo, IRedisRepository: redis, IRistrettoRepository: ristretto}
49
}
50