github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/plugin.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/e154/smart-home/api/stub"
    23  	"github.com/labstack/echo/v4"
    24  
    25  	"github.com/e154/smart-home/api/dto"
    26  )
    27  
    28  // ControllerPlugin ...
    29  type ControllerPlugin struct {
    30  	*ControllerCommon
    31  }
    32  
    33  // NewControllerPlugin ...
    34  func NewControllerPlugin(common *ControllerCommon) *ControllerPlugin {
    35  	return &ControllerPlugin{
    36  		ControllerCommon: common,
    37  	}
    38  }
    39  
    40  // GetPluginList ...
    41  func (c ControllerPlugin) PluginServiceGetPluginList(ctx echo.Context, params stub.PluginServiceGetPluginListParams) error {
    42  
    43  	pagination := c.Pagination(params.Page, params.Limit, params.Sort)
    44  	items, total, err := c.endpoint.Plugin.GetList(ctx.Request().Context(), pagination)
    45  	if err != nil {
    46  		return c.ERROR(ctx, err)
    47  	}
    48  
    49  	return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Plugin.ToPluginListResult(items), total, pagination))
    50  }
    51  
    52  // EnablePlugin ...
    53  func (c ControllerPlugin) PluginServiceEnablePlugin(ctx echo.Context, name string) error {
    54  
    55  	if err := c.endpoint.Plugin.Enable(ctx.Request().Context(), name); err != nil {
    56  		return c.ERROR(ctx, err)
    57  	}
    58  
    59  	return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{}))
    60  }
    61  
    62  // DisablePlugin ...
    63  func (c ControllerPlugin) PluginServiceDisablePlugin(ctx echo.Context, name string) error {
    64  
    65  	if err := c.endpoint.Plugin.Disable(ctx.Request().Context(), name); err != nil {
    66  		return c.ERROR(ctx, err)
    67  	}
    68  
    69  	return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{}))
    70  }
    71  
    72  // SearchPlugin ...
    73  func (c ControllerPlugin) PluginServiceSearchPlugin(ctx echo.Context, params stub.PluginServiceSearchPluginParams) error {
    74  
    75  	search := c.Search(params.Query, params.Limit, params.Offset)
    76  	items, _, err := c.endpoint.Plugin.Search(ctx.Request().Context(), search.Query, search.Limit, search.Offset)
    77  	if err != nil {
    78  		return c.ERROR(ctx, err)
    79  	}
    80  
    81  	return c.HTTP200(ctx, c.dto.Plugin.ToSearchResult(items))
    82  }
    83  
    84  // GetPlugin ...
    85  func (c ControllerPlugin) PluginServiceGetPlugin(ctx echo.Context, name string) error {
    86  
    87  	plugin, err := c.endpoint.Plugin.GetByName(ctx.Request().Context(), name)
    88  	if err != nil {
    89  		return c.ERROR(ctx, err)
    90  	}
    91  
    92  	options, err := c.endpoint.Plugin.GetOptions(ctx.Request().Context(), name)
    93  	if err != nil {
    94  		return c.ERROR(ctx, err)
    95  	}
    96  
    97  	return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Plugin.ToGetPlugin(plugin, options)))
    98  }
    99  
   100  // UpdatePluginSettings ...
   101  func (c ControllerPlugin) PluginServiceUpdatePluginSettings(ctx echo.Context, name string, _ stub.PluginServiceUpdatePluginSettingsParams) error {
   102  
   103  	obj := &stub.PluginServiceUpdatePluginSettingsJSONBody{}
   104  	if err := c.Body(ctx, obj); err != nil {
   105  		return c.ERROR(ctx, err)
   106  	}
   107  
   108  	if err := c.endpoint.Plugin.UpdateSettings(ctx.Request().Context(), name, dto.AttributeFromApi(obj.Settings)); err != nil {
   109  		return c.ERROR(ctx, err)
   110  	}
   111  
   112  	return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{}))
   113  }
   114  
   115  // PluginServiceGetPluginReadme ...
   116  func (c ControllerPlugin) PluginServiceGetPluginReadme(ctx echo.Context, name string, params stub.PluginServiceGetPluginReadmeParams) error {
   117  
   118  	html, err := c.endpoint.Plugin.Readme(ctx.Request().Context(), name, params.Lang)
   119  	if err != nil {
   120  		return c.ERROR(ctx, err)
   121  	}
   122  
   123  	return ctx.HTMLBlob(200, html)
   124  }