github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/action.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package dto 20 21 import ( 22 stub "github.com/e154/smart-home/api/stub" 23 "github.com/e154/smart-home/common" 24 m "github.com/e154/smart-home/models" 25 ) 26 27 // Action ... 28 type Action struct{} 29 30 // NewActionDto ... 31 func NewActionDto() Action { 32 return Action{} 33 } 34 35 // Add ... 36 func (r Action) Add(from *stub.ApiNewActionRequest) (action *m.Action) { 37 action = &m.Action{ 38 Name: from.Name, 39 Description: from.Description, 40 ScriptId: from.ScriptId, 41 AreaId: from.AreaId, 42 EntityId: common.NewEntityIdFromPtr(from.EntityId), 43 EntityActionName: from.EntityActionName, 44 } 45 return 46 } 47 48 // Update ... 49 func (r Action) Update(from *stub.ActionServiceUpdateActionJSONBody, id int64) (action *m.Action) { 50 action = &m.Action{ 51 Id: id, 52 Name: from.Name, 53 Description: from.Description, 54 ScriptId: from.ScriptId, 55 AreaId: from.AreaId, 56 EntityId: common.NewEntityIdFromPtr(from.EntityId), 57 EntityActionName: from.EntityActionName, 58 } 59 return 60 } 61 62 // ToSearchResult ... 63 func (r Action) ToSearchResult(list []*m.Action) *stub.ApiSearchActionResult { 64 65 items := make([]stub.ApiAction, 0, len(list)) 66 67 for _, i := range list { 68 items = append(items, r.ToAction(i)) 69 } 70 71 return &stub.ApiSearchActionResult{ 72 Items: items, 73 } 74 } 75 76 // ToListResult ... 77 func (r Action) ToListResult(list []*m.Action) []stub.ApiAction { 78 79 items := make([]stub.ApiAction, 0, len(list)) 80 81 for _, action := range list { 82 items = append(items, stub.ApiAction{ 83 Id: action.Id, 84 Name: action.Name, 85 Description: action.Description, 86 ScriptId: action.ScriptId, 87 AreaId: action.AreaId, 88 Area: GetStubArea(action.Area), 89 EntityId: action.EntityId.StringPtr(), 90 EntityActionName: action.EntityActionName, 91 CreatedAt: action.CreatedAt, 92 UpdatedAt: action.UpdatedAt, 93 }) 94 } 95 96 return items 97 } 98 99 // ToAction ... 100 func (r Action) ToAction(action *m.Action) (obj stub.ApiAction) { 101 obj = ToAction(action) 102 return 103 } 104 105 // ToAction ... 106 func ToAction(action *m.Action) (obj stub.ApiAction) { 107 if action == nil { 108 return 109 } 110 obj = stub.ApiAction{ 111 Id: action.Id, 112 Name: action.Name, 113 Description: action.Description, 114 ScriptId: action.ScriptId, 115 Script: GetStubScript(action.Script), 116 AreaId: action.AreaId, 117 Area: GetStubArea(action.Area), 118 EntityId: action.EntityId.StringPtr(), 119 Entity: ToEntity(action.Entity), 120 EntityActionName: action.EntityActionName, 121 CreatedAt: action.CreatedAt, 122 UpdatedAt: action.UpdatedAt, 123 } 124 return 125 }