Skip to content

Commit

Permalink
issues #64 fix update map element type
Browse files Browse the repository at this point in the history
  • Loading branch information
alekseyf12 committed Sep 9, 2019
1 parent 1db4e04 commit a58c45f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion adaptors/map_device.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package adaptors

import (
"github.com/jinzhu/gorm"
"github.com/e154/smart-home/db"
m "github.com/e154/smart-home/models"
"github.com/jinzhu/gorm"
)

type MapDevice struct {
Expand Down
30 changes: 24 additions & 6 deletions adaptors/map_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,68 @@ func GetMapElementAdaptor(d *gorm.DB) *MapElement {

func (n *MapElement) Add(ver *m.MapElement) (id int64, err error) {

tx := n.db.Begin()
if err = tx.Error; err != nil {
return
}

switch {
case ver.Prototype.MapText != nil:
textAdaptor := GetMapTextAdaptor(n.db)
textAdaptor := GetMapTextAdaptor(tx)
ver.PrototypeId, err = textAdaptor.Add(ver.Prototype.MapText)
ver.PrototypeType = common.PrototypeTypeText
case ver.Prototype.MapImage != nil:
imageAdaptor := GetMapImageAdaptor(n.db)
imageAdaptor := GetMapImageAdaptor(tx)
ver.PrototypeId, err = imageAdaptor.Add(ver.Prototype.MapImage)
ver.PrototypeType = common.PrototypeTypeImage
case ver.Prototype.MapDevice != nil:
deviceAdaptor := GetMapDeviceAdaptor(n.db)
deviceAdaptor := GetMapDeviceAdaptor(tx)
ver.Prototype.MapDevice.SystemName = ver.Name
if ver.PrototypeId, err = deviceAdaptor.Add(ver.Prototype.MapDevice); err != nil {
tx.Rollback()
return
}

ver.PrototypeType = common.PrototypeTypeDevice
//actions
deviceAction := GetMapDeviceActionAdaptor(n.db)
deviceAction := GetMapDeviceActionAdaptor(tx)
//err = deviceAction.AddMultiple(t.Actions)
for _, action := range ver.Prototype.MapDevice.Actions {
action.MapDeviceId = ver.PrototypeId
if action.Id, err = deviceAction.Add(action); err != nil {
log.Error(err.Error())
tx.Rollback()
return
}
}

//states
stateAdaptor := GetMapDeviceStateAdaptor(n.db)
stateAdaptor := GetMapDeviceStateAdaptor(tx)
//err = stateAdaptor.AddMultiple(t.States)
for _, state := range ver.Prototype.MapDevice.States {
state.MapDeviceId = ver.PrototypeId
if state.Id, err = stateAdaptor.Add(state); err != nil {
log.Error(err.Error())
tx.Rollback()
return
}
}
default:

}

if err != nil {
tx.Rollback()
return
}

dbVer := n.toDb(ver)
if id, err = n.table.Add(dbVer); err != nil {
table := db.MapElements{Db: tx}
if id, err = table.Add(dbVer); err != nil {
return
}

if err = tx.Commit().Error; err != nil {
return
}

Expand Down Expand Up @@ -137,6 +154,7 @@ func (n *MapElement) Update(ver *m.MapElement) (err error) {
ver.PrototypeId, err = imageAdaptor.Add(mapImage)
case common.PrototypeTypeDevice:
deviceAdaptor := GetMapDeviceAdaptor(tx)
ver.Prototype.MapDevice.SystemName = ver.Name
if ver.PrototypeId, err = deviceAdaptor.Add(ver.Prototype.MapDevice); err != nil {
log.Error(err.Error())
tx.Rollback()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- +migrate Up
-- SQL in section 'Up' is executed when this migration is applied

-- +migrate Down
-- SQL section 'Down' is executed when this migration is rolled back


0 comments on commit a58c45f

Please sign in to comment.