Refactor(ToolFavorite): Move tool favorite function

Move tool favorite function from EditorController to StoreController
This commit is contained in:
2024-04-27 16:54:05 +08:00
parent f5b6687d97
commit 38e0c90ce2
6 changed files with 129 additions and 108 deletions

View File

@@ -9,7 +9,9 @@ import top.fatweb.oxygen.api.entity.common.ResponseCode
import top.fatweb.oxygen.api.entity.common.ResponseResult import top.fatweb.oxygen.api.entity.common.ResponseResult
import top.fatweb.oxygen.api.entity.tool.ToolBase import top.fatweb.oxygen.api.entity.tool.ToolBase
import top.fatweb.oxygen.api.param.PageSortParam import top.fatweb.oxygen.api.param.PageSortParam
import top.fatweb.oxygen.api.param.tool.* import top.fatweb.oxygen.api.param.tool.ToolCreateParam
import top.fatweb.oxygen.api.param.tool.ToolUpdateParam
import top.fatweb.oxygen.api.param.tool.ToolUpgradeParam
import top.fatweb.oxygen.api.service.tool.IEditService import top.fatweb.oxygen.api.service.tool.IEditService
import top.fatweb.oxygen.api.vo.PageVo import top.fatweb.oxygen.api.vo.PageVo
import top.fatweb.oxygen.api.vo.tool.ToolCategoryVo import top.fatweb.oxygen.api.vo.tool.ToolCategoryVo
@@ -210,40 +212,4 @@ class EditController(
fun delete(@PathVariable id: Long): ResponseResult<Nothing> = fun delete(@PathVariable id: Long): ResponseResult<Nothing> =
if (editService.delete(id)) ResponseResult.databaseSuccess(ResponseCode.DATABASE_DELETE_SUCCESS) if (editService.delete(id)) ResponseResult.databaseSuccess(ResponseCode.DATABASE_DELETE_SUCCESS)
else ResponseResult.databaseFail(ResponseCode.DATABASE_DELETE_FAILED) else ResponseResult.databaseFail(ResponseCode.DATABASE_DELETE_FAILED)
/**
* Add favorite tool
*
* @param toolFavoriteAddParam Add favorite tool parameters
* @return Response object
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
* @see ToolFavoriteAddParam
* @see ResponseResult
*/
@Operation(summary = "收藏工具")
@PostMapping("/favorite")
fun addFavorite(@RequestBody toolFavoriteAddParam: ToolFavoriteAddParam): ResponseResult<Nothing> {
editService.addFavorite(toolFavoriteAddParam)
return ResponseResult.databaseSuccess(ResponseCode.DATABASE_INSERT_SUCCESS)
}
/**
* Remove favorite tool
*
* @param toolFavoriteRemoveParam Remove favorite tool parameters
* @return Response object
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
* @see ToolFavoriteRemoveParam
* @see ResponseResult
*/
@Operation(summary = "收藏工具")
@DeleteMapping("/favorite")
fun addFavorite(@RequestBody toolFavoriteRemoveParam: ToolFavoriteRemoveParam): ResponseResult<Nothing> {
editService.removeFavorite(toolFavoriteRemoveParam)
return ResponseResult.databaseSuccess(ResponseCode.DATABASE_DELETE_SUCCESS)
}
} }

View File

