github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/dashboard_tab.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 "github.com/e154/smart-home/api/stub" 23 m "github.com/e154/smart-home/models" 24 ) 25 26 // DashboardTab ... 27 type DashboardTab struct{} 28 29 // NewDashboardTabDto ... 30 func NewDashboardTabDto() DashboardTab { 31 return DashboardTab{} 32 } 33 34 func (r DashboardTab) AddDashboardTab(obj *stub.ApiNewDashboardTabRequest) (ver *m.DashboardTab) { 35 ver = &m.DashboardTab{ 36 Name: obj.Name, 37 ColumnWidth: int(obj.ColumnWidth), 38 Gap: obj.Gap, 39 Background: obj.Background, 40 Icon: obj.Icon, 41 Enabled: obj.Enabled, 42 Weight: int(obj.Weight), 43 DashboardId: obj.DashboardId, 44 Payload: obj.Payload, 45 } 46 return 47 } 48 49 func (r DashboardTab) UpdateDashboardTab(obj *stub.DashboardTabServiceUpdateDashboardTabJSONBody, id int64) (ver *m.DashboardTab) { 50 ver = &m.DashboardTab{ 51 Id: id, 52 Name: obj.Name, 53 Icon: obj.Icon, 54 ColumnWidth: int(obj.ColumnWidth), 55 Gap: obj.Gap, 56 Background: obj.Background, 57 Enabled: obj.Enabled, 58 Weight: int(obj.Weight), 59 DashboardId: obj.DashboardId, 60 Payload: obj.Payload, 61 } 62 return 63 } 64 65 // ToListResult ... 66 func (r DashboardTab) ToListResult(list []*m.DashboardTab) []*stub.ApiDashboardTab { 67 68 items := make([]*stub.ApiDashboardTab, 0, len(list)) 69 70 for _, i := range list { 71 items = append(items, ToDashboardTabShort(i)) 72 } 73 74 return items 75 } 76 77 // ToDashboardTab ... 78 func (r DashboardTab) ToDashboardTab(ver *m.DashboardTab) (obj *stub.ApiDashboardTab) { 79 obj = ToDashboardTab(ver) 80 return 81 } 82 83 // ToDashboardTab ... 84 func ToDashboardTab(ver *m.DashboardTab) (obj *stub.ApiDashboardTab) { 85 if ver == nil { 86 return 87 } 88 obj = &stub.ApiDashboardTab{ 89 Id: ver.Id, 90 Name: ver.Name, 91 Icon: ver.Icon, 92 ColumnWidth: int32(ver.ColumnWidth), 93 Gap: ver.Gap, 94 Background: ver.Background, 95 Enabled: ver.Enabled, 96 Weight: int32(ver.Weight), 97 DashboardId: ver.DashboardId, 98 Payload: ver.Payload, 99 Cards: make([]stub.ApiDashboardCard, 0, len(ver.Cards)), 100 Entities: make(map[string]stub.ApiEntity), 101 CreatedAt: ver.CreatedAt, 102 UpdatedAt: ver.UpdatedAt, 103 } 104 105 // Cards 106 for _, card := range ver.Cards { 107 obj.Cards = append(obj.Cards, *ToDashboardCard(card)) 108 } 109 110 // Entities 111 for key, entity := range ver.Entities { 112 obj.Entities[key.String()] = *ToEntity(entity) 113 } 114 115 return 116 } 117 118 // ToDashboardTabShort ... 119 func ToDashboardTabShort(ver *m.DashboardTab) (obj *stub.ApiDashboardTab) { 120 if ver == nil { 121 return 122 } 123 obj = &stub.ApiDashboardTab{ 124 Id: ver.Id, 125 Name: ver.Name, 126 Icon: ver.Icon, 127 ColumnWidth: int32(ver.ColumnWidth), 128 Gap: ver.Gap, 129 Background: ver.Background, 130 Enabled: ver.Enabled, 131 Weight: int32(ver.Weight), 132 DashboardId: ver.DashboardId, 133 Payload: ver.Payload, 134 CreatedAt: ver.CreatedAt, 135 UpdatedAt: ver.UpdatedAt, 136 } 137 return 138 } 139 140 func ImportDashboardTab(obj *stub.ApiDashboardTab) (ver *m.DashboardTab) { 141 ver = &m.DashboardTab{ 142 Id: obj.Id, 143 Name: obj.Name, 144 ColumnWidth: int(obj.ColumnWidth), 145 Gap: obj.Gap, 146 Background: obj.Background, 147 Icon: obj.Icon, 148 Enabled: obj.Enabled, 149 Weight: int(obj.Weight), 150 DashboardId: obj.DashboardId, 151 Payload: obj.Payload, 152 Cards: make([]*m.DashboardCard, 0, len(obj.Cards)), 153 } 154 155 // cards 156 for _, cardObj := range obj.Cards { 157 ver.Cards = append(ver.Cards, ImportDashboardCard(&cardObj)) 158 } 159 160 return 161 }