github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/dashboard_card.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  // DashboardCard ...
    28  type DashboardCard struct{}
    29  
    30  // NewDashboardCardDto ...
    31  func NewDashboardCardDto() DashboardCard {
    32  	return DashboardCard{}
    33  }
    34  
    35  func (r DashboardCard) AddDashboardCard(obj *stub.ApiNewDashboardCardRequest) (ver *m.DashboardCard) {
    36  	ver = &m.DashboardCard{
    37  		Title:          obj.Title,
    38  		Height:         int(obj.Height),
    39  		Width:          int(obj.Width),
    40  		Background:     obj.Background,
    41  		Weight:         int(obj.Weight),
    42  		Enabled:        obj.Enabled,
    43  		DashboardTabId: obj.DashboardTabId,
    44  		Payload:        obj.Payload,
    45  		Hidden:         obj.Hidden,
    46  	}
    47  	if obj.EntityId != nil && *obj.EntityId != "" {
    48  		ver.EntityId = common.NewEntityId(*obj.EntityId)
    49  	}
    50  	return
    51  }
    52  
    53  func (r DashboardCard) UpdateDashboardCard(obj *stub.DashboardCardServiceUpdateDashboardCardJSONBody, id int64) (ver *m.DashboardCard) {
    54  	ver = &m.DashboardCard{
    55  		Id:             id,
    56  		Title:          obj.Title,
    57  		Height:         int(obj.Height),
    58  		Width:          int(obj.Width),
    59  		Background:     obj.Background,
    60  		Weight:         int(obj.Weight),
    61  		Enabled:        obj.Enabled,
    62  		Hidden:         obj.Hidden,
    63  		DashboardTabId: obj.DashboardTabId,
    64  		Payload:        obj.Payload,
    65  		Items:          make([]*m.DashboardCardItem, 0, len(obj.Items)),
    66  	}
    67  	if obj.EntityId != nil && *obj.EntityId != "" {
    68  		ver.EntityId = common.NewEntityId(*obj.EntityId)
    69  	}
    70  	// items
    71  	for _, item := range obj.Items {
    72  		qwe := &m.DashboardCardItem{
    73  			Id:              item.Id,
    74  			Title:           item.Title,
    75  			Type:            item.Type,
    76  			Weight:          int(item.Weight),
    77  			Enabled:         item.Enabled,
    78  			DashboardCardId: id,
    79  			Payload:         item.Payload,
    80  			Hidden:          item.Hidden,
    81  			Frozen:          item.Frozen,
    82  		}
    83  		if item.EntityId != nil {
    84  			qwe.EntityId = common.NewEntityId(*item.EntityId)
    85  		}
    86  		ver.Items = append(ver.Items, qwe)
    87  	}
    88  
    89  	return
    90  }
    91  
    92  // ToListResult ...
    93  func (r DashboardCard) ToListResult(list []*m.DashboardCard) []*stub.ApiDashboardCard {
    94  
    95  	items := make([]*stub.ApiDashboardCard, 0, len(list))
    96  
    97  	for _, i := range list {
    98  		items = append(items, ToDashboardCard(i))
    99  	}
   100  
   101  	return items
   102  }
   103  
   104  // ToDashboardCard ...
   105  func ToDashboardCard(ver *m.DashboardCard) (obj *stub.ApiDashboardCard) {
   106  	if ver == nil {
   107  		return
   108  	}
   109  	obj = &stub.ApiDashboardCard{
   110  		Id:             ver.Id,
   111  		Title:          ver.Title,
   112  		Height:         int32(ver.Height),
   113  		Width:          int32(ver.Width),
   114  		Background:     ver.Background,
   115  		Weight:         int32(ver.Weight),
   116  		Enabled:        ver.Enabled,
   117  		DashboardTabId: ver.DashboardTabId,
   118  		Payload:        ver.Payload,
   119  		Hidden:         ver.Hidden,
   120  		Entities:       make(map[string]stub.ApiEntity),
   121  		CreatedAt:      ver.CreatedAt,
   122  		UpdatedAt:      ver.UpdatedAt,
   123  	}
   124  
   125  	if ver.EntityId != nil && *ver.EntityId != "" {
   126  		obj.EntityId = common.String(string(*ver.EntityId))
   127  	}
   128  
   129  	// Items
   130  	for _, item := range ver.Items {
   131  		cardItem := ToDashboardCardItem(item)
   132  		obj.Items = append(obj.Items, *cardItem)
   133  	}
   134  
   135  	// Entities
   136  	for key, entity := range ver.Entities {
   137  		obj.Entities[key.String()] = *ToEntity(entity)
   138  	}
   139  
   140  	return
   141  }
   142  
   143  func ImportDashboardCard(obj *stub.ApiDashboardCard) (ver *m.DashboardCard) {
   144  	if obj == nil {
   145  		return
   146  	}
   147  	ver = &m.DashboardCard{
   148  		Id:             obj.Id,
   149  		Title:          obj.Title,
   150  		Height:         int(obj.Height),
   151  		Width:          int(obj.Width),
   152  		Background:     obj.Background,
   153  		Weight:         int(obj.Weight),
   154  		Enabled:        obj.Enabled,
   155  		DashboardTabId: obj.DashboardTabId,
   156  		Payload:        obj.Payload,
   157  		Hidden:         obj.Hidden,
   158  		Items:          make([]*m.DashboardCardItem, 0, len(obj.Items)),
   159  	}
   160  
   161  	if obj.EntityId != nil && *obj.EntityId != "" {
   162  		ver.EntityId = common.NewEntityId(*obj.EntityId)
   163  	}
   164  
   165  	// items
   166  	for _, itemObj := range obj.Items {
   167  		ver.Items = append(ver.Items, ImportDashboardCardItem(&itemObj))
   168  	}
   169  
   170  	return
   171  }