github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/dashboard.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/dto" 25 "github.com/e154/smart-home/api/stub" 26 ) 27 28 // ControllerDashboard ... 29 type ControllerDashboard struct { 30 *ControllerCommon 31 } 32 33 // NewControllerDashboard ... 34 func NewControllerDashboard(common *ControllerCommon) *ControllerDashboard { 35 return &ControllerDashboard{ 36 ControllerCommon: common, 37 } 38 } 39 40 // AddDashboard ... 41 func (c ControllerDashboard) DashboardServiceAddDashboard(ctx echo.Context, _ stub.DashboardServiceAddDashboardParams) error { 42 43 obj := &stub.ApiNewDashboardRequest{} 44 if err := c.Body(ctx, obj); err != nil { 45 return c.ERROR(ctx, err) 46 } 47 48 board, err := c.endpoint.Dashboard.Add(ctx.Request().Context(), c.dto.Dashboard.AddDashboard(obj)) 49 if err != nil { 50 return c.ERROR(ctx, err) 51 } 52 53 return c.HTTP201(ctx, ResponseWithObj(ctx, c.dto.Dashboard.ToDashboard(board))) 54 } 55 56 // UpdateDashboard ... 57 func (c ControllerDashboard) DashboardServiceUpdateDashboard(ctx echo.Context, id int64, _ stub.DashboardServiceUpdateDashboardParams) error { 58 59 obj := &stub.DashboardServiceUpdateDashboardJSONBody{} 60 if err := c.Body(ctx, obj); err != nil { 61 return c.ERROR(ctx, err) 62 } 63 64 board, err := c.endpoint.Dashboard.Update(ctx.Request().Context(), c.dto.Dashboard.UpdateDashboard(obj, id)) 65 if err != nil { 66 return c.ERROR(ctx, err) 67 } 68 69 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Dashboard.ToDashboard(board))) 70 } 71 72 // GetDashboardById ... 73 func (c ControllerDashboard) DashboardServiceGetDashboardById(ctx echo.Context, id int64) error { 74 75 board, err := c.endpoint.Dashboard.GetById(ctx.Request().Context(), id) 76 if err != nil { 77 return c.ERROR(ctx, err) 78 } 79 80 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Dashboard.ToDashboard(board))) 81 } 82 83 // GetDashboardList ... 84 func (c ControllerDashboard) DashboardServiceGetDashboardList(ctx echo.Context, params stub.DashboardServiceGetDashboardListParams) error { 85 86 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 87 items, total, err := c.endpoint.Dashboard.GetList(ctx.Request().Context(), pagination) 88 if err != nil { 89 return c.ERROR(ctx, err) 90 } 91 92 return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Dashboard.ToListResult(items), total, pagination)) 93 } 94 95 // DeleteDashboard ... 96 func (c ControllerDashboard) DashboardServiceDeleteDashboard(ctx echo.Context, id int64) error { 97 98 if err := c.endpoint.Dashboard.Delete(ctx.Request().Context(), id); err != nil { 99 return c.ERROR(ctx, err) 100 } 101 102 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 103 } 104 105 // ImportDashboard ... 106 func (c ControllerDashboard) DashboardServiceImportDashboard(ctx echo.Context, _ stub.DashboardServiceImportDashboardParams) error { 107 108 obj := &stub.ApiDashboard{} 109 if err := c.Body(ctx, obj); err != nil { 110 return c.ERROR(ctx, err) 111 } 112 113 board, err := c.endpoint.Dashboard.Import(ctx.Request().Context(), dto.ImportDashboard(obj)) 114 if err != nil { 115 return c.ERROR(ctx, err) 116 } 117 118 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Dashboard.ToDashboard(board))) 119 } 120 121 // SearchDashboard ... 122 func (c ControllerDashboard) DashboardServiceSearchDashboard(ctx echo.Context, params stub.DashboardServiceSearchDashboardParams) error { 123 124 search := c.Search(params.Query, params.Limit, params.Offset) 125 items, _, err := c.endpoint.Dashboard.Search(ctx.Request().Context(), search.Query, search.Limit, search.Offset) 126 if err != nil { 127 return c.ERROR(ctx, err) 128 } 129 130 return c.HTTP200(ctx, c.dto.Dashboard.ToSearchResult(items)) 131 }