/
device_state.go
89 lines (63 loc) · 1.7 KB
/
device_state.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
package endpoint
import (
"github.com/e154/smart-home/system/validation"
m "github.com/e154/smart-home/models"
"github.com/e154/smart-home/common"
"errors"
)
type DeviceStateEndpoint struct {
*CommonEndpoint
}
func NewDeviceStateEndpoint(common *CommonEndpoint) *DeviceStateEndpoint {
return &DeviceStateEndpoint{
CommonEndpoint: common,
}
}
func (d *DeviceStateEndpoint) Add(params *m.DeviceState) (state *m.DeviceState, id int64, errs []*validation.Error, err error) {
// validation
_, errs = params.Valid()
if len(errs) > 0 {
return
}
if id, err = d.adaptors.DeviceState.Add(params); err != nil {
return
}
state, err = d.GetById(id)
return
}
func (d *DeviceStateEndpoint) GetById(id int64) (device *m.DeviceState, err error) {
device, err = d.adaptors.DeviceState.GetById(id)
return
}
func (d *DeviceStateEndpoint) Update(params *m.DeviceState) (state *m.DeviceState, errs []*validation.Error, err error) {
if state, err = d.adaptors.DeviceState.GetById(params.Id); err != nil {
return
}
common.Copy(&state, ¶ms)
// validation
_, errs = state.Valid()
if len(errs) > 0 {
return
}
if err = d.adaptors.DeviceState.Update(state); err != nil {
return
}
state, err = d.adaptors.DeviceState.GetById(state.Id)
return
}
func (d *DeviceStateEndpoint) Delete(id int64) (err error) {
if id == 0 {
err = errors.New("action id is null")
return
}
var device *m.DeviceState
if device, err = d.adaptors.DeviceState.GetById(id); err != nil {
return
}
err = d.adaptors.DeviceState.Delete(device.Id)
return
}
func (d *DeviceStateEndpoint) GetList(deviceId int64) (actions []*m.DeviceState, err error) {
actions, err = d.adaptors.DeviceState.GetByDeviceId(deviceId)
return
}