github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/condition.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  // Condition ...
    27  type Condition struct{}
    28  
    29  // NewConditionDto ...
    30  func NewConditionDto() Condition {
    31  	return Condition{}
    32  }
    33  
    34  // AddCondition ...
    35  func (r Condition) AddCondition(from *stub.ApiNewConditionRequest) (action *m.Condition) {
    36  	action = &m.Condition{
    37  		Name:        from.Name,
    38  		Description: from.Description,
    39  		ScriptId:    from.ScriptId,
    40  		AreaId:      from.AreaId,
    41  	}
    42  	return
    43  }
    44  
    45  // UpdateCondition ...
    46  func (r Condition) UpdateCondition(from *stub.ConditionServiceUpdateConditionJSONBody, id int64) (action *m.Condition) {
    47  	action = &m.Condition{
    48  		Id:          id,
    49  		Name:        from.Name,
    50  		Description: from.Description,
    51  		ScriptId:    from.ScriptId,
    52  		AreaId:      from.AreaId,
    53  	}
    54  	return
    55  }
    56  
    57  // ToSearchResult ...
    58  func (r Condition) ToSearchResult(list []*m.Condition) *stub.ApiSearchConditionResult {
    59  
    60  	items := make([]stub.ApiCondition, 0, len(list))
    61  
    62  	for _, i := range list {
    63  		condition := ToCondition(i)
    64  		items = append(items, *condition)
    65  	}
    66  
    67  	return &stub.ApiSearchConditionResult{
    68  		Items: items,
    69  	}
    70  }
    71  
    72  // ToListResult ...
    73  func (r Condition) ToListResult(list []*m.Condition) []*stub.ApiCondition {
    74  
    75  	items := make([]*stub.ApiCondition, 0, len(list))
    76  
    77  	for _, cond := range list {
    78  		items = append(items, &stub.ApiCondition{
    79  			Id:          cond.Id,
    80  			Name:        cond.Name,
    81  			Description: cond.Description,
    82  			ScriptId:    cond.ScriptId,
    83  			AreaId:      cond.AreaId,
    84  			Area:        GetStubArea(cond.Area),
    85  			CreatedAt:   cond.CreatedAt,
    86  			UpdatedAt:   cond.UpdatedAt,
    87  		})
    88  	}
    89  
    90  	return items
    91  }
    92  
    93  // ToCondition ...
    94  func ToCondition(cond *m.Condition) (obj *stub.ApiCondition) {
    95  	if cond == nil {
    96  		return
    97  	}
    98  	obj = &stub.ApiCondition{
    99  		Id:          cond.Id,
   100  		Name:        cond.Name,
   101  		Description: cond.Description,
   102  		ScriptId:    cond.ScriptId,
   103  		AreaId:      cond.AreaId,
   104  		Script:      GetStubScript(cond.Script),
   105  		Area:        GetStubArea(cond.Area),
   106  		CreatedAt:   cond.CreatedAt,
   107  		UpdatedAt:   cond.UpdatedAt,
   108  	}
   109  
   110  	return
   111  }