github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/role.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/stub" 25 ) 26 27 // ControllerRole ... 28 type ControllerRole struct { 29 *ControllerCommon 30 } 31 32 // NewControllerRole ... 33 func NewControllerRole(common *ControllerCommon) *ControllerRole { 34 return &ControllerRole{ 35 ControllerCommon: common, 36 } 37 } 38 39 // GetRoleAccessList ... 40 func (c ControllerRole) RoleServiceGetRoleAccessList(ctx echo.Context, name string) error { 41 42 accessList, err := c.endpoint.Role.GetAccessList(ctx.Request().Context(), name, c.accessList) 43 if err != nil { 44 return c.ERROR(ctx, err) 45 } 46 47 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Role.ToRoleAccessListResult(accessList))) 48 } 49 50 // UpdateRoleAccessList ... 51 func (c ControllerRole) RoleServiceUpdateRoleAccessList(ctx echo.Context, name string, _ stub.RoleServiceUpdateRoleAccessListParams) error { 52 53 obj := &stub.RoleServiceUpdateRoleAccessListJSONBody{} 54 if err := c.Body(ctx, obj); err != nil { 55 return c.ERROR(ctx, err) 56 } 57 58 if err := c.endpoint.Role.UpdateAccessList(ctx.Request().Context(), name, c.dto.Role.UpdateRoleAccessList(obj, name)); err != nil { 59 return c.ERROR(ctx, err) 60 } 61 62 accessList, err := c.endpoint.Role.GetAccessList(ctx.Request().Context(), name, c.accessList) 63 if err != nil { 64 return c.ERROR(ctx, err) 65 } 66 67 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Role.ToRoleAccessListResult(accessList))) 68 } 69 70 // AddRole ... 71 func (c ControllerRole) RoleServiceAddRole(ctx echo.Context, _ stub.RoleServiceAddRoleParams) error { 72 73 obj := &stub.ApiNewRoleRequest{} 74 if err := c.Body(ctx, obj); err != nil { 75 return c.ERROR(ctx, err) 76 } 77 78 role, err := c.endpoint.Role.Add(ctx.Request().Context(), c.dto.Role.FromNewRoleRequest(obj)) 79 if err != nil { 80 return c.ERROR(ctx, err) 81 } 82 83 return c.HTTP201(ctx, ResponseWithObj(ctx, c.dto.Role.GetStubRole(role))) 84 } 85 86 // GetRoleByName ... 87 func (c ControllerRole) RoleServiceGetRoleByName(ctx echo.Context, name string) error { 88 89 role, err := c.endpoint.Role.GetByName(ctx.Request().Context(), name) 90 if err != nil { 91 return c.ERROR(ctx, err) 92 } 93 94 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Role.GetStubRole(role))) 95 } 96 97 // UpdateRoleByName ... 98 func (c ControllerRole) RoleServiceUpdateRoleByName(ctx echo.Context, name string, _ stub.RoleServiceUpdateRoleByNameParams) error { 99 100 obj := &stub.RoleServiceUpdateRoleByNameJSONBody{} 101 if err := c.Body(ctx, obj); err != nil { 102 return c.ERROR(ctx, err) 103 } 104 105 role, err := c.endpoint.Role.Update(ctx.Request().Context(), c.dto.Role.FromUpdateRoleRequest(obj, name)) 106 if err != nil { 107 return c.ERROR(ctx, err) 108 } 109 110 return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Role.GetStubRole(role))) 111 } 112 113 // GetRoleList ... 114 func (c ControllerRole) RoleServiceGetRoleList(ctx echo.Context, params stub.RoleServiceGetRoleListParams) error { 115 116 pagination := c.Pagination(params.Page, params.Limit, params.Sort) 117 items, total, err := c.endpoint.Role.GetList(ctx.Request().Context(), pagination) 118 if err != nil { 119 return c.ERROR(ctx, err) 120 } 121 122 return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Role.ToListResult(items), total, pagination)) 123 } 124 125 // SearchRoleByName ... 126 func (c ControllerRole) RoleServiceSearchRoleByName(ctx echo.Context, params stub.RoleServiceSearchRoleByNameParams) error { 127 128 search := c.Search(params.Query, params.Limit, params.Offset) 129 items, _, err := c.endpoint.Role.Search(ctx.Request().Context(), search.Query, search.Limit, search.Offset) 130 if err != nil { 131 return c.ERROR(ctx, err) 132 } 133 134 return c.HTTP200(ctx, c.dto.Role.ToSearchResult(items)) 135 } 136 137 // DeleteRoleByName ... 138 func (c ControllerRole) RoleServiceDeleteRoleByName(ctx echo.Context, name string) error { 139 140 if err := c.endpoint.Role.Delete(ctx.Request().Context(), name); err != nil { 141 return c.ERROR(ctx, err) 142 } 143 144 return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{})) 145 }