@@ -10,7 +10,6 @@ import (
10
10
"gorm.io/gorm"
11
11
12
12
"go-hexagonal/internal/adapter/repository"
13
- "go-hexagonal/internal/adapter/repository/mysql"
14
13
"go-hexagonal/internal/domain/model"
15
14
"go-hexagonal/internal/domain/repo"
16
15
)
@@ -25,14 +24,13 @@ func NewExample() *Example {
25
24
}
26
25
27
26
type Example struct {
28
- mysql.TransactionImpl `structs:"-"` // inheritance mysql transaction implement
29
- Id int `json:"id" gorm:"primarykey" structs:",omitempty,underline"`
30
- Name string `json:"name" structs:",omitempty,underline"`
31
- Alias string `json:"alias" structs:",omitempty,underline"`
32
- CreatedAt time.Time `json:"created_at" structs:",omitempty,underline"`
33
- UpdatedAt time.Time `json:"updated_at" structs:",omitempty,underline"`
34
- DeletedAt gorm.DeletedAt `json:"deleted_at" structs:",omitempty,underline"`
35
- ChangeMap map [string ]interface {} `json:"-" gorm:"-" structs:"-"`
27
+ Id int `json:"id" gorm:"primarykey" structs:",omitempty,underline"`
28
+ Name string `json:"name" structs:",omitempty,underline"`
29
+ Alias string `json:"alias" structs:",omitempty,underline"`
30
+ CreatedAt time.Time `json:"created_at" structs:",omitempty,underline"`
31
+ UpdatedAt time.Time `json:"updated_at" structs:",omitempty,underline"`
32
+ DeletedAt gorm.DeletedAt `json:"deleted_at" structs:",omitempty,underline"`
33
+ ChangeMap map [string ]interface {} `json:"-" gorm:"-" structs:"-"`
36
34
}
37
35
38
36
func (e Example ) TableName () string {
@@ -52,13 +50,7 @@ func (e *Example) Create(ctx context.Context, tr *repository.Transaction, model
52
50
return nil , errors .Wrap (err , "copier fail" )
53
51
}
54
52
55
- // conn db
56
- db , err := e .ConnDB (ctx , tr )
57
- if err != nil {
58
- return nil , err
59
- }
60
-
61
- // handle sql
53
+ db := tr .Conn (ctx )
62
54
err = db .Create (entity ).Error
63
55
if err != nil {
64
56
return nil , err
@@ -75,13 +67,7 @@ func (e *Example) Create(ctx context.Context, tr *repository.Transaction, model
75
67
func (e * Example ) Delete (ctx context.Context , tr * repository.Transaction , id int ) (err error ) {
76
68
entity := & Example {}
77
69
78
- // conn db
79
- db , err := e .ConnDB (ctx , tr )
80
- if err != nil {
81
- return err
82
- }
83
-
84
- // handle sql
70
+ db := tr .Conn (ctx )
85
71
err = db .Delete (entity , id ).Error
86
72
// hard delete
87
73
// err := tx.Unscoped().Delete(entity, Id).Error
@@ -97,29 +83,17 @@ func (e *Example) Update(ctx context.Context, tr *repository.Transaction, model
97
83
entity .ChangeMap = structs .Map (entity )
98
84
entity .ChangeMap ["updated_at" ] = time .Now ()
99
85
100
- // conn db
101
- db , err := e .ConnDB (ctx , tr )
102
- if err != nil {
103
- return err
104
- }
105
-
106
- // handle sql
107
- db .Table (entity .TableName ()).Where ("id = ? AND deleted_at IS NULL" , entity .Id ).Updates (entity .ChangeMap )
86
+ db := tr .Conn (ctx )
87
+ db = db .Table (entity .TableName ()).Where ("id = ? AND deleted_at IS NULL" , entity .Id ).Updates (entity .ChangeMap )
108
88
109
89
return db .Error
110
90
}
111
91
112
92
func (e * Example ) GetByID (ctx context.Context , tr * repository.Transaction , id int ) (domain * model.Example , err error ) {
113
93
entity := & Example {}
114
94
115
- // conn db
116
- db , err := e .ConnDB (ctx , tr )
117
- if err != nil {
118
- return nil , err
119
- }
120
-
121
- // handle sql
122
- db .Table (entity .TableName ()).Find (entity , id )
95
+ db := tr .Conn (ctx )
96
+ db = db .Table (entity .TableName ()).Find (entity , id )
123
97
124
98
if db .Error != nil {
125
99
return nil , err
@@ -136,13 +110,7 @@ func (e *Example) GetByID(ctx context.Context, tr *repository.Transaction, id in
136
110
func (e * Example ) FindByName (ctx context.Context , tr * repository.Transaction , name string ) (model * model.Example , err error ) {
137
111
entity := & Example {}
138
112
139
- // conn db
140
- db , err := e .ConnDB (ctx , tr )
141
- if err != nil {
142
- return nil , err
143
- }
144
-
145
- // handle sql
113
+ db := tr .Conn (ctx )
146
114
db .Table (entity .TableName ()).Where ("name = ?" , name ).Last (entity )
147
115
if db .Error != nil {
148
116
return nil , err
0 commit comments