/
dashboard_card.go
232 lines (196 loc) · 5.49 KB
/
dashboard_card.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2016-2023, Filippov Alex
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see
// <https://www.gnu.org/licenses/>.
package adaptors
import (
"context"
"strings"
"github.com/e154/smart-home/db"
m "github.com/e154/smart-home/models"
"gorm.io/gorm"
)
type IDashboardCard interface {
Add(ctx context.Context, ver *m.DashboardCard) (id int64, err error)
GetById(ctx context.Context, mapId int64) (ver *m.DashboardCard, err error)
Update(ctx context.Context, ver *m.DashboardCard) (err error)
Delete(ctx context.Context, id int64) (err error)
Import(ctx context.Context, card *m.DashboardCard) (cardId int64, err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string) (list []*m.DashboardCard, total int64, err error)
fromDb(dbVer *db.DashboardCard) (ver *m.DashboardCard)
toDb(ver *m.DashboardCard) (dbVer *db.DashboardCard)
}
// DashboardCard ...
type DashboardCard struct {
IDashboardCard
table *db.DashboardCards
db *gorm.DB
}
// GetDashboardCardAdaptor ...
func GetDashboardCardAdaptor(d *gorm.DB) IDashboardCard {
return &DashboardCard{
table: &db.DashboardCards{Db: d},
db: d,
}
}
// Add ...
func (n *DashboardCard) Add(ctx context.Context, ver *m.DashboardCard) (id int64, err error) {
dbVer := n.toDb(ver)
id, err = n.table.Add(ctx, dbVer)
return
}
// GetById ...
func (n *DashboardCard) GetById(ctx context.Context, mapId int64) (ver *m.DashboardCard, err error) {
var dbVer *db.DashboardCard
if dbVer, err = n.table.GetById(ctx, mapId); err != nil {
return
}
ver = n.fromDb(dbVer)
return
}
// Update ...
func (n *DashboardCard) Update(ctx context.Context, ver *m.DashboardCard) (err error) {
transaction := true
tx := n.db.Begin()
if err = tx.Error; err != nil {
tx = n.db
transaction = false
}
defer func() {
if err != nil && transaction {
tx.Rollback()
return
}
if transaction {
err = tx.Commit().Error
}
}()
table := db.DashboardCards{Db: tx}
dbVer := n.toDb(ver)
if err = table.Update(ctx, dbVer); err != nil {
return
}
// items
itemAdaptor := GetDashboardCardItemAdaptor(tx)
for _, item := range ver.Items {
if err = itemAdaptor.Update(ctx, item); err != nil {
return
}
}
return
}
// Delete ...
func (n *DashboardCard) Delete(ctx context.Context, id int64) (err error) {
err = n.table.Delete(ctx, id)
return
}
// Import ...
func (n *DashboardCard) Import(ctx context.Context, card *m.DashboardCard) (cardId int64, err error) {
transaction := true
tx := n.db.Begin()
if err = tx.Error; err != nil {
tx = n.db
transaction = false
}
defer func() {
if err != nil && transaction {
tx.Rollback()
return
}
if transaction {
err = tx.Commit().Error
}
}()
cardAdaptor := GetDashboardCardAdaptor(tx)
cardItemAdaptor := GetDashboardCardItemAdaptor(tx)
card.Id = 0
if !strings.Contains(card.Title, "[IMPORTED]") {
card.Title = card.Title + " [IMPORTED]"
}
if cardId, err = cardAdaptor.Add(ctx, card); err != nil {
return
}
// items
if len(card.Items) > 0 {
for _, item := range card.Items {
item.Id = 0
item.DashboardCardId = cardId
if _, err = cardItemAdaptor.Add(ctx, item); err != nil {
return
}
}
}
return
}
// List ...
func (n *DashboardCard) List(ctx context.Context, limit, offset int64, orderBy, sort string) (list []*m.DashboardCard, total int64, err error) {
if sort == "" {
sort = "name"
}
if orderBy == "" {
orderBy = "desc"
}
var dbList []*db.DashboardCard
if dbList, total, err = n.table.List(ctx, int(limit), int(offset), orderBy, sort); err != nil {
return
}
list = make([]*m.DashboardCard, 0)
for _, dbVer := range dbList {
ver := n.fromDb(dbVer)
list = append(list, ver)
}
return
}
func (n *DashboardCard) fromDb(dbVer *db.DashboardCard) (ver *m.DashboardCard) {
ver = &m.DashboardCard{
Id: dbVer.Id,
Title: dbVer.Title,
Height: dbVer.Height,
Width: dbVer.Width,
Background: dbVer.Background,
Weight: dbVer.Weight,
Enabled: dbVer.Enabled,
DashboardTabId: dbVer.DashboardTabId,
Payload: dbVer.Payload,
EntityId: dbVer.EntityId,
Hidden: dbVer.Hidden,
CreatedAt: dbVer.CreatedAt,
UpdatedAt: dbVer.UpdatedAt,
}
// items
itemAdaptor := GetDashboardCardItemAdaptor(n.db)
for _, dbAction := range dbVer.Items {
item := itemAdaptor.fromDb(dbAction)
ver.Items = append(ver.Items, item)
}
return
}
func (n *DashboardCard) toDb(ver *m.DashboardCard) (dbVer *db.DashboardCard) {
dbVer = &db.DashboardCard{
Id: ver.Id,
Title: ver.Title,
Weight: ver.Weight,
Width: ver.Width,
Height: ver.Height,
Background: ver.Background,
Enabled: ver.Enabled,
DashboardTabId: ver.DashboardTabId,
Payload: ver.Payload,
Hidden: ver.Hidden,
EntityId: ver.EntityId,
}
return
}