This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
project_repo.go
84 lines (71 loc) · 2.28 KB
/
project_repo.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
package gormimpl
import (
"context"
"google.golang.org/grpc/codes"
"github.com/lyft/flytestdlib/promutils"
"github.com/jinzhu/gorm"
"github.com/lyft/flyteadmin/pkg/common"
flyteAdminErrors "github.com/lyft/flyteadmin/pkg/errors"
"github.com/lyft/flyteadmin/pkg/repositories/errors"
"github.com/lyft/flyteadmin/pkg/repositories/interfaces"
"github.com/lyft/flyteadmin/pkg/repositories/models"
)
type ProjectRepo struct {
db *gorm.DB
errorTransformer errors.ErrorTransformer
metrics gormMetrics
}
func (r *ProjectRepo) Create(ctx context.Context, project models.Project) error {
timer := r.metrics.CreateDuration.Start()
tx := r.db.Create(&project)
timer.Stop()
if tx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
}
func (r *ProjectRepo) Get(ctx context.Context, projectID string) (models.Project, error) {
var project models.Project
timer := r.metrics.GetDuration.Start()
tx := r.db.Where(&models.Project{
Identifier: projectID,
}).First(&project)
timer.Stop()
if tx.Error != nil {
return models.Project{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
if tx.RecordNotFound() {
return models.Project{}, flyteAdminErrors.NewFlyteAdminErrorf(codes.NotFound, "project [%s] not found", projectID)
}
return project, nil
}
func (r *ProjectRepo) ListAll(ctx context.Context, sortParameter common.SortParameter) ([]models.Project, error) {
var projects []models.Project
var tx = r.db
if sortParameter != nil {
tx = tx.Order(sortParameter.GetGormOrderExpr())
}
tx = tx.Find(&projects)
if tx.Error != nil {
return nil, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return projects, nil
}
func NewProjectRepo(db *gorm.DB, errorTransformer errors.ErrorTransformer,
scope promutils.Scope) interfaces.ProjectRepoInterface {
metrics := newMetrics(scope)
return &ProjectRepo{
db: db,
errorTransformer: errorTransformer,
metrics: metrics,
}
}
func (r *ProjectRepo) UpdateProject(ctx context.Context, projectUpdate models.Project) error {
// Use gorm client to update the two fields that are changed.
writeTx := r.db.Model(&projectUpdate).Updates(projectUpdate)
// Return error if applies.
if writeTx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(writeTx.Error)
}
return nil
}