@@ -2,12 +2,14 @@ package top.fatweb.oxygen.api.controller.tool
import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.Operation
import jakarta.validation.Valid import jakarta.validation.Valid
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.*
import org.springframework.web.bind.annotation.PathVariable
import top.fatweb.oxygen.api.annotation.BaseController import top.fatweb.oxygen.api.annotation.BaseController
import top.fatweb.oxygen.api.annotation.Trim import top.fatweb.oxygen.api.annotation.Trim
import top.fatweb.oxygen.api.entity.common.ResponseCode
import top.fatweb.oxygen.api.entity.common.ResponseResult import top.fatweb.oxygen.api.entity.common.ResponseResult
import top.fatweb.oxygen.api.param.PageSortParam 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.param.tool.ToolStoreGetParam
import top.fatweb.oxygen.api.service.tool.IStoreService import top.fatweb.oxygen.api.service.tool.IStoreService
import top.fatweb.oxygen.api.vo.PageVo import top.fatweb.oxygen.api.vo.PageVo
@@ -60,4 +62,40 @@ class StoreController(
@GetMapping("/{username}") @GetMapping("/{username}")
fun get(@PathVariable username: String, @Valid pageSortParam: PageSortParam): ResponseResult<PageVo<ToolVo>> = fun get(@PathVariable username: String, @Valid pageSortParam: PageSortParam): ResponseResult<PageVo<ToolVo>> =
ResponseResult.databaseSuccess(data = storeService.getPage(pageSortParam, username.trim())) ResponseResult.databaseSuccess(data = storeService.getPage(pageSortParam, username.trim()))
/**
* Add favorite tool
*
* @param toolFavoriteAddParam Add favorite tool parameters
* @return Response object
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
* @see ToolFavoriteAddParam
* @see ResponseResult
*/
@Operation(summary = "收藏工具")
@PostMapping("/favorite")
fun addFavorite(@RequestBody toolFavoriteAddParam: ToolFavoriteAddParam): ResponseResult<Nothing> {
storeService.addFavorite(toolFavoriteAddParam)
return ResponseResult.databaseSuccess(ResponseCode.DATABASE_INSERT_SUCCESS)
}
/**
* Remove favorite tool
*
* @param toolFavoriteRemoveParam Remove favorite tool parameters
* @return Response object
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
* @see ToolFavoriteRemoveParam
* @see ResponseResult
*/
@Operation(summary = "收藏工具")
@DeleteMapping("/favorite")
fun addFavorite(@RequestBody toolFavoriteRemoveParam: ToolFavoriteRemoveParam): ResponseResult<Nothing> {
storeService.removeFavorite(toolFavoriteRemoveParam)
return ResponseResult.databaseSuccess(ResponseCode.DATABASE_DELETE_SUCCESS)
}
} }

View File

@@ -4,7 +4,9 @@ import com.baomidou.mybatisplus.extension.service.IService
import top.fatweb.oxygen.api.entity.tool.Tool import top.fatweb.oxygen.api.entity.tool.Tool
import top.fatweb.oxygen.api.entity.tool.ToolBase import top.fatweb.oxygen.api.entity.tool.ToolBase
import top.fatweb.oxygen.api.param.PageSortParam import top.fatweb.oxygen.api.param.PageSortParam
import top.fatweb.oxygen.api.param.tool.* import top.fatweb.oxygen.api.param.tool.ToolCreateParam
import top.fatweb.oxygen.api.param.tool.ToolUpdateParam
import top.fatweb.oxygen.api.param.tool.ToolUpgradeParam
import top.fatweb.oxygen.api.vo.PageVo import top.fatweb.oxygen.api.vo.PageVo
import top.fatweb.oxygen.api.vo.tool.ToolCategoryVo import top.fatweb.oxygen.api.vo.tool.ToolCategoryVo
import top.fatweb.oxygen.api.vo.tool.ToolTemplateVo import top.fatweb.oxygen.api.vo.tool.ToolTemplateVo
@@ -154,24 +156,4 @@ interface IEditService : IService<Tool> {
* @since 1.0.0 * @since 1.0.0
*/ */
fun delete(id: Long): Boolean fun delete(id: Long): Boolean
/***
* Add favorite
*
* @param toolFavoriteAddParam Add favorite tool parameters
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
* @see ToolFavoriteAddParam
*/
fun addFavorite(toolFavoriteAddParam: ToolFavoriteAddParam)
/***
* Remove favorite tool
*
* @param toolFavoriteRemoveParam Remove favorite tool parameters
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
* @see ToolFavoriteRemoveParam
*/
fun removeFavorite(toolFavoriteRemoveParam: ToolFavoriteRemoveParam)
} }

View File

@@ -3,6 +3,8 @@ package top.fatweb.oxygen.api.service.tool
import com.baomidou.mybatisplus.extension.service.IService import com.baomidou.mybatisplus.extension.service.IService
import top.fatweb.oxygen.api.entity.tool.Tool import top.fatweb.oxygen.api.entity.tool.Tool
import top.fatweb.oxygen.api.param.PageSortParam 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.param.tool.ToolStoreGetParam
import top.fatweb.oxygen.api.vo.PageVo import top.fatweb.oxygen.api.vo.PageVo
import top.fatweb.oxygen.api.vo.tool.ToolVo import top.fatweb.oxygen.api.vo.tool.ToolVo
@@ -42,4 +44,24 @@ interface IStoreService : IService<Tool> {
* @see ToolVo * @see ToolVo
*/ */
fun getPage(pageSortParam: PageSortParam, username: String): PageVo<ToolVo> fun getPage(pageSortParam: PageSortParam, username: String): PageVo<ToolVo>
/***
* Add favorite
*
* @param toolFavoriteAddParam Add favorite tool parameters
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
* @see ToolFavoriteAddParam
*/
fun addFavorite(toolFavoriteAddParam: ToolFavoriteAddParam)
/***
* Remove favorite tool
*
* @param toolFavoriteRemoveParam Remove favorite tool parameters
* @author FatttSnake, fatttsnake@gmail.com
* @since 1.0.0
* @see ToolFavoriteRemoveParam
*/
fun removeFavorite(toolFavoriteRemoveParam: ToolFavoriteRemoveParam)
} }

