/
observation_service_storage.go
85 lines (71 loc) · 3.13 KB
/
observation_service_storage.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
package storage
import (
"context"
"errors"
"gorm.io/gorm"
dserrors "github.com/caraml-dev/timber/dataset-service/errors"
"github.com/caraml-dev/timber/dataset-service/model"
)
// observationServiceEntityName entity name for observation service
const observationServiceEntityName = "observation_service"
// ObservationService interface providing access for ObservationService storage
type ObservationService interface {
// Get an observation service given its identifier
Get(ctx context.Context, input GetInput) (*model.ObservationService, error)
// Create a new observation service and return the stored observation service with ID populated or error
Create(ctx context.Context, lw *model.ObservationService) (*model.ObservationService, error)
// Update an existing observation service and return the stored observation service or error
Update(ctx context.Context, lw *model.ObservationService) (*model.ObservationService, error)
// List all observation service given the list input
List(ctx context.Context, listInput ListInput) ([]*model.ObservationService, error)
}
type observationService struct {
db *gorm.DB
}
// NewObservationService creates new observation service storage
func NewObservationService(db *gorm.DB) ObservationService {
return &observationService{db: db}
}
// Get an observation service given its identifier
func (o *observationService) Get(ctx context.Context, input GetInput) (*model.ObservationService, error) {
observationService := &model.ObservationService{}
tx := o.db.WithContext(ctx).Where(&model.ObservationService{
Base: model.Base{
ID: input.ID,
ProjectID: input.ProjectID,
},
Name: input.Name,
}).Take(&observationService)
if tx.Error != nil && errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return observationService, dserrors.NewNotFoundError(observationServiceEntityName, input)
}
return observationService, tx.Error
}
// Create a new observation service and return the stored observation service with ID populated or error
func (o *observationService) Create(ctx context.Context, obs *model.ObservationService) (*model.ObservationService, error) {
tx := o.db.WithContext(ctx).Create(obs)
if tx.Error != nil && errors.As(tx.Error, &duplicateEntryError) {
// handle duplicate
return nil, dserrors.NewConflictError(observationServiceEntityName, tx.Error)
}
return obs, tx.Error
}
// Update an existing observation service and return the stored observation service or error
func (o *observationService) Update(ctx context.Context, obs *model.ObservationService) (*model.ObservationService, error) {
tx := o.db.WithContext(ctx).Updates(obs)
return obs, tx.Error
}
// List all observation services given the list input
func (o *observationService) List(ctx context.Context, listInput ListInput) ([]*model.ObservationService, error) {
var observationServices []*model.ObservationService
tx := o.db.WithContext(ctx).Where(&model.ObservationService{
Base: model.Base{
ProjectID: listInput.ProjectID,
},
}).Limit(listInput.Limit).
Offset(listInput.Offset).
// TODO: allow users to provide the ordering option
Order("id").
Find(&observationServices)
return observationServices, tx.Error
}