From ad0ca30f4831d94c731f1c0ba22c8d0a12c88cf4 Mon Sep 17 00:00:00 2001 From: lannoy0523 <46735290+lannoy0523@users.noreply.github.com> Date: Thu, 21 Sep 2023 10:33:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=94=B6=E8=97=8F=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=96=B0=E5=A2=9E=20#1175=20(#1176)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat:收藏项目接口新增 #1175 * feat:收藏项目接口新增 #1175 * feat:收藏项目接口新增 #1175 * feat:收藏项目接口新增 #1175 * feat:收藏项目接口新增 #1175 --- docs/apidoc/node/favorites.md | 114 ++++++++++++++++-- .../pojo/favorite/FavoriteCreateRequset.kt | 4 +- .../favorite/FavoriteProjectPageRequest.kt | 40 ++++++ .../controller/user/FavoriteController.kt | 50 ++++++-- .../bkrepo/repository/model/TFavorites.kt | 6 +- .../FavoriteService.kt} | 13 +- .../impl/FavoriteServiceImpl.kt} | 28 ++++- 7 files changed, 226 insertions(+), 29 deletions(-) create mode 100644 src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/favorite/FavoriteProjectPageRequest.kt rename src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/{folder/FolderService.kt => favorites/FavoriteService.kt} (84%) rename src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/{folder/impl/FolderServiceImpl.kt => favorites/impl/FavoriteServiceImpl.kt} (74%) diff --git a/docs/apidoc/node/favorites.md b/docs/apidoc/node/favorites.md index 38f3c80b73..fea3166dc3 100644 --- a/docs/apidoc/node/favorites.md +++ b/docs/apidoc/node/favorites.md @@ -83,7 +83,8 @@ "repoName" : "generic-local", "path" : "/123", "userId" : "admin", - "createdDate" : "2020-07-27T16:02:31.394" + "createdDate" : "2020-07-27T16:02:31.394", + "type": "NODE" } ] }, @@ -93,22 +94,23 @@ - record字段说明 - | 字段 | 类型 | 说明 | Description | - | ------------- | -------- | -------------- | ---------------------- | - | id | string | id | id | + | 字段 | 类型 | 说明 | Description | + | ------------- |--------|----------------------| ---------------------- | + | id | string | id | id | | projectId | string | 节点所属项目 | node project id | | repoName | string | 节点所属仓库 | node repository name | | path | string | 目录完整路径 | node path | - | userId | string | 创建者 | userId | - | createdDate | string | 创建时间 | create time | + | userId | string | 创建者 | userId | + | createdDate | string | 创建时间 | create time | + | type | string | 类型 | data type | -## 删除收藏文件夹 +## 删除收藏 - API: DELETE /repository/api/favorite/delete/{id} - API 名称: delete favorite - 功能说明: - - 中文:删除收藏文件夹 + - 中文:删除收藏 - English:delete favorite - 请求体 此接口请求体为空 @@ -128,3 +130,99 @@ "traceId": null } ``` + ## 创建收藏文件夹 + +- API: POST /repository/api/favorite/create/project/{projectId} +- API 名称: create_favorite_project +- 功能说明: + + - 中文:创建收藏项目 + - English:create favorite project +- 请求体 + ```json + { + "projectId": "", + "repoName": "", + "path": "" + } + ``` +- 请求字段说明 + + +| 字段 | 类型 | 是否必须 | 默认值 | 说明 | Description | + | ----------- | -------- | ---------- | -------- | ---------- | -------------- | +| projectId | string | 是 | 无 | 项目名称 | project name | +| repoName | string | 是 | 无 | 仓库名称 | repo name | +| path | string | 是 | 无 | 完整路径 | path | + +- 响应体 + + ```json + { + "code": 0, + "message": null, + "data": null, + "traceId": null + } + ``` +## 分页查询收藏项目 + +- API: POST /repository/api/favorite/page/project +- API 名称: favorite_project_page +- 功能说明: + + - 中文:分页查询收藏项目 + - English:list favorite project page +- 请求体 + + ```json + { + "pageNumber": 1, + "pageSize": 20 + } + ``` +- 请求字段说明 + + +| 字段 | 类型 | 是否必须 | 默认值 | 说明 | Description | + | ------------ | -------- | ---------- | -------- | ---------- | -------------- | +| pageNumber | int | 否 | 1 | 当前页 | current page | +| pageSize | int | 否 | 20 | 分页大小 | page size | +- 响应体 + + ```json + { + "code": 0, + "message": null, + "data": { + "pageNumber": 1, + "pageSize": 20, + "totalRecords": 1, + "totalPages": 1, + "records": [ + { + "id" : "64e4634342ad44416be9f675", + "projectId" : "blueking", + "repoName" : "generic-local", + "path" : "/123", + "userId" : "admin", + "createdDate" : "2020-07-27T16:02:31.394", + "type": "PROJECT" + } + ] + }, + "traceId": null + } + ``` +- record字段说明 + + +| 字段 | 类型 | 说明 | Description | + | ------------- |--------|----------------------| ---------------------- | +| id | string | id | id | +| projectId | string | 节点所属项目 | node project id | +| repoName | string | 节点所属仓库 | node repository name | +| path | string | 目录完整路径 | node path | +| userId | string | 创建者 | userId | +| createdDate | string | 创建时间 | create time | +| type | string | 类型 | data type | \ No newline at end of file diff --git a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/favorite/FavoriteCreateRequset.kt b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/favorite/FavoriteCreateRequset.kt index 7260d2b515..da8d5bf859 100644 --- a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/favorite/FavoriteCreateRequset.kt +++ b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/favorite/FavoriteCreateRequset.kt @@ -44,5 +44,7 @@ data class FavoriteCreateRequset( @ApiModelProperty("收藏用户") val userId: String, @ApiModelProperty("收藏时间") - val createdDate: LocalDateTime + val createdDate: LocalDateTime, + @ApiModelProperty("类型") + val type: String? = "NODE", ) diff --git a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/favorite/FavoriteProjectPageRequest.kt b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/favorite/FavoriteProjectPageRequest.kt new file mode 100644 index 0000000000..80711c8f57 --- /dev/null +++ b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/favorite/FavoriteProjectPageRequest.kt @@ -0,0 +1,40 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. + * + * A copy of the MIT License is included in this file. + * + * + * Terms of the MIT License: + * --------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.tencent.bkrepo.repository.pojo.favorite + +import com.tencent.bkrepo.common.api.constant.DEFAULT_PAGE_NUMBER +import com.tencent.bkrepo.common.api.constant.DEFAULT_PAGE_SIZE + +data class FavoriteProjectPageRequest( + var pageNumber: Int = DEFAULT_PAGE_NUMBER, + var pageSize: Int = DEFAULT_PAGE_SIZE +) diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/user/FavoriteController.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/user/FavoriteController.kt index 9ef80683d2..e60337efbc 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/user/FavoriteController.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/user/FavoriteController.kt @@ -32,6 +32,7 @@ package com.tencent.bkrepo.repository.controller.user import com.tencent.bkrepo.auth.pojo.enums.PermissionAction +import com.tencent.bkrepo.auth.pojo.enums.ResourceType import com.tencent.bkrepo.common.api.constant.HttpStatus import com.tencent.bkrepo.common.api.pojo.Page import com.tencent.bkrepo.common.api.pojo.Response @@ -40,8 +41,9 @@ import com.tencent.bkrepo.common.service.util.ResponseBuilder import com.tencent.bkrepo.repository.model.TFavorites import com.tencent.bkrepo.repository.pojo.favorite.FavoriteCreateRequset import com.tencent.bkrepo.repository.pojo.favorite.FavoritePageRequest +import com.tencent.bkrepo.repository.pojo.favorite.FavoriteProjectPageRequest import com.tencent.bkrepo.repository.pojo.favorite.FavoriteRequest -import com.tencent.bkrepo.repository.service.folder.FolderService +import com.tencent.bkrepo.repository.service.favorites.FavoriteService import io.swagger.annotations.Api import io.swagger.annotations.ApiOperation import org.springframework.web.bind.annotation.DeleteMapping @@ -57,7 +59,7 @@ import java.time.LocalDateTime @RestController @RequestMapping("/api/favorite") class FavoriteController( - private val folderService: FolderService, + private val favoriteService: FavoriteService, private val permissionManager: PermissionManager ) { @@ -76,20 +78,24 @@ class FavoriteController( createdDate = LocalDateTime.now(), userId = userId ) - folderService.createFavorite(createRequest) + favoriteService.createFavorite(createRequest) return ResponseBuilder.success() } } - @ApiOperation("删除收藏文件夹") + @ApiOperation("删除收藏") @DeleteMapping("/delete/{id}") fun removeFavorite( @RequestAttribute userId: String, @PathVariable id:String ): Response { - folderService.getFavoriteById(id)?.let { - permissionManager.checkNodePermission(PermissionAction.VIEW, it.projectId, it.repoName, it.path) - folderService.removeFavorite(id) + favoriteService.getFavoriteById(id)?.let { + if (it.type == ResourceType.PROJECT.name) { + permissionManager.isAdminUser(userId) + } else { + permissionManager.checkNodePermission(PermissionAction.VIEW, it.projectId, it.repoName, it.path) + } + favoriteService.removeFavorite(id) return ResponseBuilder.success() } return ResponseBuilder.fail(HttpStatus.BAD_REQUEST.value, "id not existed") @@ -100,7 +106,35 @@ class FavoriteController( fun pageFavorite( @RequestBody favoritePageRequest: FavoritePageRequest ): Response> { - return ResponseBuilder.success(folderService.pageFavorite(favoritePageRequest)) + return ResponseBuilder.success(favoriteService.pageFavorite(favoritePageRequest)) } + @ApiOperation("创建项目收藏") + @PostMapping( "/create/project") + fun mkProjectFavorite( + @RequestAttribute userId: String, + @RequestBody favoriteRequest: FavoriteRequest + ): Response { + with(favoriteRequest) { + permissionManager.isAdminUser(userId) + val createRequest = FavoriteCreateRequset( + projectId = projectId, + repoName = repoName, + path = path, + createdDate = LocalDateTime.now(), + userId = userId, + type = ResourceType.PROJECT.name + ) + favoriteService.createFavorite(createRequest) + return ResponseBuilder.success() + } + } + + @ApiOperation("收藏项目分页查询") + @PostMapping("/page/project") + fun pageProjectFavorite( + @RequestBody favoriteProjectPageRequest: FavoriteProjectPageRequest + ): Response> { + return ResponseBuilder.success(favoriteService.pageProjectFavorite(favoriteProjectPageRequest)) + } } \ No newline at end of file diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/model/TFavorites.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/model/TFavorites.kt index 8eed5ad3b9..e776798404 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/model/TFavorites.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/model/TFavorites.kt @@ -31,6 +31,7 @@ package com.tencent.bkrepo.repository.model +import com.tencent.bkrepo.auth.pojo.enums.ResourceType import org.springframework.data.mongodb.core.index.CompoundIndex import org.springframework.data.mongodb.core.index.CompoundIndexes import org.springframework.data.mongodb.core.mapping.Document @@ -40,7 +41,7 @@ import java.time.LocalDateTime @CompoundIndexes( CompoundIndex( name = "favorites_idx", - def = "{'userId': 1, 'repoName': 1, 'projectId': 1, 'path': 1}", + def = "{'userId': 1, 'projectId': 1,'repoName': 1, 'path': 1, 'type':1}", background = true, unique = true ) @@ -51,5 +52,6 @@ data class TFavorites( var projectId: String, var repoName: String, var path: String, - var createdDate: LocalDateTime + var createdDate: LocalDateTime, + var type: String? = ResourceType.NODE.name ) diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/folder/FolderService.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/favorites/FavoriteService.kt similarity index 84% rename from src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/folder/FolderService.kt rename to src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/favorites/FavoriteService.kt index 199b29d2bd..a21f13f1da 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/folder/FolderService.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/favorites/FavoriteService.kt @@ -29,20 +29,23 @@ * SOFTWARE. */ -package com.tencent.bkrepo.repository.service.folder +package com.tencent.bkrepo.repository.service.favorites import com.tencent.bkrepo.common.api.pojo.Page import com.tencent.bkrepo.repository.model.TFavorites import com.tencent.bkrepo.repository.pojo.favorite.FavoriteCreateRequset import com.tencent.bkrepo.repository.pojo.favorite.FavoritePageRequest +import com.tencent.bkrepo.repository.pojo.favorite.FavoriteProjectPageRequest -interface FolderService { - // 创建收藏文件夹 +interface FavoriteService { + // 创建收藏 fun createFavorite(favoriteRequest: FavoriteCreateRequset) - // 删除收藏文件夹 + // 删除收藏 fun removeFavorite(id: String) - // 获取列表 + // 获取收藏文件列表 fun pageFavorite(favoritePageRequest: FavoritePageRequest): Page // 获取特定的收藏数据 fun getFavoriteById(id: String): TFavorites? + // 获取收藏项目 + fun pageProjectFavorite(favoritePageRequest: FavoriteProjectPageRequest): Page } \ No newline at end of file diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/folder/impl/FolderServiceImpl.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/favorites/impl/FavoriteServiceImpl.kt similarity index 74% rename from src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/folder/impl/FolderServiceImpl.kt rename to src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/favorites/impl/FavoriteServiceImpl.kt index f5d90efea3..2ded94c909 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/folder/impl/FolderServiceImpl.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/favorites/impl/FavoriteServiceImpl.kt @@ -29,23 +29,25 @@ * SOFTWARE. */ -package com.tencent.bkrepo.repository.service.folder.impl +package com.tencent.bkrepo.repository.service.favorites.impl +import com.tencent.bkrepo.auth.pojo.enums.ResourceType import com.tencent.bkrepo.common.api.pojo.Page import com.tencent.bkrepo.common.mongo.dao.util.Pages import com.tencent.bkrepo.repository.dao.FavoriteDao import com.tencent.bkrepo.repository.model.TFavorites import com.tencent.bkrepo.repository.pojo.favorite.FavoriteCreateRequset import com.tencent.bkrepo.repository.pojo.favorite.FavoritePageRequest -import com.tencent.bkrepo.repository.service.folder.FolderService +import com.tencent.bkrepo.repository.pojo.favorite.FavoriteProjectPageRequest +import com.tencent.bkrepo.repository.service.favorites.FavoriteService import org.springframework.data.mongodb.core.query.Criteria import org.springframework.data.mongodb.core.query.Query import org.springframework.stereotype.Service @Service -class FolderServiceImpl( +class FavoriteServiceImpl( private val favoriteDao: FavoriteDao, -) :FolderService{ +) : FavoriteService { override fun createFavorite(favoriteRequest: FavoriteCreateRequset) { val favorite = TFavorites( @@ -53,7 +55,8 @@ class FolderServiceImpl( repoName = favoriteRequest.repoName, projectId = favoriteRequest.projectId, userId = favoriteRequest.userId, - createdDate = favoriteRequest.createdDate + createdDate = favoriteRequest.createdDate, + type = favoriteRequest.type ) favoriteDao.insert(favorite) } @@ -63,6 +66,10 @@ class FolderServiceImpl( val query = Query() projectId?.let { query.addCriteria(Criteria.where("prokectId").`is`(projectId)) } repoName?.let { query.addCriteria(Criteria.where("repoId").`is`(repoName)) } + query.addCriteria(Criteria().orOperator( + Criteria.where("type").exists(false), + Criteria.where("type").`is`(ResourceType.NODE.name) + )) val records = favoriteDao.find(query) val pageRequest = Pages.ofRequest(pageNumber, pageSize) val totalRecords = favoriteDao.count(query) @@ -77,4 +84,15 @@ class FolderServiceImpl( override fun getFavoriteById(id: String): TFavorites? { return favoriteDao.findById(id) } + + override fun pageProjectFavorite(favoritePageRequest: FavoriteProjectPageRequest): Page { + with(favoritePageRequest) { + val query = Query() + query.addCriteria(Criteria.where("type").`is`(ResourceType.PROJECT.name)) + val records = favoriteDao.find(query) + val pageRequest = Pages.ofRequest(pageNumber, pageSize) + val totalRecords = favoriteDao.count(query) + return Pages.ofResponse(pageRequest, totalRecords, records) + } + } } \ No newline at end of file