github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/condition.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/dto" 23 "github.com/e154/smart-home/api/stub" 24 "github.com/labstack/echo/v4" 25 ) 26 27 // ControllerCondition ... 28 type ControllerCondition struct { 29 *ControllerCommon 30 } 31 32 // NewControllerCondition ... 33 func NewControllerCondition(common *ControllerCommon) *ControllerCondition { 34 return &ControllerCondition{ 35 ControllerCommon: common, 36 } 37 } 38 39 // AddCondition ... 40 func (c ControllerCondition) ConditionServiceAddCondition(ctx echo.Context, _ stub.ConditionServiceAddConditionParams) error { 41 42 obj := &stub.ApiNewConditionRequest{} 43 if err := c.Body(ctx, obj); err != nil { 44 return c.ERROR(ctx, err) 45 } 46 47 condition, err := c.endpoint.Condition.Add(ctx.Request().Context(), c.dto.Condition.AddCondition(obj)) 48 if err != nil { 49 return c.ERROR(ctx, err) 50 } 51 52 return c.HTTP201(ctx, ResponseWithObj(ctx, dto.ToCondition(condition))) 53 } 54 55 // UpdateCondition ... 56 func (c ControllerCondition) ConditionServiceUpdateCondition(ctx echo.Context, id int64, _ stub.ConditionServiceUpdateConditionParams) error { 57 58 obj := &stub.ConditionServiceUpdateConditionJSONBody{} 59 if err := c.Body(ctx, obj); err != nil { 60 return c.ERROR(ctx, err) 61 } 62 63 condition, err := c.endpoint.Condition.Update(ctx.Request().Context(), c.dto.Condition.UpdateCondition(obj, id)) 64 if err != nil { 65 return c.ERROR(ctx, err) 66 } 67 68 return c.HTTP200(ctx, ResponseWithObj(ctx, dto.ToCondition(condition))) 69 } 70 71 // GetConditionById ... 72 func (c ControllerCondition) ConditionServiceGetConditionById(ctx echo.Context, id int64, _ stub.ConditionServiceGetConditionByIdParams) error { 73 74 condition, err := c.endpoint.Condition.GetById(ctx.Request().Context(), id) 75 if err != nil { 76 return c.ERROR(ctx, err) 77 } 78 79 return c.HTTP200(ctx, ResponseWithObj(ctx, dto.ToCondition(condition))) 80 } 81 82 // GetConditionList ... 83 func (c ControllerCondition) ConditionServiceGetConditionList(ctx echo.Context, params stub.ConditionServiceGetConditionListParams) error { 84 85 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 86 items, total, err := c.endpoint.Condition.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.Condition.ToListResult(items), total, pagination)) 92 } 93 94 // DeleteCondition ... 95 func (c ControllerCondition) ConditionServiceDeleteCondition(ctx echo.Context, id int64) error { 96 97 if err := c.endpoint.Condition.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 // SearchCondition ... 105 func (c ControllerCondition) ConditionServiceSearchCondition(ctx echo.Context, params stub.ConditionServiceSearchConditionParams) error { 106 107 search := c.Search(params.Query, params.Limit, params.Offset) 108 items, _, err := c.endpoint.Condition.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.Condition.ToSearchResult(items)) 114 }