Skip to content

Commit e7c3c61

Browse files
author
Hasan Demir
committed
Add interfaces
1 parent 2558dd2 commit e7c3c61

File tree

11 files changed

+57
-28
lines changed

11 files changed

+57
-28
lines changed

app/app.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
type Container struct {
1616
Config *config.Config
17-
DBClient *connection.DBClient
17+
DBClient connection.DBClientI
1818
Repository *repositories.Repository
1919
Service *services.Service
2020
}
@@ -29,13 +29,18 @@ func Init() {
2929
C = &Container{}
3030
C.Config, _ = config.Init()
3131
C.DBClient, _ = connection.CreateClient(C.Config)
32-
C.Service = CreateService()
32+
C.Service = CreateService()
3333
C.Repository = repositories.CreateRepository(C.Service, C.DBClient, context.Background())
3434
}
3535

3636
func CreateService() *services.Service {
37+
38+
crypto := &cryptoservice.Crypto{}
39+
3740
return &services.Service{
38-
Crypto: &cryptoservice.Crypto{},
39-
Token: &token.Token{},
41+
Crypto: crypto,
42+
Token: &token.Token{
43+
Crypto: crypto,
44+
},
4045
}
4146
}

pkg/connection/connection.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ import (
1010
_ "github.com/mattn/go-sqlite3"
1111
)
1212

13+
type DBClientI interface{
14+
Client() *ent.Client
15+
Close() error
16+
}
1317
type DBClient struct {
14-
Client *ent.Client
18+
client *ent.Client
1519
}
1620

1721
// CreateScheme creates the scheme
1822
func (c *DBClient) CreateScheme(ctx context.Context) error {
19-
return c.Client.Schema.Create(ctx)
23+
return c.client.Schema.Create(ctx)
2024
}
2125

2226
// Close closes the client
2327
func (c *DBClient) Close() error {
24-
return c.Client.Close()
28+
return c.client.Close()
2529
}
2630

2731
// CreateClient creates the client to connect db
@@ -33,7 +37,7 @@ func CreateClient(config *config.Config) (*DBClient, error) {
3337
}
3438

3539
dbClient := &DBClient{
36-
Client: client,
40+
client: client,
3741
}
3842

