Skip to content

Commit c06eed7

Browse files
committed
refactor: remove the dependency between dao and repo
Signed-off-by: saltbo <[email protected]>
1 parent a76ee8e commit c06eed7

File tree

11 files changed

+144
-126
lines changed

11 files changed

+144
-126
lines changed

cmd/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var serverCmd = &cobra.Command{
3232
Use: "server",
3333
Short: "A cloud disk base on the cloud service.",
3434
RunE: func(cmd *cobra.Command, args []string) error {
35-
s := app.NewServer()
35+
s := app.InitializeServer()
3636
return s.Run()
3737
},
3838
}

internal/app/dao/dao.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,21 @@ func Init(driver, dsn string) error {
3535
return nil
3636
}
3737

38-
func GetDBQuery() *query.Query {
39-
if viper.IsSet("installed") {
40-
if err := Init(viper.GetString("database.driver"), viper.GetString("database.dsn")); err != nil {
41-
log.Fatalln(err)
42-
}
43-
}
38+
type DBQueryFactory struct {
39+
}
4440

45-
return query.Use(gdb)
41+
func NewDBQueryFactory() *DBQueryFactory {
42+
return &DBQueryFactory{}
4643
}
4744

48-
func Transaction(fc func(tx *gorm.DB) error) error {
49-
return gdb.Transaction(fc)
45+
func (D *DBQueryFactory) Q() *query.Query {
46+
if !viper.IsSet("installed") {
47+
return nil
48+
}
49+
50+
if err := Init(viper.GetString("database.driver"), viper.GetString("database.dsn")); err != nil {
51+
log.Fatalln(err)
52+
}
53+
54+
return query.Use(gdb)
5055
}

internal/app/repo/matter.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,27 @@ type Matter interface {
4747
var _ Matter = (*MatterDBQuery)(nil)
4848

4949
type MatterDBQuery struct {
50-
q *query.Query
50+
DBQuery
5151
}
5252

53-
func NewMatterDBQuery(q *query.Query) *MatterDBQuery {
54-
return &MatterDBQuery{q: q}
53+
func NewMatterDBQuery(q DBQuery) *MatterDBQuery {
54+
return &MatterDBQuery{DBQuery: q}
5555
}
5656

5757
func (db *MatterDBQuery) Find(ctx context.Context, id int64) (*entity.Matter, error) {
58-
return db.q.Matter.WithContext(ctx).Where(db.q.Matter.Id.Eq(id)).First()
58+
return db.Q().Matter.WithContext(ctx).Where(db.Q().Matter.Id.Eq(id)).First()
5959
}
6060

6161
func (db *MatterDBQuery) FindWith(ctx context.Context, opt *MatterFindWithOption) (*entity.Matter, error) {
6262
conds := make([]gen.Condition, 0)
6363
if opt.Id != 0 {
64-
conds = append(conds, db.q.Matter.Id.Eq(opt.Id))
64+
conds = append(conds, db.Q().Matter.Id.Eq(opt.Id))
6565
}
6666
if opt.Alias != "" {
67-
conds = append(conds, db.q.Matter.Alias_.Eq(opt.Alias))
67+
conds = append(conds, db.Q().Matter.Alias_.Eq(opt.Alias))
6868
}
6969

70-
q := db.q.Matter.WithContext(ctx)
70+
q := db.Q().Matter.WithContext(ctx)
7171
if opt.Deleted {
7272
q = q.Unscoped()
7373
}
@@ -76,7 +76,7 @@ func (db *MatterDBQuery) FindWith(ctx context.Context, opt *MatterFindWithOption
7676
}
7777

7878
func (db *MatterDBQuery) FindByAlias(ctx context.Context, alias string) (*entity.Matter, error) {
79-
return db.q.Matter.WithContext(ctx).Where(db.q.Matter.Alias_.Eq(alias)).First()
79+
return db.Q().Matter.WithContext(ctx).Where(db.Q().Matter.Alias_.Eq(alias)).First()
8080
}
8181

8282
func (db *MatterDBQuery) PathExist(ctx context.Context, filepath string) bool {
@@ -92,41 +92,41 @@ func (db *MatterDBQuery) PathExist(ctx context.Context, filepath string) bool {
9292
parent, name = path.Split(filepath)
9393
}
9494

95-
conds := []gen.Condition{db.q.Matter.Name.Eq(name)}
95+
conds := []gen.Condition{db.Q().Matter.Name.Eq(name)}
9696
if parent != name {
97-
conds = append(conds, db.q.Matter.Parent.Eq(strings.TrimPrefix(parent, "/")))
97+
conds = append(conds, db.Q().Matter.Parent.Eq(strings.TrimPrefix(parent, "/")))
9898
}
9999

100-
_, err := db.q.Matter.WithContext(ctx).Where(conds...).First()
100+
_, err := db.Q().Matter.WithContext(ctx).Where(conds...).First()
101101
return err == nil
102102
}
103103

104104
func (db *MatterDBQuery) FindAll(ctx context.Context, opts *MatterListOption) ([]*entity.Matter, int64, error) {
105105
conds := make([]gen.Condition, 0)
106106
if opts.Uid != 0 {
107-
conds = append(conds, db.q.Matter.Uid.Eq(opts.Uid))
107+
conds = append(conds, db.Q().Matter.Uid.Eq(opts.Uid))
108108
}
109109
if opts.Sid != 0 {
110-
conds = append(conds, db.q.Matter.Sid.Eq(opts.Sid))
110+
conds = append(conds, db.Q().Matter.Sid.Eq(opts.Sid))
111111
}
112112

113113
if opts.Keyword != "" {
114-
conds = append(conds, db.q.Matter.Name.Like(fmt.Sprintf("%%%s%%", opts.Keyword)))
114+
conds = append(conds, db.Q().Matter.Name.Like(fmt.Sprintf("%%%s%%", opts.Keyword)))
115115
} else if !opts.Draft {
116-
conds = append(conds, db.q.Matter.Parent.Eq(opts.Dir))
116+
conds = append(conds, db.Q().Matter.Parent.Eq(opts.Dir))
117117
}
118118

119119
if opts.Type == "doc" {
120-
conds = append(conds, db.q.Matter.Type.In(entity.DocTypes...))
120+
conds = append(conds, db.Q().Matter.Type.In(entity.DocTypes...))
121121
} else if opts.Type != "" {
122-
conds = append(conds, db.q.Matter.Type.Like(fmt.Sprintf("%%%s%%", opts.Type)))
122+
conds = append(conds, db.Q().Matter.Type.Like(fmt.Sprintf("%%%s%%", opts.Type)))
123123
}
124124

125125
if !opts.Draft {
126-
conds = append(conds, db.q.Matter.UploadedAt.IsNotNull())
126+
conds = append(conds, db.Q().Matter.UploadedAt.IsNotNull())
127127
}
128128

129-
q := db.q.Matter.WithContext(ctx).Where(conds...).Order(db.q.Matter.DirType.Desc(), db.q.Matter.Id.Desc())
129+
q := db.Q().Matter.WithContext(ctx).Where(conds...).Order(db.Q().Matter.DirType.Desc(), db.Q().Matter.Id.Desc())
130130

131131
if opts.Limit == 0 {
132132
matters, err := q.Find()
@@ -148,7 +148,7 @@ func (db *MatterDBQuery) Create(ctx context.Context, m *entity.Matter) error {
148148
m.Name = strings.TrimSuffix(m.Name, ext) + suffix + ext
149149
}
150150

151-
return db.q.Matter.Create(m)
151+
return db.Q().Matter.Create(m)
152152
}
153153

154154
func (db *MatterDBQuery) Copy(ctx context.Context, id int64, to string) (*entity.Matter, error) {
@@ -165,7 +165,7 @@ func (db *MatterDBQuery) Copy(ctx context.Context, id int64, to string) (*entity
165165
newMatter.Parent = to
166166
if !em.IsDir() {
167167
// 如果是文件则只创建新的文件即可
168-
return newMatter, db.q.Matter.Create(newMatter)
168+
return newMatter, db.Q().Matter.Create(newMatter)
169169
}
170170

171171
// 如果是文件夹则查找所有子文件/文件夹一起复制
@@ -180,7 +180,7 @@ func (db *MatterDBQuery) Copy(ctx context.Context, id int64, to string) (*entity
180180
return newMatter
181181
})
182182

183-
return newMatter, db.q.Matter.Create(newMatters...)
183+
return newMatter, db.Q().Matter.Create(newMatters...)
184184
}
185185

186186
func (db *MatterDBQuery) Update(ctx context.Context, id int64, m *entity.Matter) error {
@@ -189,7 +189,7 @@ func (db *MatterDBQuery) Update(ctx context.Context, id int64, m *entity.Matter)
189189
return err
190190
}
191191

192-
return db.q.Transaction(func(tx *query.Query) error {
192+
return db.Q().Transaction(func(tx *query.Query) error {
193193
tq := tx.Matter.WithContext(ctx)
194194
if m.IsDir() {
195195
// 如果是目录,则需要把该目录下的子文件/目录一并修改
@@ -212,7 +212,7 @@ func (db *MatterDBQuery) Delete(ctx context.Context, id int64) error {
212212
}
213213

214214
m.TrashedBy = uuid.New().String()
215-
return db.q.Transaction(func(tx *query.Query) error {
215+
return db.Q().Transaction(func(tx *query.Query) error {
216216
tq := tx.Matter.WithContext(ctx)
217217
if m.IsDir() {
218218
// 如果是目录,则需要把该目录下的子文件/目录一并删除
@@ -234,7 +234,7 @@ func (db *MatterDBQuery) Delete(ctx context.Context, id int64) error {
234234
}
235235

236236
func (db *MatterDBQuery) Recovery(ctx context.Context, id int64) error {
237-
m, err := db.q.Matter.WithContext(ctx).Unscoped().Where(db.q.Matter.Id.Eq(id)).First()
237+
m, err := db.Q().Matter.WithContext(ctx).Unscoped().Where(db.Q().Matter.Id.Eq(id)).First()
238238
if err != nil {
239239
return err
240240
}
@@ -243,8 +243,8 @@ func (db *MatterDBQuery) Recovery(ctx context.Context, id int64) error {
243243
return fmt.Errorf("recovery: file parent[%s] not found", m.Parent)
244244
}
245245

246-
_, err = db.q.Matter.WithContext(ctx).Unscoped().Where(db.q.Matter.TrashedBy.Eq(m.TrashedBy)).
247-
UpdateSimple(db.q.Matter.TrashedBy.Value(""), db.q.Matter.DeletedAt.Null())
246+
_, err = db.Q().Matter.WithContext(ctx).Unscoped().Where(db.Q().Matter.TrashedBy.Eq(m.TrashedBy)).
247+
UpdateSimple(db.Q().Matter.TrashedBy.Value(""), db.Q().Matter.DeletedAt.Null())
248248
return err
249249
}
250250

@@ -271,10 +271,10 @@ func (db *MatterDBQuery) GetObjects(ctx context.Context, id int64) ([]string, er
271271
}
272272

273273
func (db *MatterDBQuery) findChildren(ctx context.Context, m *entity.Matter, withDeleted bool) ([]*entity.Matter, error) {
274-
q := db.q.Matter.WithContext(ctx)
274+
q := db.Q().Matter.WithContext(ctx)
275275
if withDeleted {
276276
q = q.Unscoped()
277277
}
278278

279-
return q.Where(db.q.Matter.Parent.Like(m.FullPath() + "%")).Find()
279+
return q.Where(db.Q().Matter.Parent.Like(m.FullPath() + "%")).Find()
280280
}

internal/app/repo/matter_test.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,15 @@ import (
44
"context"
55
"database/sql/driver"
66
"testing"
7-
"time"
87

98
"github.com/DATA-DOG/go-sqlmock"
109
"github.com/saltbo/zpan/internal/app/entity"
11-
"github.com/saltbo/zpan/internal/app/repo/query"
1210
"github.com/stretchr/testify/assert"
13-
"gorm.io/driver/mysql"
14-
"gorm.io/gorm"
1511
)
1612

17-
var nowFunc = func() time.Time {
18-
return time.Unix(0, 0)
19-
}
20-
21-
func newMockDB(t *testing.T) (sqlmock.Sqlmock, *gorm.DB) {
22-
rdb, mock, err := sqlmock.New()
23-
assert.NoError(t, err)
24-
gdb, err := gorm.Open(mysql.New(mysql.Config{Conn: rdb, DriverName: "mysql", SkipInitializeWithVersion: true}), &gorm.Config{
25-
NowFunc: nowFunc,
26-
})
27-
assert.NoError(t, err)
28-
return mock, gdb.Debug()
29-
}
30-
3113
func TestMatterDBQuery_PathExist(t *testing.T) {
32-
mock, gdb := newMockDB(t)
33-
q := NewMatterDBQuery(query.Use(gdb))
14+
mock, db := newMockDB(t)
15+
q := NewMatterDBQuery(db)
3416
mock.ExpectQuery("SELECT").WithArgs("to", "path/")
3517
q.PathExist(context.Background(), "/path/to/")
3618

@@ -83,7 +65,7 @@ func TestMatterDBQuery_Update(t *testing.T) {
8365

8466
for name, tc := range testCases {
8567
t.Run(name, func(t *testing.T) {
86-
mock, gdb := newMockDB(t)
68+
mock, db := newMockDB(t)
8769
mock.ExpectQuery("SELECT").WithArgs(tc.target.Id).
8870
WillReturnRows(tc.rows)
8971

@@ -97,7 +79,7 @@ func TestMatterDBQuery_Update(t *testing.T) {
9779
WillReturnResult(tc.expectMainResult)
9880
mock.ExpectCommit()
9981

100-
q := NewMatterDBQuery(query.Use(gdb))
82+
q := NewMatterDBQuery(db)
10183
ctx := context.Background()
10284
assert.NoError(t, q.Update(ctx, tc.target.Id, tc.target))
10385
})

internal/app/repo/recyclebin.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55

66
"github.com/saltbo/zpan/internal/app/entity"
7-
"github.com/saltbo/zpan/internal/app/repo/query"
87
)
98

109
type RecycleBinFindOptions struct {
@@ -23,19 +22,19 @@ type RecycleBin interface {
2322
var _ RecycleBin = (*RecycleBinDBQuery)(nil)
2423

2524
type RecycleBinDBQuery struct {
26-
q *query.Query
25+
DBQuery
2726
}
2827

29-
func NewRecycleBinDBQuery(q *query.Query) *RecycleBinDBQuery {
30-
return &RecycleBinDBQuery{q: q}
28+
func NewRecycleBinDBQuery(q DBQuery) *RecycleBinDBQuery {
29+
return &RecycleBinDBQuery{DBQuery: q}
3130
}
3231

3332
func (r *RecycleBinDBQuery) Find(ctx context.Context, alias string) (*entity.RecycleBin, error) {
34-
return r.q.RecycleBin.WithContext(ctx).Where(r.q.RecycleBin.Alias_.Eq(alias)).First()
33+
return r.Q().RecycleBin.WithContext(ctx).Where(r.Q().RecycleBin.Alias_.Eq(alias)).First()
3534
}
3635

3736
func (r *RecycleBinDBQuery) FindAll(ctx context.Context, opts *RecycleBinFindOptions) (rows []*entity.RecycleBin, total int64, err error) {
38-
q := r.q.RecycleBin.WithContext(ctx).Where(r.q.RecycleBin.Uid.Eq(opts.Uid), r.q.RecycleBin.Sid.Eq(opts.Sid)).Order(r.q.RecycleBin.Id.Desc())
37+
q := r.Q().RecycleBin.WithContext(ctx).Where(r.Q().RecycleBin.Uid.Eq(opts.Uid), r.Q().RecycleBin.Sid.Eq(opts.Sid)).Order(r.Q().RecycleBin.Id.Desc())
3938

4039
if opts.Limit == 0 {
4140
rows, err = q.Find()
@@ -46,7 +45,7 @@ func (r *RecycleBinDBQuery) FindAll(ctx context.Context, opts *RecycleBinFindOpt
4645
}
4746

4847
func (r *RecycleBinDBQuery) Create(ctx context.Context, m *entity.RecycleBin) error {
49-
return r.q.RecycleBin.WithContext(ctx).Create(m)
48+
return r.Q().RecycleBin.WithContext(ctx).Create(m)
5049
}
5150

5251
func (r *RecycleBinDBQuery) Delete(ctx context.Context, alias string) error {
@@ -55,6 +54,6 @@ func (r *RecycleBinDBQuery) Delete(ctx context.Context, alias string) error {
5554
return err
5655
}
5756

58-
_, err = r.q.RecycleBin.WithContext(ctx).Delete(m)
57+
_, err = r.Q().RecycleBin.WithContext(ctx).Delete(m)
5958
return err
6059
}

internal/app/repo/base.go renamed to internal/app/repo/shared.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package repo
22

33
import (
44
"context"
5+
6+
"github.com/saltbo/zpan/internal/app/repo/query"
57
)
68

79
type QueryPage struct {
@@ -44,3 +46,19 @@ type Updater[T comparable, ID IDType] interface {
4446
type Deleter[ID IDType] interface {
4547
Delete(ctx context.Context, id ID) error
4648
}
49+
50+
type DBQuery interface {
51+
Q() *query.Query
52+
}
53+
54+
type DBQueryFactory struct {
55+
q *query.Query
56+
}
57+
58+
func NewDBQueryFactory(q *query.Query) *DBQueryFactory {
59+
return &DBQueryFactory{q: q}
60+
}
61+
62+
func (f *DBQueryFactory) Q() *query.Query {
63+
return f.q
64+
}

internal/app/repo/shared_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package repo
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/DATA-DOG/go-sqlmock"
8+
"github.com/saltbo/zpan/internal/app/repo/query"
9+
"github.com/stretchr/testify/assert"
10+
"gorm.io/driver/mysql"
11+
"gorm.io/gorm"
12+
)
13+
14+
var nowFunc = func() time.Time {
15+
return time.Unix(0, 0)
16+
}
17+
18+
func newMockDB(t *testing.T) (sqlmock.Sqlmock, DBQuery) {
19+
rdb, mock, err := sqlmock.New()
20+
assert.NoError(t, err)
21+
gdb, err := gorm.Open(mysql.New(mysql.Config{Conn: rdb, DriverName: "mysql", SkipInitializeWithVersion: true}), &gorm.Config{
22+
NowFunc: nowFunc,
23+
})
24+
assert.NoError(t, err)
25+
return mock, NewDBQueryFactory(query.Use(gdb.Debug()))
26+
}

0 commit comments

Comments
 (0)