github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/trigger.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 // ControllerTrigger ... 28 type ControllerTrigger struct { 29 *ControllerCommon 30 } 31 32 // NewControllerTrigger ... 33 func NewControllerTrigger(common *ControllerCommon) *ControllerTrigger { 34 return &ControllerTrigger{ 35 ControllerCommon: common, 36 } 37 } 38 39 // AddTrigger ... 40 func (c ControllerTrigger) TriggerServiceAddTrigger(ctx echo.Context, _ stub.TriggerServiceAddTriggerParams) error { 41 42 obj := &stub.ApiNewTriggerRequest{} 43 if err := c.Body(ctx, obj); err != nil { 44 return c.ERROR(ctx, err) 45 } 46 47 trigger, err := c.endpoint.Trigger.Add(ctx.Request().Context(), c.dto.Trigger.AddTrigger(obj)) 48 if err != nil { 49 return c.ERROR(ctx, err) 50 } 51 52 return c.HTTP201(ctx, ResponseWithObj(ctx, c.dto.Trigger.ToTrigger(trigger))) 53 } 54 55 // UpdateTrigger ... 56 func (c ControllerTrigger) TriggerServiceUpdateTrigger(ctx echo.Context, id int64, _ stub.TriggerServiceUpdateTriggerParams) error { 57 58 obj := &stub.TriggerServiceUpdateTriggerJSONBody{} 59 if err := c.Body(ctx, obj); err != nil { 60 return c.ERROR(ctx, err) 61 } 62 63 trigger, err := c.endpoint.Trigger.Update(ctx.Request().Context(), c.dto.Trigger.UpdateTrigger(obj, id)) 64 if err != nil { 65 return c.ERROR(ctx, err) 66 } 67 68 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Trigger.ToTrigger(trigger))) 69 } 70 71 // GetTriggerById ... 72 func (c ControllerTrigger) TriggerServiceGetTriggerById(ctx echo.Context, id int64) error { 73 74 trigger, err := c.endpoint.Trigger.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.Trigger.ToTrigger(trigger))) 80 } 81 82 // GetTriggerList ... 83 func (c ControllerTrigger) TriggerServiceGetTriggerList(ctx echo.Context, params stub.TriggerServiceGetTriggerListParams) error { 84 85 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 86 items, total, err := c.endpoint.Trigger.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.Trigger.ToListResult(items), total, pagination)) 92 } 93 94 // DeleteTrigger ... 95 func (c ControllerTrigger) TriggerServiceDeleteTrigger(ctx echo.Context, id int64) error { 96 97 if err := c.endpoint.Trigger.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 // SearchTrigger ... 105 func (c ControllerTrigger) TriggerServiceSearchTrigger(ctx echo.Context, params stub.TriggerServiceSearchTriggerParams) error { 106 107 search := c.Search(params.Query, params.Limit, params.Offset) 108 items, _, err := c.endpoint.Trigger.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.Trigger.ToSearchResult(items)) 114 } 115 116 // EnableTrigger ... 117 func (c ControllerTrigger) TriggerServiceEnableTrigger(ctx echo.Context, id int64) error { 118 119 if err := c.endpoint.Trigger.Enable(ctx.Request().Context(), id); err != nil { 120 return c.ERROR(ctx, err) 121 } 122 123 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 124 } 125 126 // DisableTrigger ... 127 func (c ControllerTrigger) TriggerServiceDisableTrigger(ctx echo.Context, id int64) error { 128 129 if err := c.endpoint.Trigger.Disable(ctx.Request().Context(), id); err != nil { 130 return c.ERROR(ctx, err) 131 } 132 133 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 134 }