Skip to content

Commit 5de8541

Browse files
committed
feat: add aksk models
1 parent f266c0a commit 5de8541

File tree

6 files changed

+273
-4
lines changed

6 files changed

+273
-4
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require (
3434
github.com/google/uuid v1.5.0
3535
github.com/gorilla/sessions v1.2.2
3636
github.com/hashicorp/go-version v1.6.0
37+
github.com/hellofresh/health-go/v5 v5.5.2
3738
github.com/hnlq715/golang-lru v0.4.0
3839
github.com/ipfs/boxo v0.18.0
3940
github.com/ipfs/go-log/v2 v2.5.1
@@ -117,7 +118,6 @@ require (
117118
github.com/go-logr/logr v1.4.1 // indirect
118119
github.com/go-logr/stdr v1.2.2 // indirect
119120
github.com/go-openapi/jsonpointer v0.19.6 // indirect
120-
github.com/go-sql-driver/mysql v1.7.1 // indirect
121121
github.com/gogo/protobuf v1.3.2 // indirect
122122
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
123123
github.com/golang/protobuf v1.5.3 // indirect
@@ -134,7 +134,6 @@ require (
134134
github.com/hashicorp/golang-lru v1.0.2 // indirect
135135
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
136136
github.com/hashicorp/hcl v1.0.0 // indirect
137-
github.com/hellofresh/health-go/v5 v5.5.2 // indirect
138137
github.com/imdario/mergo v0.3.15 // indirect
139138
github.com/inconshreveable/mousetrap v1.1.0 // indirect
140139
github.com/invopop/yaml v0.2.0 // indirect

go.sum

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,6 @@ github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh
239239
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
240240
github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogBU=
241241
github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
242-
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
243-
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
244242
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
245243
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
246244
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
@@ -782,6 +780,7 @@ github.com/spf13/viper v1.17.0/go.mod h1:BmMMMLQXSbcHK6KAOiFLz0l5JHrU89OdIRHvsk0
782780
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
783781
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
784782
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
783+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
785784
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
786785
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
787786
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=

models/aksk.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package models
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/google/uuid"
8+
"github.com/uptrace/bun"
9+
)
10+
11+
type AkSk struct {
12+
bun.BaseModel `bun:"table:aksks"`
13+
ID uuid.UUID `bun:"id,pk,type:uuid,default:uuid_generate_v4()" json:"id"`
14+
// UserID ak/sk belong to user id
15+
UserID uuid.UUID `bun:"user_id,type:uuid,notnull" json:"user_id"`
16+
// AccessKey
17+
AccessKey string `bun:"access_key,unique,notnull" json:"access_key"`
18+
// SecretKey
19+
SecretKey string `bun:"secret_key,unique,notnull" json:"secret_key"`
20+
// Description
21+
Description *string `bun:"description" json:"description,omitempty"`
22+
23+
CreatedAt time.Time `bun:"created_at,type:timestamp,notnull" json:"created_at"`
24+
UpdatedAt time.Time `bun:"updated_at,type:timestamp,notnull" json:"updated_at"`
25+
}
26+
27+
type GetAkSkParams struct {
28+
id uuid.UUID
29+
accessKey *string
30+
}
31+
32+
func NewGetAkSkParams() *GetAkSkParams {
33+
return &GetAkSkParams{}
34+
}
35+
36+
func (gap *GetAkSkParams) SetID(id uuid.UUID) *GetAkSkParams {
37+
gap.id = id
38+
return gap
39+
}
40+
41+
func (gap *GetAkSkParams) SetAccessKey(ak string) *GetAkSkParams {
42+
gap.accessKey = &ak
43+
return gap
44+
}
45+
46+
type ListAkSkParams struct {
47+
userID uuid.UUID
48+
after *time.Time
49+
amount int
50+
}
51+
52+
func NewListAkSkParams() *ListAkSkParams {
53+
return &ListAkSkParams{}
54+
}
55+
56+
func (lap *ListAkSkParams) SetUserID(userID uuid.UUID) *ListAkSkParams {
57+
lap.userID = userID
58+
return lap
59+
}
60+
61+
func (lap *ListAkSkParams) SetAfter(after time.Time) *ListAkSkParams {
62+
lap.after = &after
63+
return lap
64+
}
65+
66+
func (lap *ListAkSkParams) SetAmount(amount int) *ListAkSkParams {
67+
lap.amount = amount
68+
return lap
69+
}
70+
71+
type DeleteAkSkParams struct {
72+
id uuid.UUID
73+
accessKey *string
74+
}
75+
76+
func NewDeleteAkSkParams() *DeleteAkSkParams {
77+
return &DeleteAkSkParams{}
78+
}
79+
80+
func (dap *DeleteAkSkParams) SetID(id uuid.UUID) *DeleteAkSkParams {
81+
dap.id = id
82+
return dap
83+
}
84+
85+
func (dap *DeleteAkSkParams) SetAccessKey(accessKey string) *DeleteAkSkParams {
86+
dap.accessKey = &accessKey
87+
return dap
88+
}
89+
90+
type IAkskRepo interface {
91+
Insert(ctx context.Context, asSk *AkSk) (*AkSk, error)
92+
Get(ctx context.Context, params *GetAkSkParams) (*AkSk, error)
93+
94+
List(ctx context.Context, params *ListAkSkParams) ([]*AkSk, bool, error)
95+
Delete(ctx context.Context, params *DeleteAkSkParams) (int64, error)
96+
}
97+
98+
var _ IAkskRepo = (*AkskRepo)(nil)
99+
100+
type AkskRepo struct {
101+
db bun.IDB
102+
}
103+
104+
func NewAkskRepo(db bun.IDB) IAkskRepo {
105+
return &AkskRepo{db: db}
106+
}
107+
108+
func (a AkskRepo) Insert(ctx context.Context, akSk *AkSk) (*AkSk, error) {
109+
_, err := a.db.NewInsert().Model(akSk).Exec(ctx)
110+
if err != nil {
111+
return nil, err
112+
}
113+
return akSk, nil
114+
}
115+
116+
func (a AkskRepo) Get(ctx context.Context, params *GetAkSkParams) (*AkSk, error) {
117+
repo := &AkSk{}
118+
query := a.db.NewSelect().Model(repo)
119+
120+
if uuid.Nil != params.id {
121+
query = query.Where("id = ?", params.id)
122+
}
123+
124+
if params.accessKey != nil {
125+
query = query.Where("access_key = ?", *params.accessKey)
126+
}
127+
128+
err := query.Limit(1).Scan(ctx)
129+
if err != nil {
130+
return nil, err
131+
}
132+
return repo, nil
133+
}
134+
135+
func (a AkskRepo) List(ctx context.Context, params *ListAkSkParams) ([]*AkSk, bool, error) {
136+
var branches []*AkSk
137+
query := a.db.NewSelect().Model(&branches)
138+
139+
if uuid.Nil != params.userID {
140+
query = query.Where("user_Id = ?", params.userID)
141+
}
142+
143+
query = query.Order("created_at DESC")
144+
if params.after != nil {
145+
query = query.Where("created_at < ?", *params.after)
146+
}
147+
148+
err := query.Limit(params.amount).Scan(ctx)
149+
return branches, len(branches) == params.amount, err
150+
}
151+
152+
func (a AkskRepo) Delete(ctx context.Context, params *DeleteAkSkParams) (int64, error) {
153+
query := a.db.NewDelete().Model((*AkSk)(nil))
154+
155+
if uuid.Nil != params.id {
156+
query = query.Where("id = ?", params.id)
157+
}
158+
159+
if params.accessKey != nil {
160+
query = query.Where("access_key = ?", *params.accessKey)
161+
}
162+
163+
sqlResult, err := query.Exec(ctx)
164+
if err != nil {
165+
return 0, err
166+
}
167+
affectedRows, err := sqlResult.RowsAffected()
168+
if err != nil {
169+
return 0, err
170+
}
171+
return affectedRows, err
172+
}

models/aksk_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package models_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/google/uuid"
8+
9+
"github.com/brianvoe/gofakeit/v6"
10+
"github.com/google/go-cmp/cmp"
11+
"github.com/jiaozifs/jiaozifs/models"
12+
"github.com/jiaozifs/jiaozifs/testhelper"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestAkskRepo_Delete(t *testing.T) {
17+
ctx := context.Background()
18+
closeDB, _, db := testhelper.SetupDatabase(ctx, t)
19+
defer closeDB()
20+
21+
repo := models.NewAkskRepo(db)
22+
23+
t.Run("insert and get ", func(t *testing.T) {
24+
akskModel := &models.AkSk{}
25+
require.NoError(t, gofakeit.Struct(akskModel))
26+
27+
aksk, err := repo.Insert(ctx, akskModel)
28+
require.NoError(t, err)
29+
30+
expectAksk, err := repo.Get(ctx, models.NewGetAkSkParams().SetAccessKey(aksk.AccessKey).SetID(aksk.ID))
31+
require.NoError(t, err)
32+
require.True(t, cmp.Equal(expectAksk, aksk, dbTimeCmpOpt))
33+
})
34+
t.Run("list", func(t *testing.T) {
35+
userID := uuid.New()
36+
for i := 0; i < 5; i++ {
37+
akskModel := &models.AkSk{}
38+
require.NoError(t, gofakeit.Struct(akskModel))
39+
akskModel.UserID = userID
40+
_, err := repo.Insert(ctx, akskModel)
41+
require.NoError(t, err)
42+
}
43+
44+
userID = uuid.New()
45+
for i := 0; i < 5; i++ {
46+
akskModel := &models.AkSk{}
47+
require.NoError(t, gofakeit.Struct(akskModel))
48+
akskModel.UserID = userID
49+
_, err := repo.Insert(ctx, akskModel)
50+
require.NoError(t, err)
51+
}
52+
53+
aksks, hasMore, err := repo.List(ctx, models.NewListAkSkParams().SetUserID(userID))
54+
require.NoError(t, err)
55+
require.False(t, hasMore)
56+
require.Len(t, aksks, 5)
57+
58+
aksks, hasMore, err = repo.List(ctx, models.NewListAkSkParams().SetUserID(userID).SetAmount(2))
59+
require.NoError(t, err)
60+
require.True(t, hasMore)
61+
require.Len(t, aksks, 2)
62+
})
63+
64+
t.Run("delete by id", func(t *testing.T) {
65+
akskModel := &models.AkSk{}
66+
require.NoError(t, gofakeit.Struct(akskModel))
67+
68+
aksk, err := repo.Insert(ctx, akskModel)
69+
require.NoError(t, err)
70+
71+
deleteRows, err := repo.Delete(ctx, models.NewDeleteAkSkParams().SetID(aksk.ID))
72+
require.NoError(t, err)
73+
require.Equal(t, int64(1), deleteRows)
74+
})
75+
76+
t.Run("delete by ak", func(t *testing.T) {
77+
akskModel := &models.AkSk{}
78+
require.NoError(t, gofakeit.Struct(akskModel))
79+
80+
aksk, err := repo.Insert(ctx, akskModel)
81+
require.NoError(t, err)
82+
83+
deleteRows, err := repo.Delete(ctx, models.NewDeleteAkSkParams().SetAccessKey(aksk.AccessKey))
84+
require.NoError(t, err)
85+
require.Equal(t, int64(1), deleteRows)
86+
})
87+
}

models/migrations/20210505110026_init_project.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ func init() {
8080
if err != nil {
8181
return err
8282
}
83+
//aksk
84+
_, err = db.NewCreateTable().
85+
Model((*models.AkSk)(nil)).
86+
Exec(ctx)
87+
if err != nil {
88+
return err
89+
}
8390
return err
8491
}, nil)
8592
}

models/repo.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type IRepo interface {
3535
BranchRepo() IBranchRepo
3636
RepositoryRepo() IRepositoryRepo
3737
WipRepo() IWipRepo
38+
AkskRepo() IAkskRepo
3839
}
3940

4041
type PgRepo struct {
@@ -92,3 +93,7 @@ func (repo *PgRepo) RepositoryRepo() IRepositoryRepo {
9293
func (repo *PgRepo) WipRepo() IWipRepo {
9394
return NewWipRepo(repo.db)
9495
}
96+
97+
func (repo *PgRepo) AkskRepo() IAkskRepo {
98+
return NewAkskRepo(repo.db)
99+
}

0 commit comments

Comments
 (0)