-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepositoryImportServiceImpl.go
128 lines (114 loc) · 3.15 KB
/
RepositoryImportServiceImpl.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package implservice
import (
"context"
"github.com/bitwormhole/starter/markup"
"github.com/bitwormhole/wpm/server/data/dxo"
"github.com/bitwormhole/wpm/server/service"
"github.com/bitwormhole/wpm/server/web/dto"
"github.com/bitwormhole/wpm/server/web/vo"
)
// RepositoryImportServiceImpl ...
type RepositoryImportServiceImpl struct {
markup.Component `id:"RepositoryImportService"`
RepositoryService service.LocalRepositoryService `inject:"#LocalRepositoryService"`
RepoFinder service.LocalRepositoryFinder `inject:"#LocalRepositoryFinder"`
}
func (inst *RepositoryImportServiceImpl) _Impl() service.RepositoryImportService {
return inst
}
// Find ...
func (inst *RepositoryImportServiceImpl) Find(ctx context.Context, o1 *vo.RepositoryImport) (*vo.RepositoryImport, error) {
src := o1.Items
rb := &myRepositoryImportResultBuilder{}
for _, item := range src {
path := item.Path
repolist, err := inst.RepoFinder.Search(ctx, path, 8)
if err == nil {
rb.addItems(repolist, nil)
} else {
rb.add(item, err)
}
}
return rb.create()
}
// Save ...
func (inst *RepositoryImportServiceImpl) Save(ctx context.Context, vo1 *vo.RepositoryImport) (*vo.RepositoryImport, error) {
ser := inst.RepositoryService
src := vo1.Items
rb := &myRepositoryImportResultBuilder{}
for _, item := range src {
repo, err := ser.Insert(ctx, item)
if repo == nil {
repo = item
}
rb.add(repo, err)
}
return rb.create()
}
// Locate ...
func (inst *RepositoryImportServiceImpl) Locate(ctx context.Context, o1 *vo.RepositoryImport) (*vo.RepositoryImport, error) {
src := o1.Items
rb := &myRepositoryImportResultBuilder{}
for _, item := range src {
path := item.Path
repo, err := inst.RepoFinder.Locate(ctx, path)
if repo == nil {
repo = item
}
rb.add(repo, err)
}
return rb.create()
}
// FindOrLocate ...
func (inst *RepositoryImportServiceImpl) FindOrLocate(ctx context.Context, o1 *vo.RepositoryImport) (*vo.RepositoryImport, error) {
src := o1.Items
rb := &myRepositoryImportResultBuilder{}
for _, item := range src {
path := item.Path
// try locate
repo, err := inst.RepoFinder.Locate(ctx, path)
if err == nil {
rb.add(repo, nil)
continue
}
// try search
repolist, err := inst.RepoFinder.Search(ctx, path, 8)
if err == nil {
rb.addItems(repolist, err)
continue
}
// deep error
rb.add(item, err)
}
return rb.create()
}
////////////////////////////////////////////////////////////////////////////////
type myRepositoryImportResultBuilder struct {
items []*dto.LocalRepository
}
func (inst *myRepositoryImportResultBuilder) add(item *dto.LocalRepository, err error) {
if item == nil {
return
}
if err != nil {
item.Ready = false
item.State = dxo.FileStateOffline
} else {
item.Ready = true
}
inst.items = append(inst.items, item)
}
func (inst *myRepositoryImportResultBuilder) addItems(items []*dto.LocalRepository, err error) {
for _, item := range items {
inst.add(item, err)
}
}
func (inst *myRepositoryImportResultBuilder) create() (*vo.RepositoryImport, error) {
items := inst.items
if items == nil {
items = make([]*dto.LocalRepository, 0)
}
o := &vo.RepositoryImport{}
o.Items = items
return o, nil
}