feat: 小说章节删除接口

This commit is contained in:
xiongxiaoyang 2023-04-25 17:43:33 +08:00
parent 1f2d8dc49a
commit d6df259e94
6 changed files with 84 additions and 8 deletions

View File

@ -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<Void> deleteBookChapter(
@Parameter(description = "章节ID") @PathVariable("chapterId") Long chapterId) {
return bookService.deleteBookChapter(chapterId);
}
/**
* 小说章节发布列表查询接口
*/

View File

@ -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) {
// 调用此方法自动清除小说章节信息的缓存
}
}

View File

@ -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) {
// 调用此方法自动清除小说内容信息的缓存
}
}

View File

@ -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) {
// 调用此方法自动清除小说信息的缓存
}

View File

@ -189,4 +189,12 @@ public interface BookService {
*/
RestResp<PageRespDto<UserCommentRespDto>> listComments(Long userId, PageReqDto pageReqDto);
/**
* 小说章节删除
*
* @param chapterId 章节ID
* @return void
*/
RestResp<Void> deleteBookChapter(Long chapterId);
}

View File

@ -437,6 +437,55 @@ public class BookServiceImpl implements BookService {
Collections.emptyList()));
}
@Transactional(rollbackFor = Exception.class)
@Override
public RestResp<Void> deleteBookChapter(Long chapterId) {
// 1.查询章节信息
BookChapterRespDto chapter = bookChapterCacheManager.getChapter(chapterId);
// 2.查询小说信息
BookInfoRespDto bookInfo = bookInfoCacheManager.getBookInfo(chapter.getBookId());
// 3.删除章节信息
bookChapterMapper.deleteById(chapterId);
// 4.删除章节内容
QueryWrapper<BookContent> 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<BookChapter> 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<BookContentAboutRespDto> getBookContentAbout(Long chapterId) {
log.debug("userId:{}", UserHolder.getUserId());