Feat(StoreController): Add get favorite tool list api
Add get favorite tool list api to StoreController
This commit is contained in:
@@ -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<PageVo<ToolVo>> =
|
||||
ResponseResult.databaseSuccess(data = storeService.getFavorite(pageSortParam))
|
||||
}
|
||||
@@ -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)"
|
||||
}
|
||||
}
|
||||
@@ -40,17 +40,6 @@ interface StoreMapper : BaseMapper<Tool> {
|
||||
*/
|
||||
fun selectAuthorToolIdPageByUsername(page: IPage<Long>, @Param("username") username: String): IPage<String>
|
||||
|
||||
/**
|
||||
* 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<Long>): List<Tool>
|
||||
|
||||
/**
|
||||
* Select tool in list by Author:Tool_ID
|
||||
*
|
||||
@@ -61,4 +50,15 @@ interface StoreMapper : BaseMapper<Tool> {
|
||||
* @see Tool
|
||||
*/
|
||||
fun selectListByAuthorToolIds(@Param("ids") ids: List<String>, @Param("operator") operator: Long?): List<Tool>
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
@@ -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?
|
||||
)
|
||||
@@ -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?
|
||||
)
|
||||
@@ -64,4 +64,17 @@ interface IStoreService : IService<Tool> {
|
||||
* @see ToolFavoriteRemoveParam
|
||||
*/
|
||||
fun removeFavorite(toolFavoriteRemoveParam: ToolFavoriteRemoveParam)
|
||||
|
||||
/**
|
||||
* Get favorite tool
|
||||
*
|
||||
* @param pageSortParam Page sort parameters
|
||||
* @return PageVo<ToolVo> object
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
* @see PageSortParam
|
||||
* @see PageVo
|
||||
* @see ToolVo
|
||||
*/
|
||||
fun getFavorite(pageSortParam: PageSortParam): PageVo<ToolVo>
|
||||
}
|
||||
@@ -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<StoreMapper, Tool>(), IStoreService {
|
||||
override fun getPage(toolStoreGetParam: ToolStoreGetParam): PageVo<ToolVo> {
|
||||
@@ -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<ToolVo> {
|
||||
val toolFavoritePage = Page<ToolFavorite>(pageSortParam.currentPage, 20)
|
||||
|
||||
val toolFavoriteIPage = toolFavoriteService.page(
|
||||
toolFavoritePage,
|
||||
KtQueryWrapper(ToolFavorite()).eq(ToolFavorite::userId, WebUtil.getLoginUserId())
|
||||
)
|
||||
|
||||
val toolPage = Page<Tool>(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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user