github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/dashboard.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  	m "github.com/e154/smart-home/models"
    24  )
    25  
    26  // Dashboard ...
    27  type Dashboard struct{}
    28  
    29  // NewDashboardDto ...
    30  func NewDashboardDto() Dashboard {
    31  	return Dashboard{}
    32  }
    33  
    34  func (r Dashboard) AddDashboard(obj *stub.ApiNewDashboardRequest) (ver *m.Dashboard) {
    35  	ver = &m.Dashboard{
    36  		Name:        obj.Name,
    37  		Description: obj.Description,
    38  		Enabled:     obj.Enabled,
    39  		AreaId:      obj.AreaId,
    40  	}
    41  	return
    42  }
    43  
    44  func (r Dashboard) UpdateDashboard(obj *stub.DashboardServiceUpdateDashboardJSONBody, id int64) (ver *m.Dashboard) {
    45  	ver = &m.Dashboard{
    46  		Id:          id,
    47  		Name:        obj.Name,
    48  		Description: obj.Description,
    49  		Enabled:     obj.Enabled,
    50  		AreaId:      obj.AreaId,
    51  	}
    52  	return
    53  }
    54  
    55  // ToListResult ...
    56  func (r Dashboard) ToListResult(list []*m.Dashboard) []*stub.ApiDashboardShort {
    57  
    58  	items := make([]*stub.ApiDashboardShort, 0, len(list))
    59  
    60  	for _, i := range list {
    61  		items = append(items, ToDashboardShort(i))
    62  	}
    63  
    64  	return items
    65  }
    66  
    67  // ToDashboard ...
    68  func (r Dashboard) ToDashboard(ver *m.Dashboard) (obj *stub.ApiDashboard) {
    69  	obj = ToDashboard(ver)
    70  	return
    71  }
    72  
    73  // ToSearchResult ...
    74  func (r Dashboard) ToSearchResult(list []*m.Dashboard) *stub.ApiSearchDashboardResult {
    75  
    76  	items := make([]stub.ApiDashboard, 0, len(list))
    77  
    78  	for _, i := range list {
    79  		item := r.ToDashboard(i)
    80  		items = append(items, *item)
    81  	}
    82  
    83  	return &stub.ApiSearchDashboardResult{
    84  		Items: items,
    85  	}
    86  }
    87  
    88  // ToDashboard ...
    89  func ToDashboard(ver *m.Dashboard) (obj *stub.ApiDashboard) {
    90  	if ver == nil {
    91  		return
    92  	}
    93  	obj = &stub.ApiDashboard{
    94  		Id:          ver.Id,
    95  		Name:        ver.Name,
    96  		Description: ver.Description,
    97  		Enabled:     ver.Enabled,
    98  		AreaId:      ver.AreaId,
    99  		Area:        GetStubArea(ver.Area),
   100  		Tabs:        make([]stub.ApiDashboardTab, 0, len(ver.Tabs)),
   101  		Entities:    make(map[string]stub.ApiEntity),
   102  		CreatedAt:   ver.CreatedAt,
   103  		UpdatedAt:   ver.UpdatedAt,
   104  	}
   105  
   106  	// Tabs
   107  	for _, tab := range ver.Tabs {
   108  		obj.Tabs = append(obj.Tabs, *ToDashboardTab(tab))
   109  	}
   110  
   111  	// Entities
   112  	for key, entity := range ver.Entities {
   113  		obj.Entities[key.String()] = *ToEntity(entity)
   114  	}
   115  
   116  	return
   117  }
   118  
   119  // ToDashboardShort ...
   120  func ToDashboardShort(ver *m.Dashboard) (obj *stub.ApiDashboardShort) {
   121  	if ver == nil {
   122  		return
   123  	}
   124  	obj = &stub.ApiDashboardShort{
   125  		Id:          ver.Id,
   126  		Name:        ver.Name,
   127  		Description: ver.Description,
   128  		Enabled:     ver.Enabled,
   129  		AreaId:      ver.AreaId,
   130  		Area:        GetStubArea(ver.Area),
   131  		CreatedAt:   ver.CreatedAt,
   132  		UpdatedAt:   ver.UpdatedAt,
   133  	}
   134  	return
   135  }
   136  
   137  // ImportDashboard ...
   138  func ImportDashboard(obj *stub.ApiDashboard) (ver *m.Dashboard) {
   139  	ver = &m.Dashboard{
   140  		Id:          obj.Id,
   141  		Name:        obj.Name,
   142  		Description: obj.Description,
   143  		Enabled:     obj.Enabled,
   144  		AreaId:      obj.AreaId,
   145  		Tabs:        make([]*m.DashboardTab, 0, len(obj.Tabs)),
   146  	}
   147  
   148  	// tabs
   149  	for _, tabObj := range obj.Tabs {
   150  		ver.Tabs = append(ver.Tabs, ImportDashboardTab(&tabObj))
   151  	}
   152  
   153  	return
   154  }