From d6df259e945d0215d910969608b9d5f8b2d80af8 Mon Sep 17 00:00:00 2001 From: xiongxiaoyang <1179705413@qq.com> Date: Tue, 25 Apr 2023 17:43:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B0=8F=E8=AF=B4=E7=AB=A0=E8=8A=82?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/author/AuthorController.java | 17 ++++--- .../cache/BookChapterCacheManager.java | 6 +++ .../cache/BookContentCacheManager.java | 7 +++ .../manager/cache/BookInfoCacheManager.java | 5 +- .../xxyopen/novel/service/BookService.java | 8 +++ .../novel/service/impl/BookServiceImpl.java | 49 +++++++++++++++++++ 6 files changed, 84 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/github/xxyopen/novel/controller/author/AuthorController.java b/src/main/java/io/github/xxyopen/novel/controller/author/AuthorController.java index 4c8be34..167384f 100644 --- a/src/main/java/io/github/xxyopen/novel/controller/author/AuthorController.java +++ b/src/main/java/io/github/xxyopen/novel/controller/author/AuthorController.java @@ -20,12 +20,7 @@ import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springdoc.core.annotations.ParameterObject; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; /** * 作家后台-作家模块 API 控制器 @@ -93,6 +88,16 @@ public class AuthorController { return bookService.saveBookChapter(dto); } + /** + * 小说章节删除接口 + */ + @Operation(summary = "小说章节删除接口") + @DeleteMapping("book/chapter/{chapterId}") + public RestResp deleteBookChapter( + @Parameter(description = "章节ID") @PathVariable("chapterId") Long chapterId) { + return bookService.deleteBookChapter(chapterId); + } + /** * 小说章节发布列表查询接口 */ diff --git a/src/main/java/io/github/xxyopen/novel/manager/cache/BookChapterCacheManager.java b/src/main/java/io/github/xxyopen/novel/manager/cache/BookChapterCacheManager.java index 83aa618..9c21008 100644 --- a/src/main/java/io/github/xxyopen/novel/manager/cache/BookChapterCacheManager.java +++ b/src/main/java/io/github/xxyopen/novel/manager/cache/BookChapterCacheManager.java @@ -5,6 +5,7 @@ import io.github.xxyopen.novel.dao.entity.BookChapter; import io.github.xxyopen.novel.dao.mapper.BookChapterMapper; import io.github.xxyopen.novel.dto.resp.BookChapterRespDto; import lombok.RequiredArgsConstructor; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; @@ -37,5 +38,10 @@ public class BookChapterCacheManager { .build(); } + @CacheEvict(cacheManager = CacheConsts.CAFFEINE_CACHE_MANAGER, + value = CacheConsts.BOOK_CHAPTER_CACHE_NAME) + public void evictBookChapterCache(Long chapterId) { + // 调用此方法自动清除小说章节信息的缓存 + } } diff --git a/src/main/java/io/github/xxyopen/novel/manager/cache/BookContentCacheManager.java b/src/main/java/io/github/xxyopen/novel/manager/cache/BookContentCacheManager.java index c250ff3..de41fb3 100644 --- a/src/main/java/io/github/xxyopen/novel/manager/cache/BookContentCacheManager.java +++ b/src/main/java/io/github/xxyopen/novel/manager/cache/BookContentCacheManager.java @@ -6,6 +6,7 @@ import io.github.xxyopen.novel.core.constant.DatabaseConsts; import io.github.xxyopen.novel.dao.entity.BookContent; import io.github.xxyopen.novel.dao.mapper.BookContentMapper; import lombok.RequiredArgsConstructor; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; @@ -34,5 +35,11 @@ public class BookContentCacheManager { return bookContent.getContent(); } + @CacheEvict(cacheManager = CacheConsts.REDIS_CACHE_MANAGER, + value = CacheConsts.BOOK_CONTENT_CACHE_NAME) + public void evictBookContentCache(Long chapterId) { + // 调用此方法自动清除小说内容信息的缓存 + } + } diff --git a/src/main/java/io/github/xxyopen/novel/manager/cache/BookInfoCacheManager.java b/src/main/java/io/github/xxyopen/novel/manager/cache/BookInfoCacheManager.java index 107453d..94a7db1 100644 --- a/src/main/java/io/github/xxyopen/novel/manager/cache/BookInfoCacheManager.java +++ b/src/main/java/io/github/xxyopen/novel/manager/cache/BookInfoCacheManager.java @@ -8,13 +8,14 @@ import io.github.xxyopen.novel.dao.entity.BookInfo; import io.github.xxyopen.novel.dao.mapper.BookChapterMapper; import io.github.xxyopen.novel.dao.mapper.BookInfoMapper; import io.github.xxyopen.novel.dto.resp.BookInfoRespDto; -import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; +import java.util.List; + /** * 小说信息 缓存管理类 * @@ -74,7 +75,7 @@ public class BookInfoCacheManager { @CacheEvict(cacheManager = CacheConsts.CAFFEINE_CACHE_MANAGER, value = CacheConsts.BOOK_INFO_CACHE_NAME) - public void evictBookInfoCache(Long ignoredId) { + public void evictBookInfoCache(Long bookId) { // 调用此方法自动清除小说信息的缓存 } diff --git a/src/main/java/io/github/xxyopen/novel/service/BookService.java b/src/main/java/io/github/xxyopen/novel/service/BookService.java index efda4af..51f0c21 100644 --- a/src/main/java/io/github/xxyopen/novel/service/BookService.java +++ b/src/main/java/io/github/xxyopen/novel/service/BookService.java @@ -189,4 +189,12 @@ public interface BookService { */ RestResp> listComments(Long userId, PageReqDto pageReqDto); + /** + * 小说章节删除 + * + * @param chapterId 章节ID + * @return void + */ + RestResp deleteBookChapter(Long chapterId); + } diff --git a/src/main/java/io/github/xxyopen/novel/service/impl/BookServiceImpl.java b/src/main/java/io/github/xxyopen/novel/service/impl/BookServiceImpl.java index 7fdd0e8..ca60c00 100644 --- a/src/main/java/io/github/xxyopen/novel/service/impl/BookServiceImpl.java +++ b/src/main/java/io/github/xxyopen/novel/service/impl/BookServiceImpl.java @@ -437,6 +437,55 @@ public class BookServiceImpl implements BookService { Collections.emptyList())); } + @Transactional(rollbackFor = Exception.class) + @Override + public RestResp deleteBookChapter(Long chapterId) { + // 1.查询章节信息 + BookChapterRespDto chapter = bookChapterCacheManager.getChapter(chapterId); + // 2.查询小说信息 + BookInfoRespDto bookInfo = bookInfoCacheManager.getBookInfo(chapter.getBookId()); + // 3.删除章节信息 + bookChapterMapper.deleteById(chapterId); + // 4.删除章节内容 + QueryWrapper bookContentQueryWrapper = new QueryWrapper<>(); + bookContentQueryWrapper.eq(DatabaseConsts.BookContentTable.COLUMN_CHAPTER_ID, chapterId); + bookContentMapper.delete(bookContentQueryWrapper); + // 5.更新小说信息 + BookInfo newBookInfo = new BookInfo(); + newBookInfo.setId(chapter.getBookId()); + newBookInfo.setUpdateTime(LocalDateTime.now()); + newBookInfo.setWordCount(bookInfo.getWordCount() - chapter.getChapterWordCount()); + if (Objects.equals(bookInfo.getLastChapterId(), chapterId)) { + // 设置最新章节信息 + QueryWrapper bookChapterQueryWrapper = new QueryWrapper<>(); + bookChapterQueryWrapper.eq(DatabaseConsts.BookChapterTable.COLUMN_BOOK_ID, chapter.getBookId()) + .orderByDesc(DatabaseConsts.BookChapterTable.COLUMN_CHAPTER_NUM) + .last(DatabaseConsts.SqlEnum.LIMIT_1.getSql()); + BookChapter bookChapter = bookChapterMapper.selectOne(bookChapterQueryWrapper); + Long lastChapterId = 0L; + String lastChapterName = ""; + LocalDateTime lastChapterUpdateTime = null; + if (Objects.nonNull(bookChapter)) { + lastChapterId = bookChapter.getId(); + lastChapterName = bookChapter.getChapterName(); + lastChapterUpdateTime = bookChapter.getUpdateTime(); + } + newBookInfo.setLastChapterId(lastChapterId); + newBookInfo.setLastChapterName(lastChapterName); + newBookInfo.setLastChapterUpdateTime(lastChapterUpdateTime); + } + bookInfoMapper.updateById(newBookInfo); + // 6.清理章节信息缓存 + bookChapterCacheManager.evictBookChapterCache(chapterId); + // 7.清理章节内容缓存 + bookContentCacheManager.evictBookContentCache(chapterId); + // 8.清理小说信息缓存 + bookInfoCacheManager.evictBookInfoCache(chapter.getBookId()); + // 9.发送小说信息更新的 MQ 消息 + amqpMsgManager.sendBookChangeMsg(chapter.getBookId()); + return RestResp.ok(); + } + @Override public RestResp getBookContentAbout(Long chapterId) { log.debug("userId:{}", UserHolder.getUserId());