forked from KusionStack/kusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.go
149 lines (127 loc) · 3.83 KB
/
source.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
package persistence
import (
"context"
"gorm.io/gorm"
"kusionstack.io/kusion/pkg/domain/entity"
"kusionstack.io/kusion/pkg/domain/repository"
)
// The sourceRepository type implements the repository.SourceRepository interface.
// If the sourceRepository type does not implement all the methods of the interface,
// the compiler will produce an error.
var _ repository.SourceRepository = &sourceRepository{}
// sourceRepository is a repository that stores sources in a gorm database.
type sourceRepository struct {
// db is the underlying gorm database where sources are stored.
db *gorm.DB
}
// NewSourceRepository creates a new source repository.
func NewSourceRepository(db *gorm.DB) repository.SourceRepository {
return &sourceRepository{db: db}
}
// Create saves a source to the repository.
func (r *sourceRepository) Create(ctx context.Context, dataEntity *entity.Source) error {
// r.db.AutoMigrate(&SourceModel{})
err := dataEntity.Validate()
if err != nil {
return err
}
// Map the data from Entity to DO
var dataModel SourceModel
err = dataModel.FromEntity(dataEntity)
if err != nil {
return err
}
return r.db.Transaction(func(tx *gorm.DB) error {
// Create new record in the store
err = tx.WithContext(ctx).Create(&dataModel).Error
if err != nil {
return err
}
// Map fresh record's data into Entity
newEntity, err := dataModel.ToEntity()
if err != nil {
return err
}
*dataEntity = *newEntity
return nil
})
}
// Create saves a source to the repository.
func (r *sourceRepository) CreateOrUpdate(ctx context.Context, dataEntity *entity.Source) error {
err := dataEntity.Validate()
if err != nil {
return err
}
// Map the data from Entity to DO
var dataModel SourceModel
err = dataModel.FromEntity(dataEntity)
if err != nil {
return err
}
// Check if the source exists and update it if it does
if existingSource, err := r.GetByRemote(ctx, dataModel.Remote); err == nil && existingSource != nil {
return r.Update(ctx, dataEntity)
} else {
return r.Create(ctx, dataEntity)
}
}
// Delete removes a source from the repository.
func (r *sourceRepository) Delete(ctx context.Context, id uint) error {
return r.db.Transaction(func(tx *gorm.DB) error {
var dataModel SourceModel
err := tx.WithContext(ctx).First(&dataModel, id).Error
if err != nil {
return err
}
return tx.WithContext(ctx).Delete(&dataModel).Error
})
}
// Update updates an existing source in the repository.
func (r *sourceRepository) Update(ctx context.Context, dataEntity *entity.Source) error {
// Map the data from Entity to DO
var dataModel SourceModel
err := dataModel.FromEntity(dataEntity)
if err != nil {
return err
}
err = r.db.WithContext(ctx).Updates(&dataModel).Error
if err != nil {
return err
}
return nil
}
// Get retrieves a source by its ID.
func (r *sourceRepository) Get(ctx context.Context, id uint) (*entity.Source, error) {
var dataModel SourceModel
err := r.db.WithContext(ctx).First(&dataModel, id).Error
if err != nil {
return nil, err
}
return dataModel.ToEntity()
}
// GetByRemote retrieves a source by its remote.
func (r *sourceRepository) GetByRemote(ctx context.Context, remote string) (*entity.Source, error) {
var dataModel SourceModel
err := r.db.WithContext(ctx).Where("remote = ?", remote).First(&dataModel).Error
if err != nil {
return nil, err
}
return dataModel.ToEntity()
}
// List retrieves all sources.
func (r *sourceRepository) List(ctx context.Context) ([]*entity.Source, error) {
var dataModel []SourceModel
sourceEntityList := make([]*entity.Source, 0)
result := r.db.WithContext(ctx).Find(&dataModel)
if result.Error != nil {
return nil, result.Error
}
for _, source := range dataModel {
sourceEntity, err := source.ToEntity()
if err != nil {
return nil, err
}
sourceEntityList = append(sourceEntityList, sourceEntity)
}
return sourceEntityList, nil
}