github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/dashboard_card_item.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  // DashboardCardItem ...
    28  type DashboardCardItem struct{}
    29  
    30  // NewDashboardCardItemDto ...
    31  func NewDashboardCardItemDto() DashboardCardItem {
    32  	return DashboardCardItem{}
    33  }
    34  
    35  func (r DashboardCardItem) AddDashboardCardItem(obj *stub.ApiNewDashboardCardItemRequest) (ver *m.DashboardCardItem) {
    36  	ver = &m.DashboardCardItem{
    37  		Title:           obj.Title,
    38  		Type:            obj.Type,
    39  		Weight:          int(obj.Weight),
    40  		Enabled:         obj.Enabled,
    41  		DashboardCardId: obj.DashboardCardId,
    42  		Payload:         obj.Payload,
    43  		Hidden:          obj.Hidden,
    44  		Frozen:          obj.Frozen,
    45  	}
    46  
    47  	if obj.EntityId != nil && *obj.EntityId != "" {
    48  		ver.EntityId = common.NewEntityId(*obj.EntityId)
    49  	}
    50  	return
    51  }
    52  
    53  func (r DashboardCardItem) UpdateDashboardCardItem(obj *stub.DashboardCardItemServiceUpdateDashboardCardItemJSONBody, id int64) (ver *m.DashboardCardItem) {
    54  	ver = &m.DashboardCardItem{
    55  		Id:              id,
    56  		Title:           obj.Title,
    57  		Type:            obj.Type,
    58  		Weight:          int(obj.Weight),
    59  		Enabled:         obj.Enabled,
    60  		DashboardCardId: obj.DashboardCardId,
    61  		Payload:         obj.Payload,
    62  		Hidden:          obj.Hidden,
    63  		Frozen:          obj.Frozen,
    64  	}
    65  
    66  	if obj.EntityId != nil && *obj.EntityId != "" {
    67  		ver.EntityId = common.NewEntityId(*obj.EntityId)
    68  	}
    69  	return
    70  }
    71  
    72  // ToListResult ...
    73  func (r DashboardCardItem) ToListResult(list []*m.DashboardCardItem) []*stub.ApiDashboardCardItem {
    74  
    75  	items := make([]*stub.ApiDashboardCardItem, 0, len(list))
    76  
    77  	for _, i := range list {
    78  		items = append(items, ToDashboardCardItem(i))
    79  	}
    80  
    81  	return items
    82  }
    83  
    84  // ToDashboardCardItem ...
    85  func (r DashboardCardItem) ToDashboardCardItem(ver *m.DashboardCardItem) (obj *stub.ApiDashboardCardItem) {
    86  	obj = ToDashboardCardItem(ver)
    87  	return
    88  }
    89  
    90  // ToDashboardCardItem ...
    91  func ToDashboardCardItem(ver *m.DashboardCardItem) (obj *stub.ApiDashboardCardItem) {
    92  	if ver == nil {
    93  		return
    94  	}
    95  	obj = &stub.ApiDashboardCardItem{
    96  		Id:              ver.Id,
    97  		Title:           ver.Title,
    98  		Type:            ver.Type,
    99  		Weight:          int32(ver.Weight),
   100  		Enabled:         ver.Enabled,
   101  		DashboardCardId: ver.DashboardCardId,
   102  		Payload:         ver.Payload,
   103  		Hidden:          ver.Hidden,
   104  		Frozen:          ver.Frozen,
   105  		CreatedAt:       ver.CreatedAt,
   106  		UpdatedAt:       ver.UpdatedAt,
   107  	}
   108  
   109  	if ver.EntityId != nil && *ver.EntityId != "" {
   110  		obj.EntityId = common.String(string(*ver.EntityId))
   111  	}
   112  
   113  	return
   114  }
   115  
   116  func ImportDashboardCardItem(obj *stub.ApiDashboardCardItem) (ver *m.DashboardCardItem) {
   117  	ver = &m.DashboardCardItem{
   118  		Id:              obj.Id,
   119  		Title:           obj.Title,
   120  		Type:            obj.Type,
   121  		Weight:          int(obj.Weight),
   122  		Enabled:         obj.Enabled,
   123  		DashboardCardId: obj.DashboardCardId,
   124  		Payload:         obj.Payload,
   125  		Hidden:          obj.Hidden,
   126  		Frozen:          obj.Frozen,
   127  	}
   128  	if obj.EntityId != nil {
   129  		ver.EntityId = common.NewEntityId(*obj.EntityId)
   130  	}
   131  	return
   132  }