@@ -9,16 +9,33 @@ import (
99 "opencsg.com/csghub-server/common/types"
1010)
1111
12- type ArgoWorkFlowStore struct {
12+ type argoWorkFlowStoreImpl struct {
1313 db * DB
1414}
1515
16- func NewArgoWorkFlowStore () * ArgoWorkFlowStore {
17- return & ArgoWorkFlowStore {
16+ type ArgoWorkFlowStore interface {
17+ FindByID (ctx context.Context , id int64 ) (WorkFlow ArgoWorkflow , err error )
18+ FindByTaskID (ctx context.Context , id string ) (WorkFlow ArgoWorkflow , err error )
19+ FindByUsername (ctx context.Context , username string , per , page int ) (WorkFlows []ArgoWorkflow , total int , err error )
20+ CreateWorkFlow (ctx context.Context , workFlow ArgoWorkflow ) (* ArgoWorkflow , error )
21+ // mainly for update status
22+ UpdateWorkFlow (ctx context.Context , workFlow ArgoWorkflow ) (* ArgoWorkflow , error )
23+ // delete workflow by id
24+ DeleteWorkFlow (ctx context.Context , id int64 ) error
25+ }
26+
27+ func NewArgoWorkFlowStore () ArgoWorkFlowStore {
28+ return & argoWorkFlowStoreImpl {
1829 db : defaultDB ,
1930 }
2031}
2132
33+ func NewArgoWorkFlowStoreWithDB (db * DB ) ArgoWorkFlowStore {
34+ return & argoWorkFlowStoreImpl {
35+ db : db ,
36+ }
37+ }
38+
2239type ArgoWorkflow struct {
2340 ID int64 `bun:",pk,autoincrement" json:"id"`
2441 Username string `bun:",notnull" json:"username"`
@@ -45,23 +62,23 @@ type ArgoWorkflow struct {
4562 FailuresURL string `bun:"," json:"failures_url"`
4663}
4764
48- func (s * ArgoWorkFlowStore ) FindByID (ctx context.Context , id int64 ) (WorkFlow ArgoWorkflow , err error ) {
65+ func (s * argoWorkFlowStoreImpl ) FindByID (ctx context.Context , id int64 ) (WorkFlow ArgoWorkflow , err error ) {
4966 err = s .db .Operator .Core .NewSelect ().Model (& WorkFlow ).Where ("id = ?" , id ).Scan (ctx , & WorkFlow )
5067 if err != nil {
5168 return
5269 }
5370 return
5471}
5572
56- func (s * ArgoWorkFlowStore ) FindByTaskID (ctx context.Context , id string ) (WorkFlow ArgoWorkflow , err error ) {
73+ func (s * argoWorkFlowStoreImpl ) FindByTaskID (ctx context.Context , id string ) (WorkFlow ArgoWorkflow , err error ) {
5774 err = s .db .Operator .Core .NewSelect ().Model (& WorkFlow ).Where ("task_id = ?" , id ).Scan (ctx , & WorkFlow )
5875 if err != nil {
5976 return
6077 }
6178 return
6279}
6380
64- func (s * ArgoWorkFlowStore ) FindByUsername (ctx context.Context , username string , per , page int ) (WorkFlows []ArgoWorkflow , total int , err error ) {
81+ func (s * argoWorkFlowStoreImpl ) FindByUsername (ctx context.Context , username string , per , page int ) (WorkFlows []ArgoWorkflow , total int , err error ) {
6582 query := s .db .Operator .Core .
6683 NewSelect ().
6784 Model (& WorkFlows ).
@@ -82,7 +99,7 @@ func (s *ArgoWorkFlowStore) FindByUsername(ctx context.Context, username string,
8299 return
83100}
84101
85- func (s * ArgoWorkFlowStore ) CreateWorkFlow (ctx context.Context , workFlow ArgoWorkflow ) (* ArgoWorkflow , error ) {
102+ func (s * argoWorkFlowStoreImpl ) CreateWorkFlow (ctx context.Context , workFlow ArgoWorkflow ) (* ArgoWorkflow , error ) {
86103 res , err := s .db .Core .NewInsert ().Model (& workFlow ).Exec (ctx , & workFlow )
87104 if err := assertAffectedOneRow (res , err ); err != nil {
88105 return nil , fmt .Errorf ("failed to save WorkFlow in db, error:%w" , err )
@@ -92,13 +109,13 @@ func (s *ArgoWorkFlowStore) CreateWorkFlow(ctx context.Context, workFlow ArgoWor
92109}
93110
94111// mainly for update status
95- func (s * ArgoWorkFlowStore ) UpdateWorkFlow (ctx context.Context , workFlow ArgoWorkflow ) (* ArgoWorkflow , error ) {
112+ func (s * argoWorkFlowStoreImpl ) UpdateWorkFlow (ctx context.Context , workFlow ArgoWorkflow ) (* ArgoWorkflow , error ) {
96113 _ , err := s .db .Core .NewUpdate ().Model (& workFlow ).WherePK ().Exec (ctx )
97114 return & workFlow , err
98115}
99116
100117// delete workflow by id
101- func (s * ArgoWorkFlowStore ) DeleteWorkFlow (ctx context.Context , id int64 ) error {
118+ func (s * argoWorkFlowStoreImpl ) DeleteWorkFlow (ctx context.Context , id int64 ) error {
102119 _ , err := s .db .Core .NewDelete ().Model (& ArgoWorkflow {}).Where ("id = ?" , id ).Exec (ctx )
103120 return err
104121}
0 commit comments