-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
script.go
208 lines (179 loc) · 5.57 KB
/
script.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
// 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"
"sort"
"gorm.io/gorm"
"github.com/e154/smart-home/db"
m "github.com/e154/smart-home/models"
)
// IScript ...
type IScript interface {
Add(ctx context.Context, script *m.Script) (id int64, err error)
GetById(ctx context.Context, scriptId int64) (script *m.Script, err error)
GetByName(ctx context.Context, name string) (script *m.Script, err error)
Update(ctx context.Context, script *m.Script) (err error)
Delete(ctx context.Context, scriptId int64) (err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string, query *string, ids *[]uint64) (list []*m.Script, total int64, err error)
Search(ctx context.Context, query string, limit, offset int64) (list []*m.Script, total int64, err error)
Statistic(ctx context.Context) (statistic *m.ScriptsStatistic, err error)
fromDb(dbScript *db.Script) (script *m.Script, err error)
toDb(script *m.Script) (dbScript *db.Script)
}
// Script ...
type Script struct {
IScript
table *db.Scripts
db *gorm.DB
}
// GetScriptAdaptor ...
func GetScriptAdaptor(d *gorm.DB) IScript {
return &Script{
table: &db.Scripts{Db: d},
db: d,
}
}
// Add ...
func (n *Script) Add(ctx context.Context, script *m.Script) (id int64, err error) {
dbScript := n.toDb(script)
id, err = n.table.Add(ctx, dbScript)
return
}
// GetById ...
func (n *Script) GetById(ctx context.Context, scriptId int64) (script *m.Script, err error) {
var dbScript *db.Script
if dbScript, err = n.table.GetById(ctx, scriptId); err != nil {
return
}
script, _ = n.fromDb(dbScript)
return
}
// GetByName ...
func (n *Script) GetByName(ctx context.Context, name string) (script *m.Script, err error) {
var dbScript *db.Script
if dbScript, err = n.table.GetByName(ctx, name); err != nil {
return
}
script, _ = n.fromDb(dbScript)
return
}
// Update ...
func (n *Script) Update(ctx context.Context, script *m.Script) (err error) {
dbScript := n.toDb(script)
err = n.table.Update(ctx, dbScript)
return
}
// Delete ...
func (n *Script) Delete(ctx context.Context, scriptId int64) (err error) {
err = n.table.Delete(ctx, scriptId)
return
}
// List ...
func (n *Script) List(ctx context.Context, limit, offset int64, orderBy, sort string, query *string, ids *[]uint64) (list []*m.Script, total int64, err error) {
if sort == "" {
sort = "id"
}
if orderBy == "" {
orderBy = "desc"
}
var dbList []*db.Script
if dbList, total, err = n.table.List(ctx, int(limit), int(offset), orderBy, sort, query, ids); err != nil {
return
}
list = make([]*m.Script, 0)
for _, dbScript := range dbList {
script, _ := n.fromDb(dbScript)
list = append(list, script)
}
return
}
// Search ...
func (n *Script) Search(ctx context.Context, query string, limit, offset int64) (list []*m.Script, total int64, err error) {
var dbList []*db.Script
if dbList, total, err = n.table.Search(ctx, query, int(limit), int(offset)); err != nil {
return
}
list = make([]*m.Script, 0)
for _, dbScript := range dbList {
dev, _ := n.fromDb(dbScript)
list = append(list, dev)
}
return
}
func (n *Script) Statistic(ctx context.Context) (statistic *m.ScriptsStatistic, err error) {
var dbVer *db.ScriptsStatistic
if dbVer, err = n.table.Statistic(ctx); err != nil {
return
}
statistic = &m.ScriptsStatistic{
Total: dbVer.Total,
Used: dbVer.Used,
Unused: dbVer.Unused,
CoffeeScript: dbVer.CoffeeScript,
TypeScript: dbVer.TypeScript,
JavaScript: dbVer.JavaScript,
}
return
}
func (n *Script) fromDb(dbVer *db.Script) (ver *m.Script, err error) {
ver = &m.Script{
Id: dbVer.Id,
Lang: dbVer.Lang,
Name: dbVer.Name,
Source: dbVer.Source,
Description: dbVer.Description,
Compiled: dbVer.Compiled,
CreatedAt: dbVer.CreatedAt,
UpdatedAt: dbVer.UpdatedAt,
Info: &m.ScriptInfo{
AlexaIntents: dbVer.AlexaIntents,
EntityActions: dbVer.EntityActions,
EntityScripts: dbVer.EntityScripts,
AutomationTriggers: dbVer.AutomationTriggers,
AutomationConditions: dbVer.AutomationConditions,
AutomationActions: dbVer.AutomationActions,
},
}
if dbVer.Versions != nil {
ver.Versions = make([]*m.ScriptVersion, 0, len(dbVer.Versions))
for _, version := range dbVer.Versions {
ver.Versions = append(ver.Versions, &m.ScriptVersion{
Id: version.Id,
Lang: version.Lang,
Source: version.Source,
CreatedAt: version.CreatedAt,
})
}
sort.Sort(ver.Versions)
}
return
}
func (n *Script) toDb(script *m.Script) (dbVer *db.Script) {
dbVer = &db.Script{
Id: script.Id,
Lang: script.Lang,
Name: script.Name,
Source: script.Source,
Description: script.Description,
Compiled: script.Compiled,
CreatedAt: script.CreatedAt,
UpdatedAt: script.UpdatedAt,
}
return
}