github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/script.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 // ControllerScript ... 29 type ControllerScript struct { 30 *ControllerCommon 31 } 32 33 // NewControllerScript ... 34 func NewControllerScript(common *ControllerCommon) *ControllerScript { 35 return &ControllerScript{ 36 ControllerCommon: common, 37 } 38 } 39 40 // AddScript ... 41 func (c ControllerScript) ScriptServiceAddScript(ctx echo.Context, _ stub.ScriptServiceAddScriptParams) error { 42 43 obj := &stub.ApiNewScriptRequest{} 44 if err := c.Body(ctx, obj); err != nil { 45 return c.ERROR(ctx, err) 46 } 47 48 script, err := c.endpoint.Script.Add(ctx.Request().Context(), c.dto.Script.FromNewScriptRequest(obj)) 49 if err != nil { 50 return c.ERROR(ctx, err) 51 } 52 53 return c.HTTP201(ctx, ResponseWithObj(ctx, c.dto.Script.GetStubScript(script))) 54 } 55 56 // GetScriptById ... 57 func (c ControllerScript) ScriptServiceGetScriptById(ctx echo.Context, id int64) error { 58 59 script, err := c.endpoint.Script.GetById(ctx.Request().Context(), id) 60 if err != nil { 61 return c.ERROR(ctx, err) 62 } 63 64 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Script.GetStubScript(script))) 65 } 66 67 // ScriptServiceGetCompiledScriptById ... 68 func (c ControllerScript) ScriptServiceGetCompiledScriptById(ctx echo.Context, id int64) error { 69 70 script, err := c.endpoint.Script.GetById(ctx.Request().Context(), id) 71 if err != nil { 72 return c.ERROR(ctx, err) 73 } 74 75 return ctx.Blob(200, echo.MIMEApplicationJavaScript, []byte(script.Compiled)) 76 } 77 78 // UpdateScriptById ... 79 func (c ControllerScript) ScriptServiceUpdateScriptById(ctx echo.Context, id int64, _ stub.ScriptServiceUpdateScriptByIdParams) error { 80 81 obj := &stub.ScriptServiceUpdateScriptByIdJSONBody{} 82 if err := c.Body(ctx, obj); err != nil { 83 return c.ERROR(ctx, err) 84 } 85 86 script, err := c.endpoint.Script.Update(ctx.Request().Context(), c.dto.Script.FromUpdateScriptRequest(obj, id)) 87 if err != nil { 88 return c.ERROR(ctx, err) 89 } 90 91 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Script.GetStubScript(script))) 92 } 93 94 // GetScriptList ... 95 func (c ControllerScript) ScriptServiceGetScriptList(ctx echo.Context, params stub.ScriptServiceGetScriptListParams) error { 96 97 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 98 items, total, err := c.endpoint.Script.GetList(ctx.Request().Context(), pagination, params.Query, params.Ids) 99 if err != nil { 100 return c.ERROR(ctx, err) 101 } 102 103 return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Script.ToListResult(items), total, pagination)) 104 } 105 106 // SearchScript ... 107 func (c ControllerScript) ScriptServiceSearchScript(ctx echo.Context, params stub.ScriptServiceSearchScriptParams) error { 108 109 search := c.Search(params.Query, params.Limit, params.Offset) 110 items, _, err := c.endpoint.Script.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.Script.ToSearchResult(items)) 116 } 117 118 // DeleteScriptById ... 119 func (c ControllerScript) ScriptServiceDeleteScriptById(ctx echo.Context, id int64) error { 120 121 if err := c.endpoint.Script.DeleteScriptById(ctx.Request().Context(), id); err != nil { 122 return c.ERROR(ctx, err) 123 } 124 125 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 126 } 127 128 // ExecScriptById ... 129 func (c ControllerScript) ScriptServiceExecScriptById(ctx echo.Context, id int64) error { 130 131 result, err := c.endpoint.Script.Execute(ctx.Request().Context(), id) 132 if err != nil { 133 return c.ERROR(ctx, err) 134 } 135 136 return c.HTTP200(ctx, ResponseWithObj(ctx, &stub.ApiExecScriptResult{Result: result})) 137 } 138 139 // ExecSrcScriptById ... 140 func (c ControllerScript) ScriptServiceExecSrcScriptById(ctx echo.Context, _ stub.ScriptServiceExecSrcScriptByIdParams) error { 141 142 obj := &stub.ApiExecSrcScriptRequest{} 143 if err := c.Body(ctx, obj); err != nil { 144 return c.ERROR(ctx, err) 145 } 146 147 result, err := c.endpoint.Script.ExecuteSource(ctx.Request().Context(), c.dto.Script.FromExecSrcScriptRequest(obj)) 148 if err != nil { 149 return c.ERROR(ctx, err) 150 } 151 152 return c.HTTP200(ctx, ResponseWithObj(ctx, &stub.ApiExecScriptResult{Result: result})) 153 } 154 155 // CopyScriptById ... 156 func (c ControllerScript) ScriptServiceCopyScriptById(ctx echo.Context, id int64) error { 157 158 script, err := c.endpoint.Script.Copy(ctx.Request().Context(), id) 159 if err != nil { 160 return c.ERROR(ctx, err) 161 } 162 163 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Script.GetStubScript(script))) 164 } 165 166 // GetStatistic ... 167 func (c ControllerScript) ScriptServiceGetStatistic(ctx echo.Context) error { 168 169 statistic, err := c.endpoint.Script.Statistic(ctx.Request().Context()) 170 if err != nil { 171 return c.ERROR(ctx, err) 172 } 173 174 return c.HTTP200(ctx, ResponseWithObj(ctx, dto.GetStatistic(statistic))) 175 }