/
util.go
60 lines (48 loc) · 1.71 KB
/
util.go
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
package test_v2
import (
"context"
"database/sql"
"path/filepath"
"runtime"
"time"
infra "github.com/ArtefactGitHub/Go_P_Zo/internal/api/v2/infrastructure"
util "github.com/ArtefactGitHub/Go_P_Zo/internal/api/v2/presentation/utils"
"github.com/ArtefactGitHub/Go_P_Zo/internal/platform/myauth"
"github.com/ArtefactGitHub/Go_P_Zo/internal/platform/mycontext"
"github.com/ArtefactGitHub/Go_P_Zo/pkg/common"
)
type TrashScanner struct{}
func (TrashScanner) Scan(interface{}) error {
return nil
}
func currentDir() string {
_, pwd, _, _ := runtime.Caller(0)
return filepath.Dir(pwd)
}
func GetResourceId(params common.QueryMap, resourceKey string) int {
v, _ := util.GetResourceId(params, resourceKey)
return v
}
func GetResourceIdStr(params common.QueryMap, resourceKey string) string {
v, _ := util.GetResourceIdStr(params, resourceKey)
return v
}
func WithDBAndTokenContext(ctx context.Context, db *sql.DB, userID int, expiration time.Time) context.Context {
c := WithDBContext(ctx, db)
return WithTokenContext(c, userID, expiration)
}
func WithTXAndTokenContext(ctx context.Context, db *sql.DB, tx *sql.Tx, userID int, expiration time.Time) context.Context {
c := WithDBContext(ctx, db)
c = WithTXContext(c, tx)
return WithTokenContext(c, userID, expiration)
}
func WithDBContext(ctx context.Context, db *sql.DB) context.Context {
return context.WithValue(ctx, infra.KeyDB, db)
}
func WithTXContext(ctx context.Context, tx *sql.Tx) context.Context {
return context.WithValue(ctx, infra.KeyTX, tx)
}
func WithTokenContext(ctx context.Context, userID int, expiration time.Time) context.Context {
jwt, _ := myauth.CreateUserTokenJwt(userID, expiration)
return mycontext.NewContext(ctx, mycontext.UserTokenKey, jwt)
}