Skip to content

Commit

Permalink
feat: 小说章节查询&更新接口
Browse files Browse the repository at this point in the history
  • Loading branch information
201206030 committed Apr 25, 2023
1 parent d6df259 commit a31edb0
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import io.github.xxyopen.novel.dto.req.AuthorRegisterReqDto;
import io.github.xxyopen.novel.dto.req.BookAddReqDto;
import io.github.xxyopen.novel.dto.req.ChapterAddReqDto;
import io.github.xxyopen.novel.dto.req.ChapterUpdateReqDto;
import io.github.xxyopen.novel.dto.resp.BookChapterRespDto;
import io.github.xxyopen.novel.dto.resp.BookInfoRespDto;
import io.github.xxyopen.novel.dto.resp.ChapterContentRespDto;
import io.github.xxyopen.novel.service.AuthorService;
import io.github.xxyopen.novel.service.BookService;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -98,6 +100,27 @@ public RestResp<Void> deleteBookChapter(
return bookService.deleteBookChapter(chapterId);
}

/**
* 小说章节查询接口
*/
@Operation(summary = "小说章节查询接口")
@GetMapping("book/chapter/{chapterId}")
public RestResp<ChapterContentRespDto> getBookChapter(
@Parameter(description = "章节ID") @PathVariable("chapterId") Long chapterId) {
return bookService.getBookChapter(chapterId);
}

/**
* 小说章节更新接口
*/
@Operation(summary = "小说章节更新接口")
@PutMapping("book/chapter/{chapterId}")
public RestResp<Void> updateBookChapter(
@Parameter(description = "章节ID") @PathVariable("chapterId") Long chapterId,
@Valid @RequestBody ChapterUpdateReqDto dto) {
return bookService.updateBookChapter(chapterId, dto);
}

/**
* 小说章节发布列表查询接口
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.xxyopen.novel.dto.req;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

/**
* 章节发布 请求DTO
*
* @author xiongxiaoyang
* @date 2022/5/23
*/
@Data
public class ChapterUpdateReqDto {

/**
* 章节名
*/
@NotBlank
@Schema(description = "章节名", required = true)
private String chapterName;

/**
* 章节内容
*/
@Schema(description = "章节内容", required = true)
@NotBlank
@Length(min = 50)
private String chapterContent;

/**
* 是否收费;1-收费 0-免费
*/
@Schema(description = "是否收费;1-收费 0-免费", required = true)
@NotNull
private Integer isVip;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.xxyopen.novel.dto.resp;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Data;

/**
* 小说内容 响应DTO
*
* @author xiongxiaoyang
* @date 2022/5/15
*/
@Data
@Builder
public class ChapterContentRespDto {

/**
* 章节标题
*/
@Schema(description = "章节名")
private String chapterName;

/**
* 章节内容
*/
@Schema(description = "章节内容")
private String chapterContent;

/**
* 是否收费;1-收费 0-免费
*/
@Schema(description = "是否收费;1-收费 0-免费")
private Integer isVip;

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public BookChapterRespDto getChapter(Long chapterId) {
.chapterName(bookChapter.getChapterName())
.chapterWordCount(bookChapter.getWordCount())
.chapterUpdateTime(bookChapter.getUpdateTime())
.isVip(bookChapter.getIsVip())
.build();
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/github/xxyopen/novel/service/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.dto.req.BookAddReqDto;
import io.github.xxyopen.novel.dto.req.ChapterAddReqDto;
import io.github.xxyopen.novel.dto.req.ChapterUpdateReqDto;
import io.github.xxyopen.novel.dto.req.UserCommentReqDto;
import io.github.xxyopen.novel.dto.resp.*;

Expand Down Expand Up @@ -197,4 +198,20 @@ public interface BookService {
*/
RestResp<Void> deleteBookChapter(Long chapterId);

/**
* 小说章节查询
*
* @param chapterId 章节ID
* @return 章节内容
*/
RestResp<ChapterContentRespDto> getBookChapter(Long chapterId);

/**
* 小说章节更新
*
* @param chapterId 章节ID
* @param dto 更新内容
* @return void
*/
RestResp<Void> updateBookChapter(Long chapterId, ChapterUpdateReqDto dto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.github.xxyopen.novel.dto.AuthorInfoDto;
import io.github.xxyopen.novel.dto.req.BookAddReqDto;
import io.github.xxyopen.novel.dto.req.ChapterAddReqDto;
import io.github.xxyopen.novel.dto.req.ChapterUpdateReqDto;
import io.github.xxyopen.novel.dto.req.UserCommentReqDto;
import io.github.xxyopen.novel.dto.resp.*;
import io.github.xxyopen.novel.manager.cache.*;
Expand Down Expand Up @@ -486,6 +487,63 @@ public RestResp<Void> deleteBookChapter(Long chapterId) {
return RestResp.ok();
}

@Override
public RestResp<ChapterContentRespDto> getBookChapter(Long chapterId) {
BookChapterRespDto chapter = bookChapterCacheManager.getChapter(chapterId);
String bookContent = bookContentCacheManager.getBookContent(chapterId);
return RestResp.ok(
ChapterContentRespDto.builder()
.chapterName(chapter.getChapterName())
.chapterContent(bookContent)
.isVip(chapter.getIsVip())
.build());
}

@Transactional
@Override
public RestResp<Void> updateBookChapter(Long chapterId, ChapterUpdateReqDto dto) {
// 1.查询章节信息
BookChapterRespDto chapter = bookChapterCacheManager.getChapter(chapterId);
// 2.查询小说信息
BookInfoRespDto bookInfo = bookInfoCacheManager.getBookInfo(chapter.getBookId());
// 3.更新章节信息
BookChapter newChapter = new BookChapter();
newChapter.setId(chapterId);
newChapter.setChapterName(dto.getChapterName());
newChapter.setWordCount(dto.getChapterContent().length());
newChapter.setIsVip(dto.getIsVip());
newChapter.setUpdateTime(LocalDateTime.now());
bookChapterMapper.updateById(newChapter);
// 4.更新章节内容
BookContent newContent = new BookContent();
newContent.setContent(dto.getChapterContent());
newContent.setUpdateTime(LocalDateTime.now());
QueryWrapper<BookContent> bookContentQueryWrapper = new QueryWrapper<>();
bookContentQueryWrapper.eq(DatabaseConsts.BookContentTable.COLUMN_CHAPTER_ID, chapterId);
bookContentMapper.update(newContent, bookContentQueryWrapper);
// 5.更新小说信息
BookInfo newBookInfo = new BookInfo();
newBookInfo.setId(chapter.getBookId());
newBookInfo.setUpdateTime(LocalDateTime.now());
newBookInfo.setWordCount(
bookInfo.getWordCount() - chapter.getChapterWordCount() + dto.getChapterContent().length());
if (Objects.equals(bookInfo.getLastChapterId(), chapterId)) {
// 更新最新章节信息
newBookInfo.setLastChapterName(dto.getChapterName());
newBookInfo.setLastChapterUpdateTime(LocalDateTime.now());
}
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());
Expand Down

0 comments on commit a31edb0

Please sign in to comment.