From 76bb28ceec67617baf187cfd3174e4ce112362c6 Mon Sep 17 00:00:00 2001 From: All-The-Best-for Date: Sat, 19 Oct 2024 23:44:27 +0800 Subject: [PATCH] [feat] HertzBeat Official Template Marketplace [#1792] --- .../apiTest/category.http | 37 +- .../hertzbeat-template-hub/apiTest/share.http | 21 +- .../hertzbeat-template-hub/apiTest/star.http | 43 ++- .../apiTest/template.http | 41 ++- .../hertzbeat-template-hub/apiTest/user.http | 47 ++- .../apiTest/version.http | 29 +- .../hertzbeat-template-hub/pom.xml | 18 + .../HertzbeatTemplateHubApplication.java | 2 + .../templatehub/config/FileStorageConfig.java | 4 - .../constants/CommonConstants.java | 5 + .../controller/AccountController.java | 78 ++++ .../controller/CategoryController.java | 15 +- .../controller/ResourceController.java | 92 +++++ .../controller/RoleController.java | 137 +++++++ .../controller/ShareController.java | 41 +-- .../controller/StarController.java | 64 ++-- .../controller/TemplateController.java | 336 ++++++++++++------ .../templatehub/controller/TokenStorage.java | 61 ++++ .../controller/UserController.java | 89 ++++- .../controller/VersionController.java | 86 ++--- .../templatehub/exception/CommonError.java | 17 + .../exception/GlobalExceptionHandler.java | 8 +- .../HertzbeatTemplateHubException.java | 17 + .../exception/RestErrorResponse.java | 17 + .../templatehub/model/DO/AuthResourceDO.java | 54 +++ .../templatehub/model/DO/AuthRoleDO.java | 47 +++ .../model/DO/AuthRoleResourceBindDO.java | 38 ++ .../templatehub/model/DO/AuthUserDO.java | 73 ++++ .../model/DO/AuthUserRoleBindDO.java | 38 ++ .../templatehub/model/DO/CategoryDO.java | 57 +++ .../templatehub/model/DO/StarDO.java | 53 +++ .../hertzbeat/templatehub/model/DO/TagDO.java | 56 +++ .../templatehub/model/DO/TemplateDO.java | 84 +++++ .../templatehub/model/DO/TemplateTagDO.java | 50 +++ .../templatehub/model/DO/VersionDO.java | 70 ++++ .../templatehub/model/VO/TemplateVO.java | 46 +++ .../model/dao/AuthResourceDao.java | 64 ++++ .../templatehub/model/dao/AuthRoleDao.java | 24 ++ .../model/dao/AuthRoleResourceBindDao.java | 36 ++ .../templatehub/model/dao/AuthUserDao.java | 33 ++ .../model/dao/AuthUserRoleBindDao.java | 34 ++ .../templatehub/model/dao/CategoryDao.java | 11 +- .../templatehub/model/dao/StarDao.java | 18 +- .../templatehub/model/dao/TemplateDao.java | 69 ++-- .../templatehub/model/dao/VersionDao.java | 26 +- .../templatehub/model/dto/LoginDto.java | 64 ++++ .../templatehub/model/dto/Message.java | 4 +- .../model/dto/RefreshTokenResponse.java | 40 +++ .../templatehub/model/dto/TemplateDto.java | 5 +- .../templatehub/model/dto/TokenDto.java | 41 +++ .../templatehub/service/AccountService.java | 98 +++++ .../templatehub/service/CategoryService.java | 6 +- .../service/FileStorageService.java | 6 +- .../templatehub/service/ResourceService.java | 69 ++++ .../templatehub/service/RoleService.java | 90 +++++ .../templatehub/service/StarService.java | 16 +- .../templatehub/service/TemplateService.java | 105 +++--- .../templatehub/service/VersionService.java | 22 +- .../service/impl/AccountServiceImpl.java | 217 +++++++++++ .../service/impl/CategoryServiceImpl.java | 28 +- .../service/impl/DataConflictException.java | 14 + .../service/impl/ResourceServiceImpl.java | 93 +++++ .../service/impl/RoleServiceImpl.java | 123 +++++++ .../service/impl/StarServiceImpl.java | 46 +-- .../service/impl/TemplateServiceImpl.java | 291 ++++++++------- .../service/impl/VersionServiceImpl.java | 118 +++--- .../sureness/SurenessFilterExample.java | 120 +++++++ .../config/SurenessConfiguration.java | 122 +++++++ .../processor/CustomTokenProcessor.java | 105 ++++++ .../RefreshExpiredTokenException.java | 14 + .../provider/DatabaseAccountProvider.java | 25 ++ .../provider/DatabasePathTreeProvider.java | 38 ++ .../subject/CustomPasswdSubjectCreator.java | 54 +++ .../sureness/subject/CustomTokenSubject.java | 189 ++++++++++ .../subject/CustomTokenSubjectCreator.java | 48 +++ .../hertzbeat/templatehub/util/JsonUtil.java | 122 +++++++ .../templatehub/util/ResponseUtil.java | 79 ++++ .../src/main/resources/application.yml | 54 +++ .../src/main/resources/sureness.yml | 66 ++++ ...ertzbeatTemplateDOHubApplicationTests.java | 30 ++ .../controller/CategoryDOControllerTest.java | 160 +++++++++ .../controller/ShareControllerTest.java | 49 +-- .../controller/StarDOControllerTest.java | 164 +++++++++ .../controller/TemplateDOControllerTest.java | 143 ++++++++ .../controller/VersionDOControllerTest.java | 160 +++++++++ .../exception/GlobalExceptionHandlerTest.java | 2 +- .../model/dao/CategoryDODaoTest.java | 60 ++++ .../templatehub/model/dao/StarDODaoTest.java | 27 ++ .../model/dao/TemplateDODaoTest.java | 151 ++++++++ .../model/dao/VersionDODaoTest.java | 59 +++ .../service/CategoryDOServiceTest.java | 141 ++++++++ .../service/StarDOServiceTest.java | 126 +++++++ .../service/TemplateDOServiceTest.java | 204 +++++++++++ .../service/VersionDOServiceTest.java | 227 ++++++++++++ 94 files changed, 5813 insertions(+), 708 deletions(-) create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/AccountController.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/ResourceController.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/RoleController.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/TokenStorage.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/DO/AuthResourceDO.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/DO/AuthRoleDO.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/DO/AuthRoleResourceBindDO.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/DO/AuthUserDO.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/DO/AuthUserRoleBindDO.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/DO/CategoryDO.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/DO/StarDO.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/DO/TagDO.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/DO/TemplateDO.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/DO/TemplateTagDO.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/DO/VersionDO.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/VO/TemplateVO.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/dao/AuthResourceDao.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/dao/AuthRoleDao.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/dao/AuthRoleResourceBindDao.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/dao/AuthUserDao.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/dao/AuthUserRoleBindDao.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/dto/LoginDto.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/dto/RefreshTokenResponse.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/model/dto/TokenDto.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/service/AccountService.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/service/ResourceService.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/service/RoleService.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/service/impl/AccountServiceImpl.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/service/impl/DataConflictException.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/service/impl/ResourceServiceImpl.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/service/impl/RoleServiceImpl.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/sureness/SurenessFilterExample.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/sureness/config/SurenessConfiguration.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/sureness/processor/CustomTokenProcessor.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/sureness/processor/RefreshExpiredTokenException.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/sureness/provider/DatabaseAccountProvider.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/sureness/provider/DatabasePathTreeProvider.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/sureness/subject/CustomPasswdSubjectCreator.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/sureness/subject/CustomTokenSubject.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/sureness/subject/CustomTokenSubjectCreator.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/util/JsonUtil.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/util/ResponseUtil.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/resources/application.yml create mode 100644 template-marketplace/hertzbeat-template-hub/src/main/resources/sureness.yml create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/HertzbeatTemplateDOHubApplicationTests.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/controller/CategoryDOControllerTest.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/controller/StarDOControllerTest.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/controller/TemplateDOControllerTest.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/controller/VersionDOControllerTest.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/model/dao/CategoryDODaoTest.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/model/dao/StarDODaoTest.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/model/dao/TemplateDODaoTest.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/model/dao/VersionDODaoTest.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/service/CategoryDOServiceTest.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/service/StarDOServiceTest.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/service/TemplateDOServiceTest.java create mode 100644 template-marketplace/hertzbeat-template-hub/src/test/java/org/apache/hertzbeat/templatehub/service/VersionDOServiceTest.java diff --git a/template-marketplace/hertzbeat-template-hub/apiTest/category.http b/template-marketplace/hertzbeat-template-hub/apiTest/category.http index 23be565e593..42ab2aaa914 100644 --- a/template-marketplace/hertzbeat-template-hub/apiTest/category.http +++ b/template-marketplace/hertzbeat-template-hub/apiTest/category.http @@ -1,21 +1,38 @@ -### add category -POST http://localhost:8080/api/category/upload/test +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +### add categoryDO +POST http://localhost:8080/api/categoryDO/upload/test Content-Type: application/x-www-form-urlencoded description=test -### modify category -POST http://localhost:8080/api/category/modify/1 +### modify categoryDO +POST http://localhost:8080/api/categoryDO/modify/1 Content-Type: application/x-www-form-urlencoded name=Database monitoring& description=数据库监控模版 -### delete category -DELETE http://localhost:8080/api/category/delete/1 +### delete categoryDO +DELETE http://localhost:8080/api/categoryDO/delete/1 -### get category by isDel -GET http://localhost:8080/api/category/all/0 +### get categoryDO by isDel +GET http://localhost:8080/api/categoryDO/all/0 -### get category page by isDel -GET http://localhost:8080/api/category/page/isDel/0?page=0&size=2 \ No newline at end of file +### get categoryDO page by isDel +GET http://localhost:8080/api/categoryDO/page/isDel/0?page=0&size=2 \ No newline at end of file diff --git a/template-marketplace/hertzbeat-template-hub/apiTest/share.http b/template-marketplace/hertzbeat-template-hub/apiTest/share.http index aeb3f7908bf..cced4472d49 100644 --- a/template-marketplace/hertzbeat-template-hub/apiTest/share.http +++ b/template-marketplace/hertzbeat-template-hub/apiTest/share.http @@ -1,5 +1,22 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + ### share -GET http://localhost:8080/api/share/getShareURL/100 +GET http://localhost:8080/api/share/getShareURL/1 ### downloadShare -GET http://localhost:8080/api/share/share/6laZF \ No newline at end of file +GET http://localhost:8080/api/share/download/6laZF \ No newline at end of file diff --git a/template-marketplace/hertzbeat-template-hub/apiTest/star.http b/template-marketplace/hertzbeat-template-hub/apiTest/star.http index 3cd3ab94e35..b819317d9f4 100644 --- a/template-marketplace/hertzbeat-template-hub/apiTest/star.http +++ b/template-marketplace/hertzbeat-template-hub/apiTest/star.http @@ -1,20 +1,39 @@ -### star version -POST http://localhost:8080/api/version/star +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +### get starDO templateDO id by user +GET http://localhost:8080/api/starDO/1 + +### assert templateDO is starDO by user +GET http://localhost:8080/api/starDO/isStar/1/1 + +### starDO versionDO +POST http://localhost:8080/api/templateDO/starDO Content-Type: application/x-www-form-urlencoded user=1& - template=1& - version=1 - -### get star version by user -GET http://localhost:8080/api/star/1 + templateDO=3 -### get star version page by user -GET http://localhost:8080/api/star/page/user/1?page=0&size=2 +### get starDO templateDO page by user +GET http://localhost:8080/api/starDO/page/user/1?page=0&size=10 -### cancel star -POST http://localhost:8080/api/star/cancel/1 +### cancel starDO +POST http://localhost:8080/api/starDO/cancel/1 Content-Type: application/x-www-form-urlencoded - versionId = 1 + templateId = 1 diff --git a/template-marketplace/hertzbeat-template-hub/apiTest/template.http b/template-marketplace/hertzbeat-template-hub/apiTest/template.http index c962f37170b..77690a5b21d 100644 --- a/template-marketplace/hertzbeat-template-hub/apiTest/template.http +++ b/template-marketplace/hertzbeat-template-hub/apiTest/template.http @@ -1,35 +1,48 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + ### Get templates By user GET http://localhost:8080/api/template/1 ### Get templates Page By user -GET http://localhost:8080/api/template/page/user/1?page=0&size=2 +GET http://localhost:8080/api/template/page/user/1?page=0&size=10 ### Get templates By category GET http://localhost:8080/api/template/category/1 ### Get templates Page By category -GET http://localhost:8080/api/template/page/category/1_3/0/1?page=0&size=2 - -### Get templates By namelike -GET http://localhost:8080/api/template/name/My +GET http://localhost:8080/api/template/page/category/1_3/0/1/1?page=0&size=2 -### Get templates Page By namelike -GET http://localhost:8080/api/template/page/name/My/0/1?page=0&size=2 +### Get templates Page +GET http://localhost:8080/api/template/page/0/1?page=0&size=10 ### Get templates order by order GET http://localhost:8080/api/template/query/3/0 -### Get templates Page order by order -GET http://localhost:8080/api/template/page/order/3/0?page=0&size=2 - ### Get templates Page By option -GET http://localhost:8080/api/template/page/option/My/1_3/0/1?page=0&size=2 +GET http://localhost:8080/api/template/page/option/My/1_3/0/1/1?page=0&size=10 ### Get all templates -GET http://localhost:8080/api/template +GET http://localhost:8080/api/template/ +Content-Type: application/json -### Get all templates Page -GET http://localhost:8080/api/template/page/0?page=0&size=2 +{ + "Token": "eyJhbGciOiJIUzUxMiIsInppcCI6IkRFRiJ9.eJwty0EKwjAQheG7zLqRZDpNbFfeQ0QmccSIsaVJVRDvbiruHh_vf8O1RBjAG0fhzEb15EiRR6u2QViJZ20MomMSaCAvvp6fPqNG2hnbbsKYqsecq5cxKV7KRWWZHzKvzgUG47BvyVrbNSCv6Q-dxhXm8Sa13f_GkU8p3uHw-QJ8uCs1.S7g0Z5z3xRySt_xCn8jugaMUwBO56aAZMr-tYGhoskM0hcdkG91WrEF-gAF43r_Ue3V_LGKKyGZrUUpOXeHJIA" +} ### download GET http://localhost:8080/api/template/download/1/1/v1.0.0/1 diff --git a/template-marketplace/hertzbeat-template-hub/apiTest/user.http b/template-marketplace/hertzbeat-template-hub/apiTest/user.http index fa7cc9cb6c3..1192edc5e9a 100644 --- a/template-marketplace/hertzbeat-template-hub/apiTest/user.http +++ b/template-marketplace/hertzbeat-template-hub/apiTest/user.http @@ -1 +1,46 @@ -### \ No newline at end of file +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +### Registy +POST http://localhost:8080/api/auth/register +Content-Type: application/json + +{ + "type": 1, + "identifier": "wbs2024@163.com", + "credential": "123456" +} + +### Login +POST http://localhost:8080/api/auth/token +Content-Type: application/json + +{ + "type": 1, + "identifier": "wbs2024@163.com", + "credential": "123456" +} + +### custom token +POST http://localhost:8080/api/auth/custom/token +Content-Type: application/json + +{ + "type": 1, + "identifier": "wbs2024@163.com", + "credential": "123456" +} \ No newline at end of file diff --git a/template-marketplace/hertzbeat-template-hub/apiTest/version.http b/template-marketplace/hertzbeat-template-hub/apiTest/version.http index 3ef98964d2f..251d11d6027 100644 --- a/template-marketplace/hertzbeat-template-hub/apiTest/version.http +++ b/template-marketplace/hertzbeat-template-hub/apiTest/version.http @@ -1,8 +1,25 @@ -### Get all version information of a template -GET http://localhost:8080/api/version/version/1 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# -### Get all version page of a template -GET http://localhost:8080/api/version/page/1/0?page=0&size=2 +### Get all versionDO information of a templateDO +GET http://localhost:8080/api/versionDO/versionDO/1 -### Get version information By ID -GET http://localhost:8080/api/version/get/1 \ No newline at end of file +### Get all versionDO page of a templateDO +GET http://localhost:8080/api/versionDO/page/1/0?page=0&size=2 + +### Get versionDO information By ID +GET http://localhost:8080/api/versionDO/get/1 \ No newline at end of file diff --git a/template-marketplace/hertzbeat-template-hub/pom.xml b/template-marketplace/hertzbeat-template-hub/pom.xml index 122f260bd72..d7bcaae7c50 100644 --- a/template-marketplace/hertzbeat-template-hub/pom.xml +++ b/template-marketplace/hertzbeat-template-hub/pom.xml @@ -55,6 +55,7 @@ 3.5.7 8.4.3 3.14.0 + 1.1.0 @@ -149,6 +150,23 @@ test + + org.springframework.boot + spring-boot-starter-validation + + + + com.usthe.sureness + sureness-core + 1.1.0 + + + + javax.servlet + javax.servlet-api + 3.1.0 + + diff --git a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/HertzbeatTemplateHubApplication.java b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/HertzbeatTemplateHubApplication.java index f57f0a5c03d..8b58f71dd10 100644 --- a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/HertzbeatTemplateHubApplication.java +++ b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/HertzbeatTemplateHubApplication.java @@ -19,8 +19,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.transaction.annotation.EnableTransactionManagement; +@ServletComponentScan(basePackages = "org.apache.hertzbeat.templatehub.sureness") @SpringBootApplication @EnableTransactionManagement public class HertzbeatTemplateHubApplication { diff --git a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/config/FileStorageConfig.java b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/config/FileStorageConfig.java index f88e697ba29..0a10a8b9329 100644 --- a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/config/FileStorageConfig.java +++ b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/config/FileStorageConfig.java @@ -39,10 +39,6 @@ public class FileStorageConfig { @Value("${minio.secretKey}") private String secretKey; - private String getStorageType(){ - return this.storageType; - } - @Bean public FileStorageService fileStorageService() { if ("minio".equalsIgnoreCase(storageType)) { diff --git a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/constants/CommonConstants.java b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/constants/CommonConstants.java index dff86feb7a4..5c20878d4fd 100644 --- a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/constants/CommonConstants.java +++ b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/constants/CommonConstants.java @@ -32,6 +32,11 @@ public interface CommonConstants { */ byte FAIL_CODE = 0x0F; + /** + * Response status code: Incorrect login account password + */ + byte LOGIN_FAILED_CODE = 0x06; + /** * Response status code: Parameter verification failed */ diff --git a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/AccountController.java b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/AccountController.java new file mode 100644 index 00000000000..a53fa6e4068 --- /dev/null +++ b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/AccountController.java @@ -0,0 +1,78 @@ +package org.apache.hertzbeat.templatehub.controller; + +import com.usthe.sureness.util.JsonWebTokenUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.hertzbeat.templatehub.model.DTO.LoginDto; +import org.apache.hertzbeat.templatehub.model.DTO.Message; +import org.apache.hertzbeat.templatehub.service.AccountService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.apache.hertzbeat.templatehub.constants.CommonConstants.FAIL_CODE; + +@RestController +@RequestMapping("/auth") +@Slf4j +public class AccountController { + + @Autowired + private AccountService accountService; + + private static final String TOKEN_SPLIT = "--"; + + @PostMapping("/token") + public ResponseEntity>> issueJwtToken(@RequestBody @Validated LoginDto account) { + boolean authenticatedFlag = accountService.authenticateAccount(account); + if (!authenticatedFlag) { + Message> message = Message.fail(FAIL_CODE,"username or password not incorrect"); + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(message); + } + List ownRole = accountService.loadAccountRoles(account.getIdentifier()); + String jwt = JsonWebTokenUtil.issueJwt(UUID.randomUUID().toString(), account.getIdentifier(), + "tom-auth-server", 3600L, ownRole); + Message> message = Message.success(Collections.singletonMap("token", jwt)); + return ResponseEntity.ok(message); + } + + @PostMapping("/custom/token") + public ResponseEntity>> issueCustomToken(@RequestBody @Validated LoginDto account) { + boolean authenticatedFlag = accountService.authenticateAccount(account); + if (!authenticatedFlag) { + Message> message = Message.fail(FAIL_CODE,"username or password not incorrect"); + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(message); + } + long refreshPeriodTime = 36000L; + String token = account.getIdentifier() + TOKEN_SPLIT + System.currentTimeMillis() + + TOKEN_SPLIT + refreshPeriodTime + + TOKEN_SPLIT + UUID.randomUUID().toString().replace("-", ""); + TokenStorage.addToken(account.getIdentifier(), token); + Map responseData = Collections.singletonMap("customToken", token); + Message> message = Message.success(responseData); + return ResponseEntity.status(HttpStatus.CREATED).body(message); + } + + @PostMapping("/register") + public ResponseEntity> accountRegister(@RequestBody @Validated LoginDto account) { + + if (accountService.registerAccount(account)) { + Message message=Message.success("sign up success, login after"); + if (log.isDebugEnabled()) { + log.debug("account: {}, sign up success", account); + } + return ResponseEntity.ok(message); + } else { + return ResponseEntity.ok(Message.fail(FAIL_CODE,"username already exist")); + } + } +} diff --git a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/CategoryController.java b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/CategoryController.java index 168995ec824..059e0fd1d54 100644 --- a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/CategoryController.java +++ b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/CategoryController.java @@ -17,9 +17,8 @@ package org.apache.hertzbeat.templatehub.controller; -import org.apache.hertzbeat.templatehub.exception.HertzbeatTemplateHubException; -import org.apache.hertzbeat.templatehub.model.dto.Message; -import org.apache.hertzbeat.templatehub.model.entity.Category; +import org.apache.hertzbeat.templatehub.model.DO.CategoryDO; +import org.apache.hertzbeat.templatehub.model.DTO.Message; import org.springframework.data.domain.Page; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -95,25 +94,25 @@ public ResponseEntity> deleteCategory(@PathVariable("id") int id } @GetMapping("/all/{isDel}") - public ResponseEntity>> getAllCategory(@PathVariable("isDel") int isDel) { + public ResponseEntity>> getAllCategory(@PathVariable("isDel") int isDel) { if (isDel!=0&&isDel!=1) { return ResponseEntity.ok(Message.fail(FAIL_CODE,"error option")); } - List allCategoryByIsDel = categoryService.getAllCategoryByIsDel(isDel); + List allCategoryDOByIsDel = categoryService.getAllCategoryByIsDel(isDel); - return ResponseEntity.ok(Message.success(allCategoryByIsDel)); + return ResponseEntity.ok(Message.success(allCategoryDOByIsDel)); } @GetMapping("/page/isDel/{isDel}") - public ResponseEntity>> getCategoryPageByIsDel(@PathVariable("isDel") int isDel, @RequestParam int page, @RequestParam int size) { + public ResponseEntity>> getCategoryPageByIsDel(@PathVariable("isDel") int isDel, @RequestParam int page, @RequestParam int size) { if (isDel!=0&&isDel!=1||page<0||size<0) { return ResponseEntity.ok(Message.fail(FAIL_CODE,"Params Error")); } - Page allCategoryByIsDel = categoryService.getPageByIsDel(isDel, page, size); + Page allCategoryByIsDel = categoryService.getPageByIsDel(isDel, page, size); return ResponseEntity.ok(Message.success(allCategoryByIsDel)); } diff --git a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/ResourceController.java b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/ResourceController.java new file mode 100644 index 00000000000..912a558a64f --- /dev/null +++ b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/ResourceController.java @@ -0,0 +1,92 @@ +package org.apache.hertzbeat.templatehub.controller; + +import jakarta.validation.constraints.NotBlank; +import lombok.extern.slf4j.Slf4j; +import org.apache.hertzbeat.templatehub.model.DTO.Message; +import org.apache.hertzbeat.templatehub.model.DO.AuthResourceDO; +import org.apache.hertzbeat.templatehub.service.ResourceService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +/** + * @author tomsun28 + * @date 00:24 2019-08-01 + */ +@RequestMapping("/resource") +@RestController +@Slf4j +public class ResourceController { + + @Autowired + private ResourceService resourceService; + + @PostMapping + public ResponseEntity addResource(@RequestBody @Validated AuthResourceDO authResource) { + if (resourceService.addResource(authResource)) { + if (log.isDebugEnabled()) { + log.debug("add resource success: {}", authResource); + } + return ResponseEntity.status(HttpStatus.CREATED).build(); + } else { + Message message = Message.builder().msg("resource already exist").build(); + return ResponseEntity.status(HttpStatus.CONFLICT).body(message); + } + } + + @PutMapping + public ResponseEntity updateResource(@RequestBody @Validated AuthResourceDO authResource) { + if (resourceService.updateResource(authResource)) { + if (log.isDebugEnabled()) { + log.debug("update resource success: {}", authResource); + } + return ResponseEntity.ok().build(); + } else { + Message message = Message.builder().msg("resource not exist").build(); + return ResponseEntity.status(HttpStatus.CONFLICT).body(message); + } + } + + @DeleteMapping("/{resourceId}") + public ResponseEntity deleteResource(@PathVariable @NotBlank Long resourceId ) { + if (resourceService.deleteResource(resourceId)) { + if (log.isDebugEnabled()) { + log.debug("delete resource success: {}", resourceId); + } + return ResponseEntity.ok().build(); + } else { + Message message = Message.builder().msg("delete resource fail, please try again later").build(); + log.error("delete resource fail: {}", resourceId); + return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body(message); + } + } + + @GetMapping("/{currentPage}/{pageSize}") + public ResponseEntity getResource(@PathVariable Integer currentPage, @PathVariable Integer pageSize ) { + if (Objects.isNull(currentPage) || Objects.isNull(pageSize)) { + // no pageable + Optional> resourceListOptional = resourceService.getAllResource(); + if (resourceListOptional.isPresent()) { + Message message = Message.builder().data(resourceListOptional.get()).build(); + return ResponseEntity.ok().body(message); + } else { + Message message = Message.builder().data(new ArrayList<>(0)).build(); + return ResponseEntity.ok().body(message); + } + } else { + // pageable + Page resourcePage = resourceService.getPageResource(currentPage, pageSize); + Message message = Message.builder().data(resourcePage.get()).build(); + return ResponseEntity.ok().body(message); + } + } + +} diff --git a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/RoleController.java b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/RoleController.java new file mode 100644 index 00000000000..ed0784ac72f --- /dev/null +++ b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/RoleController.java @@ -0,0 +1,137 @@ +package org.apache.hertzbeat.templatehub.controller; + +import jakarta.validation.constraints.NotBlank; +import lombok.extern.slf4j.Slf4j; +import org.apache.hertzbeat.templatehub.model.DTO.Message; +import org.apache.hertzbeat.templatehub.model.DO.AuthResourceDO; +import org.apache.hertzbeat.templatehub.model.DO.AuthRoleDO; +import org.apache.hertzbeat.templatehub.service.RoleService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +/** + * @author tomsun28 + * @date 00:24 2019-08-01 + */ +@RestController +@RequestMapping("/role") +@Slf4j +public class RoleController { + + @Autowired + private RoleService roleService; + + + @GetMapping("/resource/{roleId}/{currentPage}/{pageSize}") + public ResponseEntity getResourceOwnByRole(@PathVariable @NotBlank Long roleId, @PathVariable Integer currentPage, @PathVariable Integer pageSize) { + if (currentPage == null){ + currentPage = 1; + } + if (pageSize == null) { + pageSize = 10; + } + Page resourcePage = roleService.getPageResourceOwnRole(roleId, currentPage, pageSize); + Message message = Message.builder().data(resourcePage).build(); + return ResponseEntity.ok().body(message); + } + + @GetMapping("/resource/-/{roleId}/{currentPage}/{pageSize}") + public ResponseEntity getResourceNotOwnByRole(@PathVariable @NotBlank Long roleId, @PathVariable Integer currentPage, @PathVariable Integer pageSize) { + if (currentPage == null){ + currentPage = 1; + } + if (pageSize == null) { + pageSize = 10; + } + Page resourcePage = roleService.getPageResourceNotOwnRole(roleId, currentPage, pageSize); + Message message = Message.builder().data(resourcePage).build(); + return ResponseEntity.ok().body(message); + } + + @PostMapping("/authority/resource/{roleId}/{resourceId}") + public ResponseEntity authorityRoleResource(@PathVariable @NotBlank Long roleId, + @PathVariable @NotBlank Long resourceId) { + roleService.authorityRoleResource(roleId,resourceId); + return ResponseEntity.status(HttpStatus.OK).build(); + } + + @DeleteMapping("/authority/resource/{roleId}/{resourceId}") + public ResponseEntity deleteAuthorityRoleResource(@PathVariable @NotBlank Long roleId, + @PathVariable @NotBlank Long resourceId) { + roleService.deleteAuthorityRoleResource(roleId,resourceId); + return ResponseEntity.status(HttpStatus.OK).build(); + } + + @PostMapping + public ResponseEntity addRole(@RequestBody @Validated AuthRoleDO authRole) { + if (roleService.addRole(authRole)) { + if (log.isDebugEnabled()) { + log.debug("add role success: {}", authRole); + } + return ResponseEntity.status(HttpStatus.CREATED).build(); + } else { + Message message = Message.builder() + .msg("role already exist").build(); + return ResponseEntity.status(HttpStatus.CONFLICT).body(message); + } + } + + @PutMapping + public ResponseEntity updateRole(@RequestBody @Validated AuthRoleDO authRole) { + if (roleService.updateRole(authRole)) { + if (log.isDebugEnabled()) { + log.debug("update role success: {}", authRole); + } + return ResponseEntity.ok().build(); + } else { + Message message = Message.builder() + .msg("role not exist").build(); + return ResponseEntity.status(HttpStatus.CONFLICT).body(message); + } + } + + @DeleteMapping("/{roleId}") + public ResponseEntity deleteRole(@PathVariable @NotBlank Long roleId) { + if (roleService.deleteRole(roleId)) { + if (log.isDebugEnabled()) { + log.debug("delete role success: {}", roleId); + } + return ResponseEntity.ok().build(); + } else { + Message message = Message.builder() + .msg("delete role fail, no this role here").build(); + log.debug("delete role fail: {}", roleId); + return ResponseEntity.status(HttpStatus.CONFLICT).body(message); + } + } + + @GetMapping("/{currentPage}/{pageSize}") + public ResponseEntity getRole(@PathVariable Integer currentPage, @PathVariable Integer pageSize ) { + if (Objects.isNull(currentPage) || Objects.isNull(pageSize)) { + // no pageable + Optional> roleListOptional = roleService.getAllRole(); + if (roleListOptional.isPresent()) { + Message message = Message.builder().data(roleListOptional.get()).build(); + return ResponseEntity.ok().body(message); + } else { + Message message = Message.builder().data(new ArrayList<>()).build(); + return ResponseEntity.ok().body(message); + } + } else { + // pageable + Page rolePage = roleService.getPageRole(currentPage, pageSize); + Message message = Message.builder().data(rolePage).build(); + return ResponseEntity.ok().body(message); + } + } + +} diff --git a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/ShareController.java b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/ShareController.java index 392af425acf..c32d0df1ad0 100644 --- a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/ShareController.java +++ b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/ShareController.java @@ -18,9 +18,9 @@ package org.apache.hertzbeat.templatehub.controller; import lombok.extern.slf4j.Slf4j; -import org.apache.hertzbeat.templatehub.model.dto.Message; -import org.apache.hertzbeat.templatehub.model.entity.Template; -import org.apache.hertzbeat.templatehub.model.entity.Version; +import org.apache.hertzbeat.templatehub.model.DO.VersionDO; +import org.apache.hertzbeat.templatehub.model.DTO.Message; +import org.apache.hertzbeat.templatehub.model.DO.TemplateDO; import org.apache.hertzbeat.templatehub.service.TemplateService; import org.apache.hertzbeat.templatehub.service.VersionService; import org.apache.hertzbeat.templatehub.util.Base62Util; @@ -57,40 +57,33 @@ public class ShareController { @GetMapping("/getShareURL/{version}") public ResponseEntity> getShareURL(@PathVariable("version") long versionId){ - if(versionId<=0){ - return ResponseEntity.ok(Message.fail(FAIL_CODE,"params error")); - } + if(versionId<=0) return ResponseEntity.ok(Message.fail(FAIL_CODE,"params error")); - Version version = versionService.getVersion((int) versionId); - if(version==null){ - return ResponseEntity.ok(Message.fail(FAIL_CODE,"version not found")); - } + VersionDO versionDO = versionService.getVersion((int) versionId); + if(versionDO ==null) return ResponseEntity.ok(Message.fail(FAIL_CODE,"version not found")); String base62Key = Base62Util.idToShortKey(versionId+100000000); - return ResponseEntity.ok(Message.success("http://"+serverAddress+":"+serverPort+contextPath+"/share/share/"+base62Key)); + return ResponseEntity.ok(Message.success("http://"+serverAddress+":"+serverPort+contextPath+"/share/download/"+base62Key)); } - @GetMapping("/share/{key}") + @GetMapping("/download/{key}") public ResponseEntity downloadShare(@PathVariable("key") String key){ - if(!Base62Util.isBase62(key)){ - return ResponseEntity.notFound().build(); - } + if(!Base62Util.isBase62(key)) return ResponseEntity.notFound().build(); long versionId = (int) Base62Util.shortKeyToId(key)-100000000; - Version version = versionService.getVersion((int) versionId); + VersionDO versionDO = versionService.getVersion((int) versionId); - Template template = templateService.getTemplate(version.getTemplate()); + if(versionDO ==null) return ResponseEntity.notFound().build(); - Resource resource = templateService.downloadTemplate(template.getUser(), template.getId(), version.getVersion(), (int) versionId); + TemplateDO templateDO = templateService.getTemplate(versionDO.getTemplateId()); - if(resource!=null){ - return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + version.getVersion() + ".yml\"") - .body(resource); - }else { - return ResponseEntity.notFound().build(); - } + Resource resource = templateService.downloadTemplate(templateDO.getUser(), templateDO.getId(), versionDO.getVersion(), (int) versionId); + + if(resource!=null) return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, + "attachment; filename=\"" + versionDO.getVersion() + ".yml\"").body(resource); + else return ResponseEntity.notFound().build(); } } diff --git a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/StarController.java b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/StarController.java index 1fcacfddd83..687bef16032 100644 --- a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/StarController.java +++ b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/StarController.java @@ -18,17 +18,21 @@ package org.apache.hertzbeat.templatehub.controller; import lombok.extern.slf4j.Slf4j; -import org.apache.hertzbeat.templatehub.model.dto.Message; -import org.apache.hertzbeat.templatehub.model.entity.Version; +import org.apache.hertzbeat.templatehub.model.DO.TemplateDO; +import org.apache.hertzbeat.templatehub.model.DTO.Message; +import org.apache.hertzbeat.templatehub.model.VO.TemplateVO; import org.apache.hertzbeat.templatehub.service.StarService; import org.apache.hertzbeat.templatehub.service.TemplateService; import org.apache.hertzbeat.templatehub.service.VersionService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; import org.springframework.http.ResponseEntity; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; +import java.util.Collections; import java.util.List; import static org.apache.hertzbeat.templatehub.constants.CommonConstants.FAIL_CODE; @@ -48,50 +52,62 @@ public class StarController { @Autowired private TemplateService templateService; - @Deprecated @GetMapping("/{user}") - public ResponseEntity>> getAllVersionByUserStar(@PathVariable("user") int userId){ - if(userId<=0){ - return ResponseEntity.ok(Message.fail(FAIL_CODE,"params error")); - } + public ResponseEntity>> getAllTemplateIdByUserStar(@PathVariable("user") int userId){ + if(userId<=0) return ResponseEntity.ok(Message.fail(FAIL_CODE,"params error")); + + List templateByUserStar = starService.getTemplateByUserStar(userId, 0); + Collections.sort(templateByUserStar); + return ResponseEntity.ok(Message.success(templateByUserStar)); + } + + @GetMapping("/isStar/{user}/{templateId}") + public ResponseEntity> assertTemplateIdIsStarByUser(@PathVariable("user") int userId, + @PathVariable("templateId") int templateId){ + if(userId<=0||templateId<=0) return ResponseEntity.ok(Message.fail(FAIL_CODE,"params error")); - List versionByUserStar = starService.getVersionByUserStar(userId, 0, 0, 0); - return ResponseEntity.ok(Message.success(versionByUserStar)); + Boolean b = starService.assertTemplateIdIsStarByUser(userId, templateId); + + return ResponseEntity.ok(Message.success(b)); } @GetMapping("/page/user/{user}") - public ResponseEntity>> getVersionPageByUserStar(@PathVariable("user") int userId, - @RequestParam(required = false) Integer page, - @RequestParam(required = false) Integer size){ + public ResponseEntity>> getTemplatePageByUserStar(@PathVariable("user") int userId, + @RequestParam(required = false) Integer page, + @RequestParam(required = false) Integer size){ if(userId<=0){ return ResponseEntity.ok(Message.fail(FAIL_CODE,"User Error")); } - Page versionByUserStar = starService.getPageByUserStar(userId, 0, 0, 0,page,size); - return ResponseEntity.ok(Message.success(versionByUserStar)); + Page res = starService.getPageByUserStar(userId, 0, 0, 0,page,size); + List templateVOList = res.getContent() + .stream() + .map(templateDO -> new TemplateVO(templateDO,true)) + .toList(); + return ResponseEntity.ok(Message.success(new PageImpl<>(templateVOList, PageRequest.of(page,size), res.getTotalElements()))); } @Transactional @PostMapping("/cancel/{user}") - public ResponseEntity> cancelStar(@PathVariable("user") int userId, @RequestParam("versionId") int versionId){ - if(userId<=0||versionId<=0){ - return ResponseEntity.ok(Message.fail(FAIL_CODE,"User Error")); + public ResponseEntity> cancelStar(@PathVariable("user") int userId, @RequestParam("templateId") int templateId){ + if(userId<=0||templateId<=0){ + return ResponseEntity.ok(Message.fail(FAIL_CODE,"Params Error")); } - Boolean isOk = starService.cancelStarByUser(userId, versionId); - if(!isOk){ - return ResponseEntity.ok(Message.fail(FAIL_CODE,"cancel Error")); + boolean b=starService.assertTemplateIdIsStarByUser(userId, templateId); + if(!b){ + return ResponseEntity.ok(Message.fail(FAIL_CODE,"Template is not star")); } - int tempateId = versionService.cancelStarVersion(versionId); - if(tempateId<=0){ + Boolean isOk = starService.cancelStarByUser(userId, templateId); + if(!isOk){ return ResponseEntity.ok(Message.fail(FAIL_CODE,"cancel Error")); } - boolean isOk2 = templateService.cancelStarTemplate(tempateId); + boolean isOk2 = templateService.cancelStarTemplate(templateId); if(!isOk2){ return ResponseEntity.ok(Message.fail(FAIL_CODE,"cancel Error")); } - return ResponseEntity.ok(Message.success("success")); + return ResponseEntity.ok(Message.success("cancel success")); } } diff --git a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/TemplateController.java b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/TemplateController.java index 28245d6ee2b..95ef50cefb8 100644 --- a/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/TemplateController.java +++ b/template-marketplace/hertzbeat-template-hub/src/main/java/org/apache/hertzbeat/templatehub/controller/TemplateController.java @@ -20,16 +20,21 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.hertzbeat.templatehub.exception.HertzbeatTemplateHubException; -import org.apache.hertzbeat.templatehub.model.dto.Message; -import org.apache.hertzbeat.templatehub.model.dto.TemplateDto; -import org.apache.hertzbeat.templatehub.model.entity.Template; -import org.apache.hertzbeat.templatehub.model.entity.Version; +import org.apache.hertzbeat.templatehub.model.DO.TemplateDO; +import org.apache.hertzbeat.templatehub.model.DO.VersionDO; +import org.apache.hertzbeat.templatehub.model.DTO.Message; +import org.apache.hertzbeat.templatehub.model.DTO.TemplateDto; +import org.apache.hertzbeat.templatehub.model.VO.TemplateVO; +import org.apache.hertzbeat.templatehub.service.StarService; import org.apache.hertzbeat.templatehub.service.VersionService; import org.jetbrains.annotations.NotNull; import org.springframework.core.io.Resource; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import org.apache.hertzbeat.templatehub.service.TemplateService; import lombok.extern.slf4j.Slf4j; @@ -55,8 +60,12 @@ public class TemplateController { @Autowired private VersionService versionService; + @Autowired + private StarService starService; + @PostMapping("/upload") - public ResponseEntity> uploadTemplate(@ModelAttribute("templateDto") String s, @RequestParam("file") MultipartFile file){ + public ResponseEntity> uploadTemplate(@ModelAttribute("templateDto") String s, + @RequestParam("file") MultipartFile file){ if(file.isEmpty()||s==null || s.isEmpty()) return ResponseEntity.ok(Message.fail(FAIL_CODE,"params error")); ObjectMapper objectMapper = new ObjectMapper(); @@ -64,7 +73,8 @@ public ResponseEntity> uploadTemplate(@ModelAttribute("templateD try { templateDto = objectMapper.readValue(s, TemplateDto.class); } catch (JsonProcessingException e) { - return ResponseEntity.ok(Message.fail(FAIL_CODE,"Template description information reading exception"+e.getMessage())); + return ResponseEntity.ok(Message.fail(FAIL_CODE, + "Template description information reading exception"+e.getMessage())); } templateDto.setCreate_time(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); templateDto.setUpdate_time(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); @@ -86,9 +96,10 @@ public ResponseEntity> getCountByIsDelAndOffshelf(@PathVariable return ResponseEntity.ok(Message.success(countByIsDelAndOffShelf)); } - @GetMapping("/page/category/{categoryIdStr}/{isDel}/{orderOption}") - public ResponseEntity>> getTemplatePageByCategory(@PathVariable("categoryIdStr") String categoryIdStr, @PathVariable("isDel") int isDel, - @PathVariable("orderOption") int orderOption, @RequestParam int page, @RequestParam int size){ + @GetMapping("/page/category/{categoryIdStr}/{isDel}/{orderOption}/{userId}") + public ResponseEntity>> getTemplatePageByCategory(@PathVariable("categoryIdStr") String categoryIdStr, @PathVariable("isDel") int isDel, + @PathVariable("orderOption") int orderOption, @PathVariable("userId") int userId, + @RequestParam int page, @RequestParam int size){ String[] s = categoryIdStr.split("_"); List categoryIdList=new ArrayList<>(); for (String string : s) { @@ -104,53 +115,56 @@ public ResponseEntity>> getTemplatePageByCategory(@PathVa if(categoryIdList.isEmpty() ||page<0||size<0||orderOption<=0||isDel<0||isDel>1) return ResponseEntity.ok(Message.fail(FAIL_CODE,"Params Error")); - Page