github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/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 controllers
    20  
    21  import (
    22  	"github.com/labstack/echo/v4"
    23  
    24  	"github.com/e154/smart-home/api/stub"
    25  )
    26  
    27  // ControllerVariable ...
    28  type ControllerVariable struct {
    29  	*ControllerCommon
    30  }
    31  
    32  // NewControllerVariable ...
    33  func NewControllerVariable(common *ControllerCommon) *ControllerVariable {
    34  	return &ControllerVariable{
    35  		ControllerCommon: common,
    36  	}
    37  }
    38  
    39  // AddVariable ...
    40  func (c ControllerVariable) VariableServiceAddVariable(ctx echo.Context, _ stub.VariableServiceAddVariableParams) error {
    41  
    42  	obj := &stub.ApiNewVariableRequest{}
    43  	if err := c.Body(ctx, obj); err != nil {
    44  		return c.ERROR(ctx, err)
    45  	}
    46  
    47  	variable := c.dto.Variable.AddVariable(obj)
    48  
    49  	if err := c.endpoint.Variable.Add(ctx.Request().Context(), variable); err != nil {
    50  		return c.ERROR(ctx, err)
    51  	}
    52  
    53  	return c.HTTP201(ctx, ResponseWithObj(ctx, c.dto.Variable.ToVariable(variable)))
    54  }
    55  
    56  // UpdateVariable ...
    57  func (c ControllerVariable) VariableServiceUpdateVariable(ctx echo.Context, name string, _ stub.VariableServiceUpdateVariableParams) error {
    58  
    59  	obj := &stub.VariableServiceUpdateVariableJSONBody{}
    60  	if err := c.Body(ctx, obj); err != nil {
    61  		return c.ERROR(ctx, err)
    62  	}
    63  
    64  	variable := c.dto.Variable.UpdateVariable(obj, name)
    65  
    66  	if err := c.endpoint.Variable.Update(ctx.Request().Context(), variable); err != nil {
    67  		return c.ERROR(ctx, err)
    68  	}
    69  
    70  	return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Variable.ToVariable(variable)))
    71  }
    72  
    73  // GetVariableByName ...
    74  func (c ControllerVariable) VariableServiceGetVariableByName(ctx echo.Context, name string) error {
    75  
    76  	variable, err := c.endpoint.Variable.GetById(ctx.Request().Context(), name)
    77  	if err != nil {
    78  		return c.ERROR(ctx, err)
    79  	}
    80  
    81  	return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Variable.ToVariable(variable)))
    82  }
    83  
    84  // GetVariableList ...
    85  func (c ControllerVariable) VariableServiceGetVariableList(ctx echo.Context, params stub.VariableServiceGetVariableListParams) error {
    86  
    87  	pagination := c.Pagination(params.Page, params.Limit, params.Sort)
    88  	items, total, err := c.endpoint.Variable.GetList(ctx.Request().Context(), pagination)
    89  	if err != nil {
    90  		return c.ERROR(ctx, err)
    91  	}
    92  
    93  	return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Variable.ToListResult(items), total, pagination))
    94  }
    95  
    96  // DeleteVariable ...
    97  func (c ControllerVariable) VariableServiceDeleteVariable(ctx echo.Context, name string) error {
    98  
    99  	if err := c.endpoint.Variable.Delete(ctx.Request().Context(), name); err != nil {
   100  		return c.ERROR(ctx, err)
   101  	}
   102  
   103  	return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{}))
   104  }
   105  
   106  // SearchVariable ...
   107  func (c ControllerVariable) VariableServiceSearchVariable(ctx echo.Context, params stub.VariableServiceSearchVariableParams) error {
   108  
   109  	search := c.Search(params.Query, params.Limit, params.Offset)
   110  	items, _, err := c.endpoint.Variable.Search(ctx.Request().Context(), search.Query, search.Limit, search.Offset)
   111  	if err != nil {
   112  		return c.ERROR(ctx, err)
   113  	}
   114  
   115  	return c.HTTP200(ctx, c.dto.Variable.ToSearchResult(items))
   116  }