View File

@@ -14,7 +14,9 @@ import top.fatweb.oxygen.api.entity.tool.*
import top.fatweb.oxygen.api.exception.* import top.fatweb.oxygen.api.exception.*
import top.fatweb.oxygen.api.mapper.tool.EditMapper import top.fatweb.oxygen.api.mapper.tool.EditMapper
import top.fatweb.oxygen.api.param.PageSortParam import top.fatweb.oxygen.api.param.PageSortParam
import top.fatweb.oxygen.api.param.tool.* import top.fatweb.oxygen.api.param.tool.ToolCreateParam
import top.fatweb.oxygen.api.param.tool.ToolUpdateParam
import top.fatweb.oxygen.api.param.tool.ToolUpgradeParam
import top.fatweb.oxygen.api.service.tool.* import top.fatweb.oxygen.api.service.tool.*
import top.fatweb.oxygen.api.util.WebUtil import top.fatweb.oxygen.api.util.WebUtil
import top.fatweb.oxygen.api.vo.PageVo import top.fatweb.oxygen.api.vo.PageVo
@@ -37,8 +39,7 @@ class EditServiceImpl(
private val toolTemplateService: IToolTemplateService, private val toolTemplateService: IToolTemplateService,
private val toolCategoryService: IToolCategoryService, private val toolCategoryService: IToolCategoryService,
private val toolDataService: IToolDataService, private val toolDataService: IToolDataService,
private val rToolCategoryService: IRToolCategoryService, private val rToolCategoryService: IRToolCategoryService
private val toolFavoriteService: IToolFavoriteService
) : ServiceImpl<EditMapper, Tool>(), IEditService { ) : ServiceImpl<EditMapper, Tool>(), IEditService {
override fun getTemplate(platform: ToolBase.Platform): List<ToolTemplateVo> = override fun getTemplate(platform: ToolBase.Platform): List<ToolTemplateVo> =
toolTemplateService.list( toolTemplateService.list(
@@ -270,48 +271,4 @@ class EditServiceImpl(
rToolCategoryService.remove(KtQueryWrapper(RToolCategory()).eq(RToolCategory::toolId, tool.id)) rToolCategoryService.remove(KtQueryWrapper(RToolCategory()).eq(RToolCategory::toolId, tool.id))
return this.removeById(id) return this.removeById(id)
} }
@Transactional
override fun addFavorite(toolFavoriteAddParam: ToolFavoriteAddParam) {
if (toolFavoriteService.exists(
KtQueryWrapper(ToolFavorite())
.eq(ToolFavorite::userId, WebUtil.getLoginUserId())
.eq(ToolFavorite::username, toolFavoriteAddParam.username)
.eq(ToolFavorite::toolId, toolFavoriteAddParam.toolId)
.eq(ToolFavorite::platform, toolFavoriteAddParam.platform)
)
) {
throw RecordAlreadyExists()
}
this.detail(
toolFavoriteAddParam.username!!,
toolFavoriteAddParam.toolId!!,
"latest",
toolFavoriteAddParam.platform!!
)
toolFavoriteService.save(
ToolFavorite().apply {
userId = WebUtil.getLoginUserId()
username = toolFavoriteAddParam.username
toolId = toolFavoriteAddParam.toolId
platform = toolFavoriteAddParam.platform
}
)
}
@Transactional
override fun removeFavorite(toolFavoriteRemoveParam: ToolFavoriteRemoveParam) {
if (!toolFavoriteService.remove(
KtQueryWrapper(ToolFavorite())
.eq(ToolFavorite::userId, WebUtil.getLoginUserId())
.eq(ToolFavorite::username, toolFavoriteRemoveParam.username)
.eq(ToolFavorite::toolId, toolFavoriteRemoveParam.toolId)
.eq(ToolFavorite::platform, toolFavoriteRemoveParam.platform)
)
) {
throw NoRecordFoundException()
}
}
} }