3943
if (config.EnvType == "local") {
@@ -43,6 +47,11 @@ func CreateClient(config *config.Config) (*DBClient, error) {
4347
return dbClient, nil
4448
}
4549

50+
// Client returns the database client
51+
func (c *DBClient) Client() *ent.Client {
52+
return c.client
53+
}
54+
4655
// createConnectionString creates the connection string
4756
func createConnectionString(config *config.Config) string {
4857
if(config.Database.Connection == "sqlite3") {

pkg/crypto/crypto.go

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import (
66

77
"golang.org/x/crypto/bcrypt"
88
)
9+
type CryptoI interface {
10+
SHA256(plainText string) string
11+
HashPassword(password string) string
12+
CheckPasswordHash(password, hash string) bool
13+
}
914

1015
type Crypto struct {}
1116

pkg/services/services.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import (
77

88
type Service struct {
99
Crypto *cryptoservice.Crypto
10-
Token *token.Token
10+
Token token.TokenI
1111
}

pkg/token/token.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import (
88
"github.com/hsndmr/go-sanctum/pkg/random"
99
)
1010

11+
type TokenI interface {
12+
Create() (*NewToken, error)
13+
Split(token string) (string, string, error)
14+
}
15+
1116
type Token struct {
12-
Crypto *cryptoservice.Crypto
17+
Crypto cryptoservice.CryptoI
1318
}
1419

1520
// Create creates a new token

repositories/personal_access_token.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package repositories
22

33
import (
4-
"context"
54
"time"
65

76
"github.com/hsndmr/go-sanctum/ent"
8-
"github.com/hsndmr/go-sanctum/pkg/connection"
97
"github.com/hsndmr/go-sanctum/pkg/token"
108
)
119

@@ -16,12 +14,15 @@ type CreatePersonalAccessTokenDto struct {
1614
Abilities []string
1715
}
1816

17+
type PersonalAccessTokenRepositoryI interface {
18+
Create(dto *CreatePersonalAccessTokenDto) (*ent.PersonalAccessToken, error)
19+
}
1920
type PersonalAccessTokenRepository struct {
2021
*BaseRepository
21-
Token *token.Token
22+
Token token.TokenI
2223
}
2324
// CreatePersonalAccessToken creates a new personal access token
24-
func (p *PersonalAccessTokenRepository) Create(dto *CreatePersonalAccessTokenDto, db *connection.DBClient, ctx context.Context) (*ent.PersonalAccessToken, error) {
25+
func (p *PersonalAccessTokenRepository) Create(dto *CreatePersonalAccessTokenDto) (*ent.PersonalAccessToken, error) {
2526
expirationAt := time.Now().Add(time.Hour * 24 * 7)
2627
if(dto.ExpirationAt != nil) {
2728
expirationAt = *dto.ExpirationAt
@@ -33,13 +34,13 @@ func (p *PersonalAccessTokenRepository) Create(dto *CreatePersonalAccessTokenDto
3334
return nil, err
3435
}
3536

36-
personalAccessTokenCreate := db.Client.PersonalAccessToken.Create()
37+
personalAccessTokenCreate := p.db.Client().PersonalAccessToken.Create()
3738

3839
return personalAccessTokenCreate.
3940
SetName(dto.Name).
4041
SetUser(dto.User).
4142
SetToken(tokenItem.Hash).
4243
SetExpirationAt(expirationAt).
4344
SetAbilities(dto.Abilities).
44-
Save(ctx)
45+
Save(p.ctx)
4546
}

repositories/repository.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@ import (
88
)
99

1010
type BaseRepository struct {
11-
db *connection.DBClient
11+
db connection.DBClientI
1212
ctx context.Context
1313
}
1414

15-
1615
type Repository struct {
17-
User *UserRepository
18-
PersonalAccessToken *PersonalAccessTokenRepository
16+
User UserRepositoryI
17+
PersonalAccessToken PersonalAccessTokenRepositoryI
1918
}
2019

2120

22-
func CreateRepository(services *services.Service, db *connection.DBClient, ctx context.Context) *Repository {
21+
func CreateRepository(services *services.Service, db connection.DBClientI, ctx context.Context) *Repository {
2322

2423
bs := &BaseRepository{
2524
db: db,

repositories/user.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ type CreateUserDto struct {
1212
Email string
1313
Password string
1414
}
15+
16+
type UserRepositoryI interface {
17+
Create(dto *CreateUserDto) (*ent.User, error)
18+
FindByEmail(email string) (*ent.User, error)
19+
}
20+
1521
type UserRepository struct {
1622
*BaseRepository
1723
Crypto *cryptoservice.Crypto
1824
}
1925

2026
// Create creates a user and returns the user.
2127
func (r *UserRepository) Create(dto *CreateUserDto) (*ent.User, error) {
22-
return r.db.Client.User.
28+
return r.db.Client().User.
2329
Create().
2430
SetEmail(dto.Email).
2531
SetPassword(r.Crypto.HashPassword(dto.Password)).
@@ -29,7 +35,7 @@ func (r *UserRepository) Create(dto *CreateUserDto) (*ent.User, error) {
2935

3036
// FindByEmail finds a user by email and returns the user.
3137
func (r *UserRepository) FindByEmail(email string) (*ent.User, error) {
32-
return r.db.Client.User.
38+
return r.db.Client().User.
3339
Query().
3440
Where(user.Email(email)).
3541
Only(r.ctx)

tests/controllers/controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var router *gin.Engine
1616

1717
func TestMain(m *testing.M) {
1818
app.Init()
19-
defer app.C.DBClient.Client.Close()
19+
defer app.C.DBClient.Client().Close()
2020
router = routers.InitRouter()
2121
exitVal := m.Run()
2222
os.Exit(exitVal)

tests/repositories/personal_access_token_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package repositorytest
22

33
import (
4-
"context"
54
"testing"
65

76
"github.com/hsndmr/go-sanctum/app"
@@ -20,7 +19,7 @@ func TestCreatePersonalAccessToken(t *testing.T) {
2019
t.Run("it should create personal access token", func(t *testing.T) {
2120
token, err := patr.Create(&repositories.CreatePersonalAccessTokenDto{
2221
User: user,
23-
}, app.C.DBClient, context.Background())
22+
})
2423

2524
if err != nil {
2625
t.Errorf("failed creating personal access token: %s", err.Error())
@@ -37,7 +36,7 @@ func TestCreatePersonalAccessToken(t *testing.T) {
3736
token, _ := patr.Create(&repositories.CreatePersonalAccessTokenDto{
3837
User: user,
3938
Abilities: abilities,
40-
}, app.C.DBClient, context.Background())
39+
})
4140

4241
assert.Equal(t, token.Abilities, abilities, "the abilities should be equal")
4342
})

tests/repositories/repository_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func TestMain(m *testing.M) {
1212
app.Init()
13-
defer app.C.DBClient.Client.Close()
13+
defer app.C.DBClient.Client().Close()
1414
exitVal := m.Run()
1515
os.Exit(exitVal)
1616
}

0 commit comments

Comments
 (0)