github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/dashboard_tab.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 // ControllerDashboardTab ... 28 type ControllerDashboardTab struct { 29 *ControllerCommon 30 } 31 32 // NewControllerDashboardTab ... 33 func NewControllerDashboardTab(common *ControllerCommon) *ControllerDashboardTab { 34 return &ControllerDashboardTab{ 35 ControllerCommon: common, 36 } 37 } 38 39 // AddDashboardTab ... 40 func (c ControllerDashboardTab) DashboardTabServiceAddDashboardTab(ctx echo.Context, _ stub.DashboardTabServiceAddDashboardTabParams) error { 41 42 obj := &stub.ApiNewDashboardTabRequest{} 43 if err := c.Body(ctx, obj); err != nil { 44 return c.ERROR(ctx, err) 45 } 46 47 board, err := c.endpoint.DashboardTab.Add(ctx.Request().Context(), c.dto.DashboardTab.AddDashboardTab(obj)) 48 if err != nil { 49 return c.ERROR(ctx, err) 50 } 51 52 return c.HTTP201(ctx, ResponseWithObj(ctx, c.dto.DashboardTab.ToDashboardTab(board))) 53 } 54 55 // UpdateDashboardTab ... 56 func (c ControllerDashboardTab) DashboardTabServiceUpdateDashboardTab(ctx echo.Context, id int64, _ stub.DashboardTabServiceUpdateDashboardTabParams) error { 57 58 obj := &stub.DashboardTabServiceUpdateDashboardTabJSONBody{} 59 if err := c.Body(ctx, obj); err != nil { 60 return c.ERROR(ctx, err) 61 } 62 63 tab, err := c.endpoint.DashboardTab.Update(ctx.Request().Context(), c.dto.DashboardTab.UpdateDashboardTab(obj, id)) 64 if err != nil { 65 return c.ERROR(ctx, err) 66 } 67 68 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.DashboardTab.ToDashboardTab(tab))) 69 } 70 71 // GetDashboardTabById ... 72 func (c ControllerDashboardTab) DashboardTabServiceGetDashboardTabById(ctx echo.Context, id int64) error { 73 74 tab, err := c.endpoint.DashboardTab.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.DashboardTab.ToDashboardTab(tab))) 80 } 81 82 // GetDashboardTabList ... 83 func (c ControllerDashboardTab) DashboardTabServiceGetDashboardTabList(ctx echo.Context, params stub.DashboardTabServiceGetDashboardTabListParams) error { 84 85 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 86 items, total, err := c.endpoint.DashboardTab.GetList(ctx.Request().Context(), pagination) 87 if err != nil { 88 return c.ERROR(ctx, err) 89 } 90 91 return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.DashboardTab.ToListResult(items), total, pagination)) 92 } 93 94 // DeleteDashboardTab ... 95 func (c ControllerDashboardTab) DashboardTabServiceDeleteDashboardTab(ctx echo.Context, id int64) error { 96 97 if err := c.endpoint.DashboardTab.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 // DashboardTabServiceImportDashboardTab ... 105 func (c ControllerDashboardTab) DashboardTabServiceImportDashboardTab(ctx echo.Context, _ stub.DashboardTabServiceImportDashboardTabParams) error { 106 107 obj := &stub.ApiDashboardTab{} 108 if err := c.Body(ctx, obj); err != nil { 109 return c.ERROR(ctx, err) 110 } 111 112 board, err := c.endpoint.DashboardTab.Import(ctx.Request().Context(), dto.ImportDashboardTab(obj)) 113 if err != nil { 114 return c.ERROR(ctx, err) 115 } 116 117 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.DashboardTab.ToDashboardTab(board))) 118 }