Skip to content

Commit

Permalink
Merge pull request #254 from e154/develop
Browse files Browse the repository at this point in the history
Technical debt (#253)
  • Loading branch information
e154 authored Jan 2, 2024
2 parents abcace9 + ab7997d commit 355dd9c
Show file tree
Hide file tree
Showing 108 changed files with 2,390 additions and 1,275 deletions.
2 changes: 0 additions & 2 deletions adaptors/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ func (n *Task) Update(ctx context.Context, ver *m.UpdateTask) (err error) {
Enabled: ver.Enabled,
Condition: ver.Condition,
AreaId: ver.AreaId,
CreatedAt: ver.CreatedAt,
UpdatedAt: ver.UpdatedAt,
}

//triggers
Expand Down
108 changes: 90 additions & 18 deletions adaptors/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ import (

// ITrigger ...
type ITrigger interface {
Add(ctx context.Context, ver *m.Trigger) (id int64, err error)
Add(ctx context.Context, ver *m.NewTrigger) (id int64, err error)
GetById(ctx context.Context, id int64) (metric *m.Trigger, err error)
Update(ctx context.Context, ver *m.Trigger) error
Update(ctx context.Context, ver *m.UpdateTrigger) error
Delete(ctx context.Context, deviceId int64) (err error)
List(ctx context.Context, limit, offset int64, orderBy, sort string, onlyEnabled bool) (list []*m.Trigger, total int64, err error)
Search(ctx context.Context, query string, limit, offset int) (list []*m.Trigger, total int64, err error)
Expand Down Expand Up @@ -63,8 +63,81 @@ func GetTriggerAdaptor(d *gorm.DB, orm *orm.Orm) ITrigger {
}

// Add ...
func (n *Trigger) Add(ctx context.Context, ver *m.Trigger) (id int64, err error) {
id, err = n.table.Add(ctx, n.toDb(ver))
func (n *Trigger) Add(ctx context.Context, ver *m.NewTrigger) (id int64, err error) {
dbVer := &db.Trigger{
Name: ver.Name,
Description: ver.Description,
ScriptId: ver.ScriptId,
AreaId: ver.AreaId,
PluginName: ver.PluginName,
Enabled: ver.Enabled,
}

// entities
for _, entityId := range ver.EntityIds {
dbVer.Entities = append(dbVer.Entities, &db.Entity{
Id: common.EntityId(entityId),
})
}

// serialize payload
b, _ := json.Marshal(m.TriggerPayload{
Obj: ver.Payload,
})
dbVer.Payload = string(b)
id, err = n.table.Add(ctx, dbVer)
return
}

// Update ...
func (n *Trigger) Update(ctx context.Context, ver *m.UpdateTrigger) (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
}
}()

dbVer := &db.Trigger{
Id: ver.Id,
Name: ver.Name,
Description: ver.Description,
ScriptId: ver.ScriptId,
AreaId: ver.AreaId,
PluginName: ver.PluginName,
Enabled: ver.Enabled,
}

// entities
for _, entityId := range ver.EntityIds {
dbVer.Entities = append(dbVer.Entities, &db.Entity{
Id: common.EntityId(entityId),
})
}

// serialize payload
b, _ := json.Marshal(m.TriggerPayload{
Obj: ver.Payload,
})
dbVer.Payload = string(b)

table := db.Triggers{Db: tx}
if err = table.DeleteEntity(ctx, dbVer.Id); err != nil {
return
}

err = table.Update(ctx, dbVer)

return
}

Expand All @@ -88,11 +161,6 @@ func (n *Trigger) GetByIdWithData(ctx context.Context, id int64, from, to *time.
return
}

// Update ...
func (n *Trigger) Update(ctx context.Context, ver *m.Trigger) error {
return n.table.Update(ctx, n.toDb(ver))
}

// Delete ...
func (n *Trigger) Delete(ctx context.Context, deviceId int64) (err error) {
err = n.table.Delete(ctx, deviceId)
Expand Down Expand Up @@ -146,7 +214,7 @@ func (n *Trigger) fromDb(dbVer *db.Trigger) (ver *m.Trigger) {
Id: dbVer.Id,
Name: dbVer.Name,
Description: dbVer.Description,
EntityId: dbVer.EntityId,
Entities: make([]*m.Entity, 0, len(dbVer.Entities)),
ScriptId: dbVer.ScriptId,
AreaId: dbVer.AreaId,
PluginName: dbVer.PluginName,
Expand All @@ -159,10 +227,12 @@ func (n *Trigger) fromDb(dbVer *db.Trigger) (ver *m.Trigger) {
scriptAdaptor := GetScriptAdaptor(n.db)
ver.Script, _ = scriptAdaptor.fromDb(dbVer.Script)
}
// entity
if dbVer.Entity != nil {
// entities
if dbVer.Entities != nil {
entityAdaptor := GetEntityAdaptor(n.db, n.orm)
ver.Entity = entityAdaptor.fromDb(dbVer.Entity)
for _, entity := range dbVer.Entities {
ver.Entities = append(ver.Entities, entityAdaptor.fromDb(entity))
}
}
// aea
if dbVer.Area != nil {
Expand All @@ -183,7 +253,6 @@ func (n *Trigger) toDb(ver *m.Trigger) (dbVer *db.Trigger) {
Id: ver.Id,
Name: ver.Name,
Description: ver.Description,
EntityId: ver.EntityId,
ScriptId: ver.ScriptId,
AreaId: ver.AreaId,
PluginName: ver.PluginName,
Expand All @@ -196,14 +265,17 @@ func (n *Trigger) toDb(ver *m.Trigger) (dbVer *db.Trigger) {
dbVer.ScriptId = common.Int64(ver.Script.Id)
}

if ver.Entity != nil {
dbVer.EntityId = common.NewEntityId(ver.Entity.Id.String())
}

if ver.Area != nil {
dbVer.AreaId = common.Int64(ver.Area.Id)
}

// entities
for _, entity := range dbVer.Entities {
dbVer.Entities = append(dbVer.Entities, &db.Entity{
Id: entity.Id,
})
}

// serialize payload
b, _ := json.Marshal(m.TriggerPayload{
Obj: ver.Payload,
Expand Down
34 changes: 19 additions & 15 deletions api/api.swagger3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3328,16 +3328,16 @@ paths:
application/json:
schema:
type: object
required: [ name, description, pluginName, attributes, enabled ]
required: [ name, description, pluginName, attributes, enabled, entityIds ]
properties:
name:
type: string
description:
type: string
entity:
$ref: '#/components/schemas/apiEntity'
entityId:
type: string
entityIds:
type: array
items:
type: string
script:
$ref: '#/components/schemas/apiScript'
scriptId:
Expand Down Expand Up @@ -5830,16 +5830,16 @@ components:
format: int64
apiNewTriggerRequest:
type: object
required: [ name, description, pluginName, attributes, enabled ]
required: [ name, description, pluginName, attributes, enabled, entityIds ]
properties:
name:
type: string
description:
type: string
entity:
$ref: '#/components/schemas/apiEntity'
entityId:
type: string
entityIds:
type: array
items:
type: string
script:
$ref: '#/components/schemas/apiScript'
scriptId:
Expand Down Expand Up @@ -6364,7 +6364,7 @@ components:
format: int32
apiTrigger:
type: object
required: [ id, name, description, pluginName, enabled, createdAt, updatedAt, attributes ]
required: [ id, name, description, pluginName, enabled, createdAt, updatedAt, attributes, entities, entityIds ]
properties:
id:
type: integer
Expand All @@ -6373,10 +6373,14 @@ components:
type: string
description:
type: string
entity:
$ref: '#/components/schemas/apiEntity'
entityId:
type: string
entities:
type: array
items:
$ref: '#/components/schemas/apiEntityShort'
entityIds:
type: array
items:
type: string
script:
$ref: '#/components/schemas/apiScript'
scriptId:
Expand Down
22 changes: 14 additions & 8 deletions api/dto/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func NewTriggerDto() Trigger {
}

// AddTrigger ...
func (r Trigger) AddTrigger(from *stub.ApiNewTriggerRequest) (action *m.Trigger) {
action = &m.Trigger{
func (r Trigger) AddTrigger(from *stub.ApiNewTriggerRequest) (action *m.NewTrigger) {
action = &m.NewTrigger{
Name: from.Name,
Description: from.Description,
EntityId: common.NewEntityIdFromPtr(from.EntityId),
EntityIds: from.EntityIds,
ScriptId: from.ScriptId,
AreaId: from.AreaId,
PluginName: from.PluginName,
Expand All @@ -48,12 +48,12 @@ func (r Trigger) AddTrigger(from *stub.ApiNewTriggerRequest) (action *m.Trigger)
}

// UpdateTrigger ...
func (r Trigger) UpdateTrigger(from *stub.TriggerServiceUpdateTriggerJSONBody, id int64) (action *m.Trigger) {
action = &m.Trigger{
func (r Trigger) UpdateTrigger(from *stub.TriggerServiceUpdateTriggerJSONBody, id int64) (action *m.UpdateTrigger) {
action = &m.UpdateTrigger{
Id: id,
Name: from.Name,
Description: from.Description,
EntityId: common.NewEntityIdFromPtr(from.EntityId),
EntityIds: from.EntityIds,
ScriptId: from.ScriptId,
AreaId: from.AreaId,
PluginName: from.PluginName,
Expand Down Expand Up @@ -104,8 +104,8 @@ func ToTrigger(trigger *m.Trigger) (obj *stub.ApiTrigger) {
Id: trigger.Id,
Name: trigger.Name,
Description: trigger.Description,
Entity: ToEntity(trigger.Entity),
EntityId: trigger.EntityId.StringPtr(),
Entities: make([]stub.ApiEntityShort, 0, len(trigger.Entities)),
EntityIds: make([]string, 0, len(trigger.Entities)),
Script: GetStubScript(trigger.Script),
ScriptId: trigger.ScriptId,
AreaId: trigger.AreaId,
Expand All @@ -117,5 +117,11 @@ func ToTrigger(trigger *m.Trigger) (obj *stub.ApiTrigger) {
CreatedAt: trigger.CreatedAt,
UpdatedAt: trigger.UpdatedAt,
}
for _, entity := range trigger.Entities {
obj.Entities = append(obj.Entities, stub.ApiEntityShort{
Id: entity.Id.String(),
})
obj.EntityIds = append(obj.EntityIds, entity.Id.String())
}
return
}
Loading

0 comments on commit 355dd9c

Please sign in to comment.