github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/entity.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 "github.com/e154/smart-home/common" 27 ) 28 29 // ControllerEntity ... 30 type ControllerEntity struct { 31 *ControllerCommon 32 } 33 34 // NewControllerEntity ... 35 func NewControllerEntity(common *ControllerCommon) *ControllerEntity { 36 return &ControllerEntity{ 37 ControllerCommon: common, 38 } 39 } 40 41 // AddEntity ... 42 func (c ControllerEntity) EntityServiceAddEntity(ctx echo.Context, _ stub.EntityServiceAddEntityParams) error { 43 44 obj := &stub.ApiNewEntityRequest{} 45 if err := c.Body(ctx, obj); err != nil { 46 return c.ERROR(ctx, err) 47 } 48 49 entity, err := c.endpoint.Entity.Add(ctx.Request().Context(), c.dto.Entity.AddEntity(obj)) 50 if err != nil { 51 return c.ERROR(ctx, err) 52 } 53 54 return c.HTTP200(ctx, ResponseWithObj(ctx, dto.ToEntity(entity))) 55 } 56 57 // ImportEntity ... 58 func (c ControllerEntity) EntityServiceImportEntity(ctx echo.Context, _ stub.EntityServiceImportEntityParams) error { 59 60 obj := &stub.ApiEntity{} 61 if err := c.Body(ctx, obj); err != nil { 62 return c.ERROR(ctx, err) 63 } 64 65 err := c.endpoint.Entity.Import(ctx.Request().Context(), c.dto.Entity.ImportEntity(obj)) 66 if err != nil { 67 return c.ERROR(ctx, err) 68 } 69 70 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 71 } 72 73 // UpdateEntity ... 74 func (c ControllerEntity) EntityServiceUpdateEntity(ctx echo.Context, id string, _ stub.EntityServiceUpdateEntityParams) error { 75 76 obj := &stub.EntityServiceUpdateEntityJSONBody{} 77 if err := c.Body(ctx, obj); err != nil { 78 return c.ERROR(ctx, err) 79 } 80 81 entity, err := c.endpoint.Entity.Update(ctx.Request().Context(), c.dto.Entity.UpdateEntity(obj)) 82 if err != nil { 83 return c.ERROR(ctx, err) 84 } 85 86 return c.HTTP200(ctx, ResponseWithObj(ctx, dto.ToEntity(entity))) 87 } 88 89 // GetEntity ... 90 func (c ControllerEntity) EntityServiceGetEntity(ctx echo.Context, id string) error { 91 92 entity, err := c.endpoint.Entity.GetById(ctx.Request().Context(), common.EntityId(id)) 93 if err != nil { 94 return c.ERROR(ctx, err) 95 } 96 97 return c.HTTP200(ctx, ResponseWithObj(ctx, dto.ToEntity(entity))) 98 } 99 100 // GetEntityList ... 101 func (c ControllerEntity) EntityServiceGetEntityList(ctx echo.Context, params stub.EntityServiceGetEntityListParams) error { 102 103 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 104 items, total, err := c.endpoint.Entity.List(ctx.Request().Context(), pagination, params.Query, params.Plugin, params.Area, params.Tags) 105 if err != nil { 106 return c.ERROR(ctx, err) 107 } 108 109 return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Entity.ToListResult(items), total, pagination)) 110 } 111 112 // DeleteEntity ... 113 func (c ControllerEntity) EntityServiceDeleteEntity(ctx echo.Context, id string) error { 114 115 if err := c.endpoint.Entity.Delete(ctx.Request().Context(), common.EntityId(id)); err != nil { 116 return c.ERROR(ctx, err) 117 } 118 119 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 120 } 121 122 // SearchEntity ... 123 func (c ControllerEntity) EntityServiceSearchEntity(ctx echo.Context, params stub.EntityServiceSearchEntityParams) error { 124 125 search := c.Search(params.Query, params.Limit, params.Offset) 126 items, _, err := c.endpoint.Entity.Search(ctx.Request().Context(), search.Query, search.Limit, search.Offset) 127 if err != nil { 128 return c.ERROR(ctx, err) 129 } 130 131 return c.HTTP200(ctx, c.dto.Entity.ToSearchResult(items)) 132 } 133 134 func (c ControllerEntity) EntityServiceEnabledEntity(ctx echo.Context, id string) error { 135 if err := c.endpoint.Entity.Enable(ctx.Request().Context(), common.EntityId(id)); err != nil { 136 return c.ERROR(ctx, err) 137 } 138 139 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 140 } 141 142 func (c ControllerEntity) EntityServiceDisabledEntity(ctx echo.Context, id string) error { 143 if err := c.endpoint.Entity.Disable(ctx.Request().Context(), common.EntityId(id)); err != nil { 144 return c.ERROR(ctx, err) 145 } 146 147 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 148 } 149 150 // GetStatistic ... 151 func (c ControllerEntity) EntityServiceGetStatistic(ctx echo.Context) error { 152 153 statistic, err := c.endpoint.Entity.Statistic(ctx.Request().Context()) 154 if err != nil { 155 return c.ERROR(ctx, err) 156 } 157 158 return c.HTTP200(ctx, ResponseWithObj(ctx, dto.GetStatistic(statistic))) 159 }