From 059b5dc2cb2f135428ca69a891a1c36a2e51047e Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Sun, 28 Apr 2024 15:29:06 +0800 Subject: [PATCH] Feat(StoreController): Add get favorite tool list api Add get favorite tool list api to StoreController --- .../api/controller/tool/StoreController.kt | 18 ++++++ .../oxygen/api/entity/tool/ToolFavorite.kt | 24 ++----- .../oxygen/api/mapper/tool/StoreMapper.kt | 22 +++---- .../api/param/tool/ToolFavoriteAddParam.kt | 23 ++----- .../api/param/tool/ToolFavoriteRemoveParam.kt | 21 ++---- .../oxygen/api/service/tool/IStoreService.kt | 13 ++++ .../api/service/tool/impl/StoreServiceImpl.kt | 49 ++++++++++---- ..._240426__Add_table_'t_b_tool_favorite'.sql | 15 ++--- src/main/resources/mapper/tool/EditMapper.xml | 14 ++-- .../resources/mapper/tool/StoreMapper.xml | 64 ++++--------------- 10 files changed, 118 insertions(+), 145 deletions(-) diff --git a/src/main/kotlin/top/fatweb/oxygen/api/controller/tool/StoreController.kt b/src/main/kotlin/top/fatweb/oxygen/api/controller/tool/StoreController.kt index 935ab4e..f3c1e58 100644 --- a/src/main/kotlin/top/fatweb/oxygen/api/controller/tool/StoreController.kt +++ b/src/main/kotlin/top/fatweb/oxygen/api/controller/tool/StoreController.kt @@ -98,4 +98,22 @@ class StoreController( return ResponseResult.databaseSuccess(ResponseCode.DATABASE_DELETE_SUCCESS) } + + /** + * Get favorite tool + * + * @param pageSortParam Page sort parameters + * @return Response object includes favorite tool paging information + * @author FatttSnake, fatttsnake@gmail.com + * @since 1.0.0 + * @see PageSortParam + * @see ResponseResult + * @see PageVo + * @see ToolVo + */ + @Trim + @Operation(summary = "获取收藏工具") + @GetMapping("/favorite") + fun getFavorite(@Valid pageSortParam: PageSortParam): ResponseResult> = + ResponseResult.databaseSuccess(data = storeService.getFavorite(pageSortParam)) } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/oxygen/api/entity/tool/ToolFavorite.kt b/src/main/kotlin/top/fatweb/oxygen/api/entity/tool/ToolFavorite.kt index 56c7f18..1d474e6 100644 --- a/src/main/kotlin/top/fatweb/oxygen/api/entity/tool/ToolFavorite.kt +++ b/src/main/kotlin/top/fatweb/oxygen/api/entity/tool/ToolFavorite.kt @@ -1,10 +1,6 @@ package top.fatweb.oxygen.api.entity.tool -import com.baomidou.mybatisplus.annotation.TableField -import com.baomidou.mybatisplus.annotation.TableId -import com.baomidou.mybatisplus.annotation.TableLogic -import com.baomidou.mybatisplus.annotation.TableName -import com.baomidou.mybatisplus.annotation.Version +import com.baomidou.mybatisplus.annotation.* import java.io.Serializable @TableName("t_b_tool_favorite") @@ -28,13 +24,13 @@ class ToolFavorite : Serializable { var userId: Long? = null /** - * Username + * Author ID * * @author FatttSnake, fatttsnake@gmail.com * @since 1.0.0 */ - @TableField("username") - var username: String? = null + @TableField("author_id") + var authorId: Long? = null /** * Tool ID @@ -45,16 +41,6 @@ class ToolFavorite : Serializable { @TableField("tool_id") var toolId: String? = null - /** - * Platform - * - * @author FatttSnake, fatttsnake@gmail.com - * @since 1.0.0 - * @see ToolBase.Platform - */ - @TableField("platform") - var platform: ToolBase.Platform? = null - /** * Deleted * @@ -76,6 +62,6 @@ class ToolFavorite : Serializable { var version: Int? = null override fun toString(): String { - return "ToolFavorite(id=$id, userId=$userId, username=$username, toolId=$toolId, platform=$platform, deleted=$deleted, version=$version)" + return "ToolFavorite(id=$id, userId=$userId, authorId=$authorId, toolId=$toolId, deleted=$deleted, version=$version)" } } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/oxygen/api/mapper/tool/StoreMapper.kt b/src/main/kotlin/top/fatweb/oxygen/api/mapper/tool/StoreMapper.kt index e464b45..7efdf91 100644 --- a/src/main/kotlin/top/fatweb/oxygen/api/mapper/tool/StoreMapper.kt +++ b/src/main/kotlin/top/fatweb/oxygen/api/mapper/tool/StoreMapper.kt @@ -40,17 +40,6 @@ interface StoreMapper : BaseMapper { */ fun selectAuthorToolIdPageByUsername(page: IPage, @Param("username") username: String): IPage - /** - * Select tool in list by tool IDs - * - * @param ids List of tool IDs - * @return List of Tool object - * @author FatttSnake, fatttsnake@gmail.com - * @since 1.0.0 - * @see Tool - */ - fun selectListByIds(@Param("ids") ids: List): List - /** * Select tool in list by Author:Tool_ID * @@ -61,4 +50,15 @@ interface StoreMapper : BaseMapper { * @see Tool */ fun selectListByAuthorToolIds(@Param("ids") ids: List, @Param("operator") operator: Long?): List + + /** + * Count published tool by username and toolId + * + * @param authorId Author ID + * @param toolId Tool ID + * @return Number + * @author FatttSnake, fatttsnake@gmail.com + * @since 1.0.0 + */ + fun countPublishedToolByAuthorAndToolId(@Param("authorId") authorId: Long, @Param("toolId") toolId: String): Long } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/oxygen/api/param/tool/ToolFavoriteAddParam.kt b/src/main/kotlin/top/fatweb/oxygen/api/param/tool/ToolFavoriteAddParam.kt index 3a7537c..28b6fc0 100644 --- a/src/main/kotlin/top/fatweb/oxygen/api/param/tool/ToolFavoriteAddParam.kt +++ b/src/main/kotlin/top/fatweb/oxygen/api/param/tool/ToolFavoriteAddParam.kt @@ -2,10 +2,8 @@ package top.fatweb.oxygen.api.param.tool import io.swagger.v3.oas.annotations.media.Schema import jakarta.validation.constraints.NotBlank -import jakarta.validation.constraints.NotNull import jakarta.validation.constraints.Pattern import top.fatweb.oxygen.api.annotation.Trim -import top.fatweb.oxygen.api.entity.tool.ToolBase /** * Add favorite tool parameters @@ -15,15 +13,15 @@ import top.fatweb.oxygen.api.entity.tool.ToolBase */ data class ToolFavoriteAddParam( /** - * Username + * Author ID * * @author FatttSnake, fatttsnake@gmail.com * @since 1.0.0 */ @Trim - @Schema(description = "用户名", required = true) - @field: NotBlank(message = "Username cannot be blank") - var username: String?, + @Schema(description = "作者 ID", required = true) + @field: NotBlank(message = "AuthorId cannot be blank") + var authorId: Long?, /** * Tool ID @@ -38,16 +36,5 @@ data class ToolFavoriteAddParam( regexp = "^[a-zA-Z-_][0-9a-zA-Z-_]{2,19}\$", message = "ToolId can only match '^[a-zA-Z-_][0-9a-zA-Z-_]{2,19}\$'" ) - var toolId: String?, - - /** - * Platform - * - * @author FatttSnake, fatttsnake@gmail.com - * @since 1.0.0 - * @see ToolBase.Platform - */ - @Schema(description = "平台") - @field:NotNull(message = "Platform can not be null") - val platform: ToolBase.Platform? + var toolId: String? ) \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/oxygen/api/param/tool/ToolFavoriteRemoveParam.kt b/src/main/kotlin/top/fatweb/oxygen/api/param/tool/ToolFavoriteRemoveParam.kt index 3ae65c9..17741ee 100644 --- a/src/main/kotlin/top/fatweb/oxygen/api/param/tool/ToolFavoriteRemoveParam.kt +++ b/src/main/kotlin/top/fatweb/oxygen/api/param/tool/ToolFavoriteRemoveParam.kt @@ -2,10 +2,8 @@ package top.fatweb.oxygen.api.param.tool import io.swagger.v3.oas.annotations.media.Schema import jakarta.validation.constraints.NotBlank -import jakarta.validation.constraints.NotNull import jakarta.validation.constraints.Pattern import top.fatweb.oxygen.api.annotation.Trim -import top.fatweb.oxygen.api.entity.tool.ToolBase /** * Remove favorite tool parameters @@ -15,15 +13,15 @@ import top.fatweb.oxygen.api.entity.tool.ToolBase */ data class ToolFavoriteRemoveParam( /** - * Username + * Author ID * * @author FatttSnake, fatttsnake@gmail.com * @since 1.0.0 */ @Trim - @Schema(description = "用户名", required = true) - @field: NotBlank(message = "Username cannot be blank") - var username: String?, + @Schema(description = "作者 ID", required = true) + @field: NotBlank(message = "AuthorId cannot be blank") + var authorId: Long?, /** * Tool ID @@ -39,15 +37,4 @@ data class ToolFavoriteRemoveParam( message = "ToolId can only match '^[a-zA-Z-_][0-9a-zA-Z-_]{2,19}\$'" ) var toolId: String?, - - /** - * Platform - * - * @author FatttSnake, fatttsnake@gmail.com - * @since 1.0.0 - * @see ToolBase.Platform - */ - @Schema(description = "平台") - @field:NotNull(message = "Platform can not be null") - val platform: ToolBase.Platform? ) \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/oxygen/api/service/tool/IStoreService.kt b/src/main/kotlin/top/fatweb/oxygen/api/service/tool/IStoreService.kt index 7718edc..f5a3337 100644 --- a/src/main/kotlin/top/fatweb/oxygen/api/service/tool/IStoreService.kt +++ b/src/main/kotlin/top/fatweb/oxygen/api/service/tool/IStoreService.kt @@ -64,4 +64,17 @@ interface IStoreService : IService { * @see ToolFavoriteRemoveParam */ fun removeFavorite(toolFavoriteRemoveParam: ToolFavoriteRemoveParam) + + /** + * Get favorite tool + * + * @param pageSortParam Page sort parameters + * @return PageVo object + * @author FatttSnake, fatttsnake@gmail.com + * @since 1.0.0 + * @see PageSortParam + * @see PageVo + * @see ToolVo + */ + fun getFavorite(pageSortParam: PageSortParam): PageVo } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/oxygen/api/service/tool/impl/StoreServiceImpl.kt b/src/main/kotlin/top/fatweb/oxygen/api/service/tool/impl/StoreServiceImpl.kt index 5270eff..18fb21a 100644 --- a/src/main/kotlin/top/fatweb/oxygen/api/service/tool/impl/StoreServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/oxygen/api/service/tool/impl/StoreServiceImpl.kt @@ -15,7 +15,6 @@ import top.fatweb.oxygen.api.param.PageSortParam import top.fatweb.oxygen.api.param.tool.ToolFavoriteAddParam import top.fatweb.oxygen.api.param.tool.ToolFavoriteRemoveParam import top.fatweb.oxygen.api.param.tool.ToolStoreGetParam -import top.fatweb.oxygen.api.service.tool.IEditService import top.fatweb.oxygen.api.service.tool.IStoreService import top.fatweb.oxygen.api.service.tool.IToolFavoriteService import top.fatweb.oxygen.api.util.WebUtil @@ -34,7 +33,6 @@ import top.fatweb.oxygen.api.vo.tool.ToolVo */ @Service class StoreServiceImpl( - private val editService: IEditService, private val toolFavoriteService: IToolFavoriteService ) : ServiceImpl(), IStoreService { override fun getPage(toolStoreGetParam: ToolStoreGetParam): PageVo { @@ -65,30 +63,33 @@ class StoreServiceImpl( @Transactional override fun addFavorite(toolFavoriteAddParam: ToolFavoriteAddParam) { + if (toolFavoriteAddParam.authorId == WebUtil.getLoginUserId()) { + throw NoRecordFoundException() + } + if (toolFavoriteService.exists( KtQueryWrapper(ToolFavorite()) .eq(ToolFavorite::userId, WebUtil.getLoginUserId()) - .eq(ToolFavorite::username, toolFavoriteAddParam.username) + .eq(ToolFavorite::authorId, toolFavoriteAddParam.authorId) .eq(ToolFavorite::toolId, toolFavoriteAddParam.toolId) - .eq(ToolFavorite::platform, toolFavoriteAddParam.platform) ) ) { throw RecordAlreadyExists() } - editService.detail( - toolFavoriteAddParam.username!!, - toolFavoriteAddParam.toolId!!, - "latest", - toolFavoriteAddParam.platform!! - ) + if (baseMapper.countPublishedToolByAuthorAndToolId( + toolFavoriteAddParam.authorId!!, + toolFavoriteAddParam.toolId!! + ) <= 0 + ) { + throw NoRecordFoundException() + } toolFavoriteService.save( ToolFavorite().apply { userId = WebUtil.getLoginUserId() - username = toolFavoriteAddParam.username + authorId = toolFavoriteAddParam.authorId toolId = toolFavoriteAddParam.toolId - platform = toolFavoriteAddParam.platform } ) } @@ -98,12 +99,32 @@ class StoreServiceImpl( if (!toolFavoriteService.remove( KtQueryWrapper(ToolFavorite()) .eq(ToolFavorite::userId, WebUtil.getLoginUserId()) - .eq(ToolFavorite::username, toolFavoriteRemoveParam.username) + .eq(ToolFavorite::authorId, toolFavoriteRemoveParam.authorId) .eq(ToolFavorite::toolId, toolFavoriteRemoveParam.toolId) - .eq(ToolFavorite::platform, toolFavoriteRemoveParam.platform) ) ) { throw NoRecordFoundException() } } + + override fun getFavorite(pageSortParam: PageSortParam): PageVo { + val toolFavoritePage = Page(pageSortParam.currentPage, 20) + + val toolFavoriteIPage = toolFavoriteService.page( + toolFavoritePage, + KtQueryWrapper(ToolFavorite()).eq(ToolFavorite::userId, WebUtil.getLoginUserId()) + ) + + val toolPage = Page(toolFavoriteIPage.current, toolFavoriteIPage.size, toolFavoriteIPage.total) + if (toolFavoriteIPage.total > 0) { + toolPage.setRecords( + baseMapper.selectListByAuthorToolIds( + toolFavoriteIPage.records.map { "${it.authorId}:${it.toolId}" }, + WebUtil.getLoginUserId() + ) + ) + } + + return ToolConverter.toolPageToToolPageVo(toolPage) + } } \ No newline at end of file diff --git a/src/main/resources/db/migration/master/V1_0_0_240426__Add_table_'t_b_tool_favorite'.sql b/src/main/resources/db/migration/master/V1_0_0_240426__Add_table_'t_b_tool_favorite'.sql index 20c051e..6eded91 100644 --- a/src/main/resources/db/migration/master/V1_0_0_240426__Add_table_'t_b_tool_favorite'.sql +++ b/src/main/resources/db/migration/master/V1_0_0_240426__Add_table_'t_b_tool_favorite'.sql @@ -2,12 +2,11 @@ drop table if exists t_b_tool_favorite; create table if not exists t_b_tool_favorite ( - id bigint not null primary key, - user_id bigint not null comment '用户 ID', - username varchar(20) not null comment '用户名', - tool_id varchar(50) not null comment '工具 ID', - platform varchar(20) not null comment '平台', - deleted bigint not null default 0, - version int not null default 0, - constraint t_b_tool_favorite_unique_user_id_username_tool_id_platform unique (user_id, username, tool_id, platform, deleted) + id bigint not null primary key, + user_id bigint not null comment '用户 ID', + author_id bigint not null comment '作者 ID', + tool_id varchar(50) not null comment '工具 ID', + deleted bigint not null default 0, + version int not null default 0, + constraint t_b_tool_favorite_unique_user_id_username_tool_id_platform unique (user_id, author_id, tool_id, deleted) ) comment '工具-收藏表'; \ No newline at end of file diff --git a/src/main/resources/mapper/tool/EditMapper.xml b/src/main/resources/mapper/tool/EditMapper.xml index 818ca35..b7ad444 100644 --- a/src/main/resources/mapper/tool/EditMapper.xml +++ b/src/main/resources/mapper/tool/EditMapper.xml @@ -95,11 +95,15 @@ - - + + \ No newline at end of file