From 38e0c90ce2d4ead4b3a1056e222c8e403299d16a Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Sat, 27 Apr 2024 16:54:05 +0800 Subject: [PATCH] Refactor(ToolFavorite): Move tool favorite function Move tool favorite function from EditorController to StoreController --- .../api/controller/tool/EditController.kt | 40 +------------ .../api/controller/tool/StoreController.kt | 42 +++++++++++++- .../oxygen/api/service/tool/IEditService.kt | 24 +------- .../oxygen/api/service/tool/IStoreService.kt | 22 +++++++ .../api/service/tool/impl/EditServiceImpl.kt | 51 ++-------------- .../api/service/tool/impl/StoreServiceImpl.kt | 58 ++++++++++++++++++- 6 files changed, 129 insertions(+), 108 deletions(-) diff --git a/src/main/kotlin/top/fatweb/oxygen/api/controller/tool/EditController.kt b/src/main/kotlin/top/fatweb/oxygen/api/controller/tool/EditController.kt index ef150b4..86f8d4c 100644 --- a/src/main/kotlin/top/fatweb/oxygen/api/controller/tool/EditController.kt +++ b/src/main/kotlin/top/fatweb/oxygen/api/controller/tool/EditController.kt @@ -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.tool.ToolBase 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.vo.PageVo import top.fatweb.oxygen.api.vo.tool.ToolCategoryVo @@ -210,40 +212,4 @@ class EditController( fun delete(@PathVariable id: Long): ResponseResult = if (editService.delete(id)) ResponseResult.databaseSuccess(ResponseCode.DATABASE_DELETE_SUCCESS) 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 { - 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 { - editService.removeFavorite(toolFavoriteRemoveParam) - - return ResponseResult.databaseSuccess(ResponseCode.DATABASE_DELETE_SUCCESS) - } } \ No newline at end of file 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 c29e193..935ab4e 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 @@ -2,12 +2,14 @@ package top.fatweb.oxygen.api.controller.tool import io.swagger.v3.oas.annotations.Operation import jakarta.validation.Valid -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.* import top.fatweb.oxygen.api.annotation.BaseController 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.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.IStoreService import top.fatweb.oxygen.api.vo.PageVo @@ -60,4 +62,40 @@ class StoreController( @GetMapping("/{username}") fun get(@PathVariable username: String, @Valid pageSortParam: PageSortParam): ResponseResult> = 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 { + 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 { + storeService.removeFavorite(toolFavoriteRemoveParam) + + return ResponseResult.databaseSuccess(ResponseCode.DATABASE_DELETE_SUCCESS) + } } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/oxygen/api/service/tool/IEditService.kt b/src/main/kotlin/top/fatweb/oxygen/api/service/tool/IEditService.kt index 8d9ba15..e9b64bc 100644 --- a/src/main/kotlin/top/fatweb/oxygen/api/service/tool/IEditService.kt +++ b/src/main/kotlin/top/fatweb/oxygen/api/service/tool/IEditService.kt @@ -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.ToolBase 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.tool.ToolCategoryVo import top.fatweb.oxygen.api.vo.tool.ToolTemplateVo @@ -154,24 +156,4 @@ interface IEditService : IService { * @since 1.0.0 */ 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) } \ 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 d5efaa8..7718edc 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 @@ -3,6 +3,8 @@ package top.fatweb.oxygen.api.service.tool import com.baomidou.mybatisplus.extension.service.IService import top.fatweb.oxygen.api.entity.tool.Tool 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.vo.PageVo import top.fatweb.oxygen.api.vo.tool.ToolVo @@ -42,4 +44,24 @@ interface IStoreService : IService { * @see ToolVo */ fun getPage(pageSortParam: PageSortParam, username: String): PageVo + + /*** + * 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) } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/oxygen/api/service/tool/impl/EditServiceImpl.kt b/src/main/kotlin/top/fatweb/oxygen/api/service/tool/impl/EditServiceImpl.kt index d879dbe..74e2208 100644 --- a/src/main/kotlin/top/fatweb/oxygen/api/service/tool/impl/EditServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/oxygen/api/service/tool/impl/EditServiceImpl.kt @@ -14,7 +14,9 @@ import top.fatweb.oxygen.api.entity.tool.* import top.fatweb.oxygen.api.exception.* import top.fatweb.oxygen.api.mapper.tool.EditMapper 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.util.WebUtil import top.fatweb.oxygen.api.vo.PageVo @@ -37,8 +39,7 @@ class EditServiceImpl( private val toolTemplateService: IToolTemplateService, private val toolCategoryService: IToolCategoryService, private val toolDataService: IToolDataService, - private val rToolCategoryService: IRToolCategoryService, - private val toolFavoriteService: IToolFavoriteService + private val rToolCategoryService: IRToolCategoryService ) : ServiceImpl(), IEditService { override fun getTemplate(platform: ToolBase.Platform): List = toolTemplateService.list( @@ -270,48 +271,4 @@ class EditServiceImpl( rToolCategoryService.remove(KtQueryWrapper(RToolCategory()).eq(RToolCategory::toolId, tool.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() - } - } } \ 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 21e5413..5270eff 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 @@ -1,14 +1,23 @@ 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.service.impl.ServiceImpl import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional import top.fatweb.oxygen.api.converter.tool.ToolConverter 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.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 import top.fatweb.oxygen.api.vo.PageVo import top.fatweb.oxygen.api.vo.tool.ToolVo @@ -24,7 +33,10 @@ import top.fatweb.oxygen.api.vo.tool.ToolVo * @see IStoreService */ @Service -class StoreServiceImpl : ServiceImpl(), IStoreService { +class StoreServiceImpl( + private val editService: IEditService, + private val toolFavoriteService: IToolFavoriteService +) : ServiceImpl(), IStoreService { override fun getPage(toolStoreGetParam: ToolStoreGetParam): PageVo { val toolIdsPage = Page(toolStoreGetParam.currentPage, 20) toolIdsPage.setOptimizeCountSql(false) @@ -50,4 +62,48 @@ class StoreServiceImpl : ServiceImpl(), IStoreService { 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() + } + } } \ No newline at end of file