forked from kyma-incubator/compass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dao.go
120 lines (105 loc) · 2.74 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
package application
import (
"context"
"github.com/astaxie/beego/orm"
"github.com/kyma-incubator/compass/docs/investigations/storage/sql-toolbox/libs/beego/internal/db"
"github.com/kyma-incubator/compass/docs/investigations/storage/sql-toolbox/libs/beego/internal/dto"
"github.com/kyma-incubator/compass/docs/investigations/storage/sql-toolbox/libs/beego/internal/model"
"github.com/pkg/errors"
)
func NewApplicationDao(ormer orm.Ormer) *Dao {
return &Dao{
ormer: ormer,
}
}
type Dao struct {
ormer orm.Ormer
}
func (d *Dao) GetApplications(ctx context.Context, p model.PageRequest, sel model.Filer) (*model.ApplicationPage, error) {
var apps []dto.ApplicationDTO
qs := d.ormer.QueryTable(apps)
_, err := qs.Limit(p.PageSize).OrderBy("id").All(&apps)
if err != nil {
return nil, err
}
// TODO not nice at all: qs.Filter("name__contains", "slene")
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
appDTO := dto.AppFromModel(app)
// TODO this is soo bad, because if I forget to commit transaction it can be extended to other calls IMO
// in other approaches, there is a separate object for transactions!!!
err = d.ormer.Begin()
if err != nil {
return nil, err
}
if _, err := d.ormer.Insert(appDTO); err != nil {
d.ormer.Rollback()
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 {
// d.ormer.Rollback()
// return nil, err
// }
// apiDTO.ID = id
// a.ID = id
// apiDTO.AppID = app.ID
//
// if _, err := d.ormer.Insert(apiDTO); err != nil {
// d.ormer.Rollback()
// return nil, err
// }
// }
//}
//
//if app.Documents.Data != nil {
// for _, doc := range app.Documents.Data {
// dDTO := dto.DocumentFromModel(app.ID, doc)
// id, err := idp.GenID()
// if err != nil {
// d.ormer.Rollback()
// return nil, err
// }
// dDTO.ID = id
// doc.ID = id
//
// if _, err := d.ormer.Insert(dDTO); err != nil {
// d.ormer.Rollback()
// return nil, err
// }
// }
//}
if err := d.ormer.Commit(); err != nil {
return nil, err
}
return &app, nil
}
func (d *Dao) DeleteApplication(ctx context.Context, id string) (bool, error) {
rows, err := d.ormer.Delete(dto.ApplicationDTO{ID: id})
if err != nil {
return false, err
}
switch rows {
case 0:
return false, nil
case 1:
return true, nil
default:
return false, errors.New("wrong number of applications removed")
}
}