github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/tag.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 m "github.com/e154/smart-home/models" 24 "github.com/labstack/echo/v4" 25 ) 26 27 // ControllerTag ... 28 type ControllerTag struct { 29 *ControllerCommon 30 } 31 32 // NewControllerTag ... 33 func NewControllerTag(common *ControllerCommon) *ControllerTag { 34 return &ControllerTag{ 35 ControllerCommon: common, 36 } 37 } 38 39 // TagServiceGetTagById ... 40 func (c ControllerTag) TagServiceGetTagById(ctx echo.Context, id int64) error { 41 42 tag, err := c.endpoint.Tag.GetById(ctx.Request().Context(), id) 43 if err != nil { 44 return c.ERROR(ctx, err) 45 } 46 47 return c.HTTP200(ctx, ResponseWithObj(ctx, tag)) 48 } 49 50 // TagServiceUpdateTagById ... 51 func (c ControllerTag) TagServiceUpdateTagById(ctx echo.Context, id int64, _ stub.TagServiceUpdateTagByIdParams) error { 52 53 obj := &stub.TagServiceUpdateTagByIdJSONBody{} 54 if err := c.Body(ctx, obj); err != nil { 55 return c.ERROR(ctx, err) 56 } 57 58 tag, err := c.endpoint.Tag.Update(ctx.Request().Context(), &m.Tag{Id: id, Name: obj.Name}) 59 if err != nil { 60 return c.ERROR(ctx, err) 61 } 62 63 return c.HTTP200(ctx, ResponseWithObj(ctx, tag)) 64 } 65 66 // TagServiceDeleteTagById ... 67 func (c ControllerTag) TagServiceDeleteTagById(ctx echo.Context, id int64) error { 68 69 if err := c.endpoint.Tag.DeleteTagById(ctx.Request().Context(), id); err != nil { 70 return c.ERROR(ctx, err) 71 } 72 73 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 74 } 75 76 // TagServiceGetTagList ... 77 func (c ControllerTag) TagServiceGetTagList(ctx echo.Context, params stub.TagServiceGetTagListParams) error { 78 79 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 80 items, total, err := c.endpoint.Tag.GetList(ctx.Request().Context(), pagination, params.Query, params.Tags) 81 if err != nil { 82 return c.ERROR(ctx, err) 83 } 84 85 return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Tag.ToListResult(items), total, pagination)) 86 } 87 88 // TagServiceSearchTag ... 89 func (c ControllerTag) TagServiceSearchTag(ctx echo.Context, params stub.TagServiceSearchTagParams) error { 90 91 search := c.Search(params.Query, params.Limit, params.Offset) 92 items, _, err := c.endpoint.Tag.Search(ctx.Request().Context(), search.Query, search.Limit, search.Offset) 93 if err != nil { 94 return c.ERROR(ctx, err) 95 } 96 97 return c.HTTP200(ctx, c.dto.Tag.ToSearchResult(items)) 98 }