-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbranch.go
238 lines (194 loc) · 5.87 KB
/
branch.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package models
import (
"context"
"time"
"github.com/GitDataAI/jiaozifs/utils/hash"
"github.com/google/uuid"
"github.com/uptrace/bun"
)
type Branch struct {
bun.BaseModel `bun:"table:branches"`
ID uuid.UUID `bun:"id,pk,type:uuid,default:uuid_generate_v4()" json:"id"`
// RepositoryId which repository this branch belong
RepositoryID uuid.UUID `bun:"repository_id,type:uuid,notnull" json:"repository_id"`
CommitHash hash.Hash `bun:"commit_hash,type:bytea,notnull" json:"commit_hash"`
// Path name/path of branch
Name string `bun:"name,notnull" json:"name"`
// Description
Description *string `bun:"description" json:"description,omitempty"`
// CreatorID who create this branch
CreatorID uuid.UUID `bun:"creator_id,type:uuid,notnull" json:"creator_id"`
CreatedAt time.Time `bun:"created_at,type:timestamp,notnull" json:"created_at"`
UpdatedAt time.Time `bun:"updated_at,type:timestamp,notnull" json:"updated_at"`
}
type GetBranchParams struct {
id uuid.UUID
repositoryID uuid.UUID
name *string
}
func NewGetBranchParams() *GetBranchParams {
return &GetBranchParams{}
}
func (gup *GetBranchParams) SetID(id uuid.UUID) *GetBranchParams {
gup.id = id
return gup
}
func (gup *GetBranchParams) SetRepositoryID(repositoryID uuid.UUID) *GetBranchParams {
gup.repositoryID = repositoryID
return gup
}
func (gup *GetBranchParams) SetName(name string) *GetBranchParams {
gup.name = &name
return gup
}
type DeleteBranchParams struct {
id uuid.UUID
repositoryID uuid.UUID
name *string
}
func NewDeleteBranchParams() *DeleteBranchParams {
return &DeleteBranchParams{}
}
func (gup *DeleteBranchParams) SetRepositoryID(repositoryID uuid.UUID) *DeleteBranchParams {
gup.repositoryID = repositoryID
return gup
}
func (gup *DeleteBranchParams) SetID(id uuid.UUID) *DeleteBranchParams {
gup.id = id
return gup
}
func (gup *DeleteBranchParams) SetName(name string) *DeleteBranchParams {
gup.name = &name
return gup
}
type UpdateBranchParams struct {
id uuid.UUID
commitHash hash.Hash
}
func NewUpdateBranchParams(id uuid.UUID) *UpdateBranchParams {
return &UpdateBranchParams{id: id}
}
func (up *UpdateBranchParams) SetCommitHash(commitHash hash.Hash) *UpdateBranchParams {
up.commitHash = commitHash
return up
}
type ListBranchParams struct {
RepositoryID uuid.UUID
Name *string
NameMatch MatchMode
After *string
Amount int
}
func NewListBranchParams() *ListBranchParams {
return &ListBranchParams{}
}
func (gup *ListBranchParams) SetRepositoryID(repositoryID uuid.UUID) *ListBranchParams {
gup.RepositoryID = repositoryID
return gup
}
func (gup *ListBranchParams) SetName(name string, match MatchMode) *ListBranchParams {
gup.Name = &name
gup.NameMatch = match
return gup
}
func (gup *ListBranchParams) SetAfter(after string) *ListBranchParams {
gup.After = &after
return gup
}
func (gup *ListBranchParams) SetAmount(amount int) *ListBranchParams {
gup.Amount = amount
return gup
}
type IBranchRepo interface {
Insert(ctx context.Context, repo *Branch) (*Branch, error)
UpdateByID(ctx context.Context, params *UpdateBranchParams) error
Get(ctx context.Context, id *GetBranchParams) (*Branch, error)
List(ctx context.Context, params *ListBranchParams) ([]*Branch, bool, error)
Delete(ctx context.Context, params *DeleteBranchParams) (int64, error)
}
var _ IBranchRepo = (*BranchRepo)(nil)
type BranchRepo struct {
db bun.IDB
}
func NewBranchRepo(db bun.IDB) IBranchRepo {
return &BranchRepo{db: db}
}
func (r BranchRepo) Insert(ctx context.Context, branch *Branch) (*Branch, error) {
_, err := r.db.NewInsert().Model(branch).Exec(ctx)
if err != nil {
return nil, err
}
return branch, nil
}
func (r BranchRepo) Get(ctx context.Context, params *GetBranchParams) (*Branch, error) {
repo := &Branch{}
query := r.db.NewSelect().Model(repo)
if uuid.Nil != params.id {
query = query.Where("id = ?", params.id)
}
if uuid.Nil != params.repositoryID {
query = query.Where("repository_id = ?", params.repositoryID)
}
if params.name != nil {
query = query.Where("name = ?", *params.name)
}
err := query.Limit(1).Scan(ctx)
if err != nil {
return nil, err
}
return repo, nil
}
func (r BranchRepo) List(ctx context.Context, params *ListBranchParams) ([]*Branch, bool, error) {
var branches []*Branch
query := r.db.NewSelect().Model(&branches)
if uuid.Nil != params.RepositoryID {
query = query.Where("repository_id = ?", params.RepositoryID)
}
if params.Name != nil {
switch params.NameMatch {
case ExactMatch:
query = query.Where("name = ?", *params.Name)
case PrefixMatch:
query = query.Where("name LIKE ?", *params.Name+"%")
case SuffixMatch:
query = query.Where("name LIKE ?", "%"+*params.Name)
case LikeMatch:
query = query.Where("name LIKE ?", "%"+*params.Name+"%")
}
}
query = query.Order("name ASC")
if params.After != nil {
query = query.Where("name > ?", *params.After)
}
err := query.Limit(params.Amount).Scan(ctx)
return branches, len(branches) == params.Amount, err
}
func (r BranchRepo) Delete(ctx context.Context, params *DeleteBranchParams) (int64, error) {
query := r.db.NewDelete().Model((*Branch)(nil))
if uuid.Nil != params.id {
query = query.Where("id = ?", params.id)
}
if uuid.Nil != params.repositoryID {
query = query.Where("repository_id = ?", params.repositoryID)
}
if params.name != nil {
query = query.Where("name = ?", *params.name)
}
sqlResult, err := query.Exec(ctx)
if err != nil {
return 0, err
}
affectedRows, err := sqlResult.RowsAffected()
if err != nil {
return 0, err
}
return affectedRows, err
}
func (r BranchRepo) UpdateByID(ctx context.Context, updateModel *UpdateBranchParams) error {
updateQuery := r.db.NewUpdate().Model((*Branch)(nil)).Where("id = ?", updateModel.id)
if updateModel.commitHash != nil {
updateQuery.Set("commit_hash = ?", updateModel.commitHash)
}
_, err := updateQuery.Exec(ctx)
return err
}