View File

@@ -1,14 +1,23 @@
package top.fatweb.oxygen.api.service.tool.impl package top.fatweb.oxygen.api.service.tool.impl
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import top.fatweb.oxygen.api.converter.tool.ToolConverter import top.fatweb.oxygen.api.converter.tool.ToolConverter
import top.fatweb.oxygen.api.entity.tool.Tool import top.fatweb.oxygen.api.entity.tool.Tool
import top.fatweb.oxygen.api.entity.tool.ToolFavorite
import top.fatweb.oxygen.api.exception.NoRecordFoundException
import top.fatweb.oxygen.api.exception.RecordAlreadyExists
import top.fatweb.oxygen.api.mapper.tool.StoreMapper import top.fatweb.oxygen.api.mapper.tool.StoreMapper
import top.fatweb.oxygen.api.param.PageSortParam 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.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.IStoreService
import top.fatweb.oxygen.api.service.tool.IToolFavoriteService
import top.fatweb.oxygen.api.util.WebUtil import top.fatweb.oxygen.api.util.WebUtil
import top.fatweb.oxygen.api.vo.PageVo import top.fatweb.oxygen.api.vo.PageVo
import top.fatweb.oxygen.api.vo.tool.ToolVo import top.fatweb.oxygen.api.vo.tool.ToolVo
@@ -24,7 +33,10 @@ import top.fatweb.oxygen.api.vo.tool.ToolVo
* @see IStoreService * @see IStoreService
*/ */
@Service @Service
class StoreServiceImpl : ServiceImpl<StoreMapper, Tool>(), IStoreService { class StoreServiceImpl(
private val editService: IEditService,
private val toolFavoriteService: IToolFavoriteService
) : ServiceImpl<StoreMapper, Tool>(), IStoreService {
override fun getPage(toolStoreGetParam: ToolStoreGetParam): PageVo<ToolVo> { override fun getPage(toolStoreGetParam: ToolStoreGetParam): PageVo<ToolVo> {
val toolIdsPage = Page<Long>(toolStoreGetParam.currentPage, 20) val toolIdsPage = Page<Long>(toolStoreGetParam.currentPage, 20)
toolIdsPage.setOptimizeCountSql(false) toolIdsPage.setOptimizeCountSql(false)
@@ -50,4 +62,48 @@ class StoreServiceImpl : ServiceImpl<StoreMapper, Tool>(), IStoreService {
return ToolConverter.toolPageToToolPageVo(toolPage) return ToolConverter.toolPageToToolPageVo(toolPage)
} }
@Transactional
override fun addFavorite(toolFavoriteAddParam: ToolFavoriteAddParam) {
if (toolFavoriteService.exists(
KtQueryWrapper(ToolFavorite())
.eq(ToolFavorite::userId, WebUtil.getLoginUserId())
.eq(ToolFavorite::username, toolFavoriteAddParam.username)
.eq(ToolFavorite::toolId, toolFavoriteAddParam.toolId)
.eq(ToolFavorite::platform, toolFavoriteAddParam.platform)
)
) {
throw RecordAlreadyExists()
}
editService.detail(
toolFavoriteAddParam.username!!,
toolFavoriteAddParam.toolId!!,
"latest",
toolFavoriteAddParam.platform!!
)
toolFavoriteService.save(
ToolFavorite().apply {
userId = WebUtil.getLoginUserId()
username = toolFavoriteAddParam.username
toolId = toolFavoriteAddParam.toolId
platform = toolFavoriteAddParam.platform
}
)
}
@Transactional
override fun removeFavorite(toolFavoriteRemoveParam: ToolFavoriteRemoveParam) {
if (!toolFavoriteService.remove(
KtQueryWrapper(ToolFavorite())
.eq(ToolFavorite::userId, WebUtil.getLoginUserId())
.eq(ToolFavorite::username, toolFavoriteRemoveParam.username)
.eq(ToolFavorite::toolId, toolFavoriteRemoveParam.toolId)
.eq(ToolFavorite::platform, toolFavoriteRemoveParam.platform)
)
) {
throw NoRecordFoundException()
}
}
} }