forked from kyma-incubator/compass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.go
128 lines (104 loc) · 3.08 KB
/
dao.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
package application
import (
"context"
"fmt"
"github.com/jinzhu/gorm"
"github.com/kyma-incubator/compass/docs/investigations/storage/sql-toolbox/libs/gorm/internal/db"
"github.com/kyma-incubator/compass/docs/investigations/storage/sql-toolbox/libs/gorm/internal/dto"
"github.com/kyma-incubator/compass/docs/investigations/storage/sql-toolbox/libs/gorm/internal/model"
)
func NewApplicationDao(db *gorm.DB) *Dao {
return &Dao{
db: db,
}
}
type Dao struct {
db *gorm.DB
}
func (d *Dao) GetApplications(ctx context.Context, p model.PageRequest, sel model.Filer) (*model.ApplicationPage, error) {
// TODO NOTE When query with struct, GORM will only query with those fields has non-zero value, that means if your field’s value is 0, '', false or other zero values,
// it won’t be used to build query conditions, for example:
//db.Where(&User{Name: "jinzhu", Age: 0}).Find(&users)
////// SELECT * FROM users WHERE name = "jinzhu";
// TODO cool Count: !!! db.Where("name = ?", "jinzhu").Or("name = ?", "jinzhu 2").Find(&users).Count(&count)
var apps []dto.ApplicationDTO
// TODO error handling
if err := d.db.Limit(p.PageSize).Order("id").Find(&apps).Error; err != nil {
return nil, err
}
var out []model.Application
for _, app := range apps {
out = append(out, app.ToModel())
}
return &model.ApplicationPage{Data: out}, nil
}
func (d *Dao) CreateApplication(ctx context.Context, app model.Application) (*model.Application, error) {
idp := db.IDProvider{}
id, err := idp.GenID()
if err != nil {
return nil, err
}
app.ID = id
tx := d.db.Begin()
if tx.Error != nil {
return nil, err
}
defer func() {
tx.RollbackUnlessCommitted() //TODO cool
}()
if err := tx.Create(&app).Error; err != nil {
return nil, err
}
if app.Apis.Data != nil {
for _, a := range app.Apis.Data {
apiDTO := dto.APIFromModel(app.ID, a)
id, err := idp.GenID()
if err != nil {
return nil, err
}
apiDTO.ID = id
a.ID = id
apiDTO.AppID = app.ID
if err := tx.Create(&apiDTO).Error; err != nil {
return nil, err
}
}
}
if app.Documents.Data != nil {
for _, d := range app.Documents.Data {
dDTO := dto.DocumentFromModel(app.ID, d)
id, err := idp.GenID()
if err != nil {
return nil, err
}
dDTO.ID = id
d.ID = id
if err := tx.Create(&dDTO).Error; err != nil {
return nil, err
}
}
}
if err := tx.Commit().Error; err != nil {
return nil, err
}
return &app, nil
}
func (d *Dao) DeleteApplication(ctx context.Context, id string) (bool, error) {
// TODO WARNING When deleting a record, you need to ensure its primary field has value, and GORM will use the primary key to delete the record, if the primary key field is blank, GORM will delete all records for the model
// TODO we cannot use db.First because primary key is not an integer
app := dto.ApplicationDTO{ID: id}
res := d.db.Delete(app)
rows := res.RowsAffected
err := res.Error
if err != nil {
return false, err
}
switch rows {
case 0:
return false, nil
case 1:
return true, nil
default:
return false, fmt.Errorf("incorrect number of removed applications: %d", rows)
}
}