github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/action.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 // ControllerAction ... 28 type ControllerAction struct { 29 *ControllerCommon 30 } 31 32 // NewControllerAction ... 33 func NewControllerAction(common *ControllerCommon) *ControllerAction { 34 return &ControllerAction{ 35 ControllerCommon: common, 36 } 37 } 38 39 // AddAction ... 40 func (c ControllerAction) ActionServiceAddAction(ctx echo.Context, _ stub.ActionServiceAddActionParams) error { 41 42 obj := &stub.ApiNewActionRequest{} 43 if err := c.Body(ctx, obj); err != nil { 44 return c.ERROR(ctx, err) 45 } 46 47 action, err := c.endpoint.Action.Add(ctx.Request().Context(), c.dto.Action.Add(obj)) 48 if err != nil { 49 return c.ERROR(ctx, err) 50 } 51 52 return c.HTTP201(ctx, ResponseWithObj(ctx, c.dto.Action.ToAction(action))) 53 } 54 55 // UpdateAction ... 56 func (c ControllerAction) ActionServiceUpdateAction(ctx echo.Context, id int64, _ stub.ActionServiceUpdateActionParams) error { 57 58 obj := &stub.ActionServiceUpdateActionJSONBody{} 59 if err := c.Body(ctx, obj); err != nil { 60 return c.ERROR(ctx, err) 61 } 62 63 action, err := c.endpoint.Action.Update(ctx.Request().Context(), c.dto.Action.Update(obj, id)) 64 if err != nil { 65 return c.ERROR(ctx, err) 66 } 67 68 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Action.ToAction(action))) 69 } 70 71 // GetActionById ... 72 func (c ControllerAction) ActionServiceGetActionById(ctx echo.Context, id int64) error { 73 74 action, err := c.endpoint.Action.GetById(ctx.Request().Context(), id) 75 if err != nil { 76 return c.ERROR(ctx, err) 77 } 78 79 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Action.ToAction(action))) 80 } 81 82 // GetActionList ... 83 func (c ControllerAction) ActionServiceGetActionList(ctx echo.Context, params stub.ActionServiceGetActionListParams) error { 84 85 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 86 items, total, err := c.endpoint.Action.GetList(ctx.Request().Context(), pagination, params.Ids) 87 if err != nil { 88 return c.ERROR(ctx, err) 89 } 90 91 return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Action.ToListResult(items), total, pagination)) 92 } 93 94 // DeleteAction ... 95 func (c ControllerAction) ActionServiceDeleteAction(ctx echo.Context, id int64) error { 96 97 if err := c.endpoint.Action.Delete(ctx.Request().Context(), id); err != nil { 98 return c.ERROR(ctx, err) 99 } 100 101 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 102 } 103 104 // SearchAction ... 105 func (c ControllerAction) ActionServiceSearchAction(ctx echo.Context, params stub.ActionServiceSearchActionParams) error { 106 107 search := c.Search(params.Query, params.Limit, params.Offset) 108 items, _, err := c.endpoint.Action.Search(ctx.Request().Context(), search.Query, search.Limit, search.Offset) 109 if err != nil { 110 return c.ERROR(ctx, err) 111 } 112 113 return c.HTTP200(ctx, c.dto.Action.ToSearchResult(items)) 114 }