github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/script.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 // Script ... 28 type Script struct{} 29 30 // NewScriptDto ... 31 func NewScriptDto() Script { 32 return Script{} 33 } 34 35 // FromNewScriptRequest ... 36 func (s Script) FromNewScriptRequest(req *stub.ApiNewScriptRequest) (script *m.Script) { 37 script = &m.Script{ 38 Lang: common.ScriptLang(req.Lang), 39 Name: req.Name, 40 Source: req.Source, 41 Description: req.Description, 42 } 43 return 44 } 45 46 // FromUpdateScriptRequest ... 47 func (s Script) FromUpdateScriptRequest(req *stub.ScriptServiceUpdateScriptByIdJSONBody, id int64) (script *m.Script) { 48 script = &m.Script{ 49 Id: id, 50 Lang: common.ScriptLang(req.Lang), 51 Name: req.Name, 52 Source: req.Source, 53 Description: req.Description, 54 } 55 return 56 } 57 58 // FromExecSrcScriptRequest ... 59 func (s Script) FromExecSrcScriptRequest(req *stub.ApiExecSrcScriptRequest) (script *m.Script) { 60 script = &m.Script{ 61 Lang: common.ScriptLang(req.Lang), 62 Name: req.Name, 63 Source: req.Source, 64 Description: req.Description, 65 } 66 return 67 } 68 69 // GetStubScript ... 70 func (s Script) GetStubScript(script *m.Script) (result *stub.ApiScript) { 71 result = GetStubScript(script) 72 return 73 } 74 75 // GetStubScriptShort ... 76 func (s Script) GetStubScriptShort(script *m.Script) (result *stub.ApiScript) { 77 result = GetStubScriptShort(script) 78 return 79 } 80 81 // ToSearchResult ... 82 func (s Script) ToSearchResult(list []*m.Script) *stub.ApiSearchScriptListResult { 83 84 items := make([]stub.ApiScript, 0, len(list)) 85 86 for _, script := range list { 87 items = append(items, stub.ApiScript{ 88 Id: script.Id, 89 Lang: string(script.Lang), 90 Name: script.Name, 91 }) 92 } 93 94 return &stub.ApiSearchScriptListResult{ 95 Items: items, 96 } 97 } 98 99 // ToListResult ... 100 func (s Script) ToListResult(list []*m.Script) []*stub.ApiScript { 101 102 items := make([]*stub.ApiScript, 0, len(list)) 103 104 for _, script := range list { 105 items = append(items, &stub.ApiScript{ 106 Id: script.Id, 107 Lang: string(script.Lang), 108 Name: script.Name, 109 Description: script.Description, 110 CreatedAt: script.CreatedAt, 111 UpdatedAt: script.UpdatedAt, 112 }) 113 } 114 115 return items 116 } 117 118 // GetStubScript ... 119 func GetStubScript(script *m.Script) (result *stub.ApiScript) { 120 if script == nil { 121 return 122 } 123 result = &stub.ApiScript{ 124 Id: script.Id, 125 Lang: string(script.Lang), 126 Name: script.Name, 127 Source: script.Source, 128 Description: script.Description, 129 ScriptInfo: &stub.ApiScriptInfo{ 130 AlexaIntents: int32(script.Info.AlexaIntents), 131 EntityActions: int32(script.Info.EntityActions), 132 EntityScripts: int32(script.Info.EntityScripts), 133 AutomationTriggers: int32(script.Info.AutomationTriggers), 134 AutomationConditions: int32(script.Info.AutomationConditions), 135 AutomationActions: int32(script.Info.AutomationActions), 136 }, 137 Versions: make([]stub.ApiScriptVersion, 0, len(script.Versions)), 138 CreatedAt: script.CreatedAt, 139 UpdatedAt: script.UpdatedAt, 140 } 141 for _, version := range script.Versions { 142 result.Versions = append(result.Versions, stub.ApiScriptVersion{ 143 CreatedAt: version.CreatedAt, 144 Id: version.Id, 145 Lang: string(version.Lang), 146 Source: version.Source, 147 }) 148 } 149 return 150 } 151 152 // GetStubScriptShort ... 153 func GetStubScriptShort(script *m.Script) (result *stub.ApiScript) { 154 if script == nil { 155 return 156 } 157 result = &stub.ApiScript{ 158 Id: script.Id, 159 Name: script.Name, 160 } 161 return 162 } 163 164 func ImportScript(from *stub.ApiScript) (*int64, *m.Script) { 165 if from == nil { 166 return nil, nil 167 } 168 return common.Int64(from.Id), &m.Script{ 169 Id: from.Id, 170 Lang: common.ScriptLang(from.Lang), 171 Name: from.Name, 172 Source: from.Source, 173 Description: from.Description, 174 } 175 }