github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/variable.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  // Variable ...
    27  type Variable struct{}
    28  
    29  // NewVariableDto ...
    30  func NewVariableDto() Variable {
    31  	return Variable{}
    32  }
    33  
    34  // AddVariable ...
    35  func (r Variable) AddVariable(from *stub.ApiNewVariableRequest) (ver m.Variable) {
    36  	ver = m.Variable{
    37  		Name:  from.Name,
    38  		Value: from.Value,
    39  	}
    40  	// tags
    41  	for _, name := range from.Tags {
    42  		ver.Tags = append(ver.Tags, &m.Tag{
    43  			Name: name,
    44  		})
    45  	}
    46  	return
    47  }
    48  
    49  // UpdateVariable ...
    50  func (r Variable) UpdateVariable(obj *stub.VariableServiceUpdateVariableJSONBody, name string) (ver m.Variable) {
    51  	ver = m.Variable{
    52  		Name:  name,
    53  		Value: obj.Value,
    54  	}
    55  	// tags
    56  	for _, name := range obj.Tags {
    57  		ver.Tags = append(ver.Tags, &m.Tag{
    58  			Name: name,
    59  		})
    60  	}
    61  	return
    62  }
    63  
    64  // ToListResult ...
    65  func (r Variable) ToListResult(list []m.Variable) []*stub.ApiVariable {
    66  
    67  	items := make([]*stub.ApiVariable, 0, len(list))
    68  
    69  	for _, i := range list {
    70  		items = append(items, r.ToVariable(i))
    71  	}
    72  
    73  	return items
    74  }
    75  
    76  // ToVariable ...
    77  func (r Variable) ToVariable(ver m.Variable) (obj *stub.ApiVariable) {
    78  	obj = ToVariable(ver)
    79  	return
    80  }
    81  
    82  // ToSearchResult ...
    83  func (r Variable) ToSearchResult(list []m.Variable) *stub.ApiSearchVariableResult {
    84  
    85  	items := make([]stub.ApiVariable, 0, len(list))
    86  
    87  	for _, v := range list {
    88  		items = append(items, stub.ApiVariable{
    89  			Name:      v.Name,
    90  			System:    v.System,
    91  			CreatedAt: v.CreatedAt,
    92  			UpdatedAt: v.UpdatedAt,
    93  		})
    94  	}
    95  
    96  	return &stub.ApiSearchVariableResult{
    97  		Items: items,
    98  	}
    99  }
   100  
   101  // ToVariable ...
   102  func ToVariable(ver m.Variable) (obj *stub.ApiVariable) {
   103  	if ver.Name == "" {
   104  		return
   105  	}
   106  	obj = &stub.ApiVariable{
   107  		Name:      ver.Name,
   108  		Value:     ver.Value,
   109  		System:    ver.System,
   110  		CreatedAt: ver.CreatedAt,
   111  		UpdatedAt: ver.UpdatedAt,
   112  	}
   113  	//tags
   114  	for _, tag := range ver.Tags {
   115  		obj.Tags = append(obj.Tags, tag.Name)
   116  	}
   117  	return
   118  }