/
nodeSensor.go
76 lines (64 loc) · 1.69 KB
/
nodeSensor.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
package dao
import (
"fmt"
"time"
"github.com/naufalfmm/project-iot/common/consts"
"gorm.io/gorm"
nodeSensorDTO "github.com/naufalfmm/project-iot/model/dto/nodeSensor"
)
type NodeSensor struct {
ID uint64 `gorm:"PRIMARY_KEY"`
Code string `gorm:"not null"`
Description string
Category string `gorm:"not null"`
Unit string `gorm:"not null"`
GroupLabel string `gorm:"not null"`
GroupTh uint32 `gorm:"not null"`
NodeID uint64 `gorm:"not null"`
IsActive bool `gorm:"not null"`
BaseModelSoftDeleted
}
func (NodeSensor) TableName() string {
return "node_sensors"
}
func (ns *NodeSensor) BeforeCreate(tx *gorm.DB) error {
if ns.Code == "" {
ns.Code = fmt.Sprintf("%d-%d-%s", ns.NodeID, ns.GroupTh, ns.Category)
}
if ns.Unit == "" {
ns.Unit = consts.UnitSensorStandard[ns.Category]
}
return nil
}
func NewSensorFromCreateDTO(c nodeSensorDTO.CreateDTO, groupTh uint32) NodeSensor {
now := time.Now()
return NodeSensor{
Code: c.Code,
Description: c.Description,
Category: c.Category,
Unit: c.Unit,
GroupLabel: c.GroupLabel,
GroupTh: groupTh,
NodeID: c.NodeID,
IsActive: true,
BaseModelSoftDeleted: BaseModelSoftDeleted{
CreatedAt: now,
CreatedBy: c.By,
},
}
}
func (ns NodeSensor) ToResponseDTO() nodeSensorDTO.ResponseDTO {
return nodeSensorDTO.ResponseDTO{
ID: ns.ID,
Code: ns.Code,
Description: ns.Description,
Category: ns.Category,
Unit: ns.Unit,
GroupLabel: ns.GroupLabel,
GroupTh: ns.GroupTh,
NodeID: ns.NodeID,
IsActive: ns.IsActive,
CreatedAt: ns.BaseModelSoftDeleted.CreatedAt,
CreatedBy: ns.BaseModelSoftDeleted.CreatedBy,
}
}