作家专区开发实现

This commit is contained in:
xxy
2020-05-13 19:45:57 +08:00
parent 4878f17de8
commit 401d23871d
225 changed files with 42138 additions and 48 deletions

View File

@ -0,0 +1,121 @@
package com.java2nb.novel.controller;
import com.github.pagehelper.PageInfo;
import com.java2nb.novel.core.bean.ResultBean;
import com.java2nb.novel.core.enums.ResponseStatus;
import com.java2nb.novel.core.utils.BeanUtil;
import com.java2nb.novel.entity.Author;
import com.java2nb.novel.entity.Book;
import com.java2nb.novel.service.AuthorService;
import com.java2nb.novel.service.BookService;
import com.java2nb.novel.service.FriendLinkService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 11797
*/
@RequestMapping("author")
@RestController
@Slf4j
@RequiredArgsConstructor
public class AuthorController extends BaseController{
private final AuthorService authorService;
private final BookService bookService;
/**
* 校验笔名是否存在
* */
@PostMapping("checkPenName")
public ResultBean checkPenName(String penName){
return ResultBean.ok(authorService.checkPenName(penName));
}
/**
* 作家发布小说分页列表查询
* */
@PostMapping("listBookByPage")
public ResultBean listBookByPage(@RequestParam(value = "curr", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int pageSize ,HttpServletRequest request){
return ResultBean.ok(new PageInfo<>(bookService.listBookPageByUserId(getUserDetails(request).getId(),page,pageSize)
));
}
/**
* 发布小说
* */
@PostMapping("addBook")
public ResultBean addBook(Book book,HttpServletRequest request){
//查询作家信息
Author author = authorService.queryAuthor(getUserDetails(request).getId());
//判断作者状态是否正常
if(author.getStatus()==1){
//封禁状态,不能发布小说
return ResultBean.fail(ResponseStatus.AUTHOR_STATUS_FORBIDDEN);
}
//发布小说
bookService.addBook(book,author.getId(),author.getPenName());
return ResultBean.ok();
}
/**
* 更新小说状态,上架或下架
* */
@PostMapping("updateBookStatus")
public ResultBean updateBookStatus(Long bookId,Byte status,HttpServletRequest request){
//查询作家信息
Author author = authorService.queryAuthor(getUserDetails(request).getId());
//判断作者状态是否正常
if(author.getStatus()==1){
//封禁状态,不能发布小说
return ResultBean.fail(ResponseStatus.AUTHOR_STATUS_FORBIDDEN);
}
//更新小说状态,上架或下架
bookService.updateBookStatus(bookId,status,author.getId());
return ResultBean.ok();
}
/**
* 发布章节内容
* */
@PostMapping("addBookContent")
public ResultBean addBookContent(Long bookId,String indexName,String content,HttpServletRequest request){
//查询作家信息
Author author = authorService.queryAuthor(getUserDetails(request).getId());
//判断作者状态是否正常
if(author.getStatus()==1){
//封禁状态,不能发布小说
return ResultBean.fail(ResponseStatus.AUTHOR_STATUS_FORBIDDEN);
}
//发布章节内容
bookService.addBookContent(bookId,indexName,content,author.getId());
return ResultBean.ok();
}
}

View File

@ -1,19 +1,23 @@
package com.java2nb.novel.controller;
import com.java2nb.novel.core.bean.ResultBean;
import com.java2nb.novel.core.bean.UserDetails;
import com.java2nb.novel.core.utils.ThreadLocalUtil;
import com.java2nb.novel.entity.Book;
import com.java2nb.novel.entity.BookContent;
import com.java2nb.novel.entity.BookIndex;
import com.java2nb.novel.entity.News;
import com.java2nb.novel.entity.*;
import com.java2nb.novel.service.AuthorService;
import com.java2nb.novel.service.BookService;
import com.java2nb.novel.service.NewsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.util.List;
/**
@ -22,12 +26,14 @@ import java.util.List;
@Slf4j
@RequiredArgsConstructor
@Controller
public class PageController{
public class PageController extends BaseController{
private final BookService bookService;
private final NewsService newsService;
private final AuthorService authorService;
@RequestMapping("{url}.html")
public String module(@PathVariable("url") String url) {
@ -35,7 +41,22 @@ public class PageController{
}
@RequestMapping("{module}/{url}.html")
public String module2(@PathVariable("module") String module, @PathVariable("url") String url) {
public String module2(@PathVariable("module") String module, @PathVariable("url") String url,HttpServletRequest request) {
if(request.getRequestURI().startsWith("/author")) {
//访问作者专区
UserDetails user = getUserDetails(request);
if (user == null) {
//未登录
return "redirect:/user/login.html?originUrl=" + URLEncoder.encode(request.getRequestURL().toString());
}
boolean isAuthor = authorService.isAuthor(user.getId());
if (!isAuthor) {
return "redirect:/author/register.html" ;
}
}
return module + "/" + url;
}
@ -141,4 +162,30 @@ public class PageController{
return "about/news_info";
}
/**
* 作者注册页面
* */
@RequestMapping("author/register.html")
public String authorRegister(Author author, HttpServletRequest request, Model model){
UserDetails user = getUserDetails(request);
if(user == null){
//未登录
return "redirect:/user/login.html?originUrl=/author/register.html";
}
if(StringUtils.isNotBlank(author.getInviteCode())) {
//提交作者注册信息
String errorInfo = authorService.register(user.getId(), author);
if(StringUtils.isBlank(errorInfo)){
//注册成功
return "redirect:/author/index.html";
}
model.addAttribute("LabErr",errorInfo);
model.addAttribute("author",author);
}
return "author/register";
}
}

View File

@ -0,0 +1,42 @@
package com.java2nb.novel.service;
import com.java2nb.novel.entity.Author;
import com.java2nb.novel.entity.FriendLink;
import java.util.List;
/**
* @author 11797
*/
public interface AuthorService {
/**
* 校验笔名是否存在
* @param penName 校验的笔名
* @return true存在该笔名false: 不存在该笔名
* */
Boolean checkPenName(String penName);
/**
* 作家注册
* @param userId 注册用户ID
*@param author 注册信息
* @return 返回错误信息
* */
String register(Long userId, Author author);
/**
* 判断是否是作家
* @param userId 用户ID
* @return true是作家false: 不是作家
* */
Boolean isAuthor(Long userId);
/**
* 查询作家信息
* @param userId 用户ID
* @return 作家对象
* */
Author queryAuthor(Long userId);
}

View File

@ -191,4 +191,37 @@ public interface BookService {
* @param bookId 小说ID
*/
void updateBookPicToLocal(String picUrl, Long bookId);
/**
* 通过作者ID查询小说分页列表
* @param userId 用户ID
* @param page 页码
* @param pageSize 分页大小
* */
List<Book> listBookPageByUserId(Long userId, int page, int pageSize);
/**
* 发布小说
* @param book 小说信息
* @param authorId 作家ID
* @param penName 作家笔名
* */
void addBook(Book book, Long authorId, String penName);
/**
* 更新小说状态,上架或下架
* @param bookId 小说ID
* @param status 更新的状态
* @param authorId 作者ID
* */
void updateBookStatus(Long bookId, Byte status, Long authorId);
/**
* 发布章节内容
* @param bookId 小说ID
* @param indexName 章节名
* @param content 章节内容
* @param authorId 作者ID
* */
void addBookContent(Long bookId, String indexName, String content, Long authorId);
}

View File

@ -0,0 +1,92 @@
package com.java2nb.novel.service.impl;
import com.java2nb.novel.core.cache.CacheKey;
import com.java2nb.novel.core.cache.CacheService;
import com.java2nb.novel.core.enums.ResponseStatus;
import com.java2nb.novel.core.exception.BusinessException;
import com.java2nb.novel.entity.Author;
import com.java2nb.novel.entity.FriendLink;
import com.java2nb.novel.mapper.*;
import com.java2nb.novel.service.AuthorService;
import com.java2nb.novel.service.FriendLinkService;
import lombok.RequiredArgsConstructor;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import static com.java2nb.novel.mapper.AuthorCodeDynamicSqlSupport.authorCode;
import static com.java2nb.novel.mapper.BookDynamicSqlSupport.book;
import static com.java2nb.novel.mapper.BookDynamicSqlSupport.id;
import static com.java2nb.novel.mapper.BookDynamicSqlSupport.updateTime;
import static com.java2nb.novel.mapper.FriendLinkDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.*;
import static org.mybatis.dynamic.sql.select.SelectDSL.select;
/**
* @author 11797
*/
@Service
@RequiredArgsConstructor
public class AuthorServiceImpl implements AuthorService {
private final AuthorMapper authorMapper;
private final AuthorCodeMapper authorCodeMapper;
@Override
public Boolean checkPenName(String penName) {
return authorMapper.count(c ->
c.where(AuthorDynamicSqlSupport.penName, isEqualTo(penName))) > 0;
}
@Transactional(rollbackFor = Exception.class)
@Override
public String register(Long userId, Author author) {
Date currentDate = new Date();
//判断邀请码是否有效
if (authorCodeMapper.count(c ->
c.where(AuthorCodeDynamicSqlSupport.inviteCode, isEqualTo(author.getInviteCode()))
.and(AuthorCodeDynamicSqlSupport.isUse, isEqualTo((byte) 0))
.and(AuthorCodeDynamicSqlSupport.validityTime, isGreaterThan(currentDate))) > 0) {
//邀请码有效
//保存作家信息
author.setUserId(userId);
author.setCreateTime(currentDate);
authorMapper.insertSelective(author);
//设置邀请码状态为已使用
authorCodeMapper.update(update(authorCode)
.set(AuthorCodeDynamicSqlSupport.isUse)
.equalTo((byte) 1)
.where(AuthorCodeDynamicSqlSupport.inviteCode,isEqualTo(author.getInviteCode()))
.build()
.render(RenderingStrategies.MYBATIS3));
return "";
} else {
//邀请码无效
return "邀请码无效!";
}
}
@Override
public Boolean isAuthor(Long userId) {
return authorMapper.count(c ->
c.where(AuthorDynamicSqlSupport.userId, isEqualTo(userId))) > 0;
}
@Override
public Author queryAuthor(Long userId) {
return authorMapper.selectMany(
select(AuthorDynamicSqlSupport.id,AuthorDynamicSqlSupport.penName,AuthorDynamicSqlSupport.status)
.from(AuthorDynamicSqlSupport.author)
.where(AuthorDynamicSqlSupport.userId,isEqualTo(userId))
.build()
.render(RenderingStrategies.MYBATIS3)).get(0);
}
}

View File

@ -14,6 +14,7 @@ import com.java2nb.novel.entity.*;
import com.java2nb.novel.entity.Book;
import com.java2nb.novel.mapper.*;
import com.java2nb.novel.search.BookSP;
import com.java2nb.novel.service.AuthorService;
import com.java2nb.novel.service.BookService;
import com.java2nb.novel.vo.BookCommentVO;
import com.java2nb.novel.vo.BookSettingVO;
@ -73,6 +74,8 @@ public class BookServiceImpl implements BookService {
private final CacheService cacheService;
private final AuthorService authorService;
@SneakyThrows
@Override
@ -194,7 +197,7 @@ public class BookServiceImpl implements BookService {
@Override
public Book queryBookDetail(Long bookId) {
SelectStatementProvider selectStatement = select(id, catName, catId, picUrl, bookName, authorId, authorName, bookDesc, bookStatus, visitCount, wordCount, lastIndexId, lastIndexName, lastIndexUpdateTime,score)
SelectStatementProvider selectStatement = select(id, catName, catId, picUrl, bookName, authorId, authorName, bookDesc, bookStatus, visitCount, wordCount, lastIndexId, lastIndexName, lastIndexUpdateTime,score,status)
.from(book)
.where(id, isEqualTo(bookId))
.build()
@ -211,7 +214,7 @@ public class BookServiceImpl implements BookService {
PageHelper.startPage(1,limit);
}
SelectStatementProvider selectStatement = select(BookIndexDynamicSqlSupport.id, BookIndexDynamicSqlSupport.bookId, BookIndexDynamicSqlSupport.indexNum, BookIndexDynamicSqlSupport.indexName, BookIndexDynamicSqlSupport.updateTime)
SelectStatementProvider selectStatement = select(BookIndexDynamicSqlSupport.id, BookIndexDynamicSqlSupport.bookId, BookIndexDynamicSqlSupport.indexNum, BookIndexDynamicSqlSupport.indexName, BookIndexDynamicSqlSupport.updateTime,BookIndexDynamicSqlSupport.isVip)
.from(bookIndex)
.where(BookIndexDynamicSqlSupport.bookId, isEqualTo(bookId))
.build()
@ -222,7 +225,7 @@ public class BookServiceImpl implements BookService {
@Override
public BookIndex queryBookIndex(Long bookIndexId) {
SelectStatementProvider selectStatement = select(BookIndexDynamicSqlSupport.id, BookIndexDynamicSqlSupport.bookId, BookIndexDynamicSqlSupport.indexNum, BookIndexDynamicSqlSupport.indexName, BookIndexDynamicSqlSupport.wordCount, BookIndexDynamicSqlSupport.updateTime)
SelectStatementProvider selectStatement = select(BookIndexDynamicSqlSupport.id, BookIndexDynamicSqlSupport.bookId, BookIndexDynamicSqlSupport.indexNum, BookIndexDynamicSqlSupport.indexName, BookIndexDynamicSqlSupport.wordCount, BookIndexDynamicSqlSupport.updateTime,BookIndexDynamicSqlSupport.isVip)
.from(bookIndex)
.where(BookIndexDynamicSqlSupport.id, isEqualTo(bookIndexId))
.build()
@ -302,6 +305,7 @@ public class BookServiceImpl implements BookService {
}
SelectStatementProvider selectStatement = select(id, catId, catName, bookName, lastIndexId, lastIndexName, authorId, authorName, picUrl, bookDesc, wordCount, lastIndexUpdateTime)
.from(book)
.where(wordCount,isGreaterThan(0))
.orderBy(sortSpecification)
.limit(limit)
.build()
@ -454,5 +458,103 @@ public class BookServiceImpl implements BookService {
}
@Override
public List<Book> listBookPageByUserId(Long userId, int page, int pageSize) {
PageHelper.startPage(page,pageSize);
SelectStatementProvider selectStatement = select(id, bookName, visitCount, lastIndexName, status)
.from(book)
.where(authorId, isEqualTo(authorService.queryAuthor(userId).getId()))
.orderBy(BookDynamicSqlSupport.createTime.descending())
.build()
.render(RenderingStrategies.MYBATIS3);
return bookMapper.selectMany(selectStatement);
}
@Override
public void addBook(Book book, Long authorId, String penName) {
//判断小说名是否存在
if(queryIdByNameAndAuthor(book.getBookName(),penName)!=null){
//该作者发布过此书名的小说
throw new BusinessException(ResponseStatus.BOOKNAME_EXISTS);
};
book.setAuthorName(penName);
book.setAuthorId(authorId);
book.setVisitCount(0L);
book.setWordCount(0);
book.setScore(6.5f);
book.setLastIndexName("");
book.setCreateTime(new Date());
book.setUpdateTime(book.getCreateTime());
bookMapper.insertSelective(book);
}
@Override
public void updateBookStatus(Long bookId, Byte status, Long authorId) {
bookMapper.update(update(book)
.set(BookDynamicSqlSupport.status)
.equalTo(status)
.where(id,isEqualTo(bookId))
.and(BookDynamicSqlSupport.authorId,isEqualTo(authorId))
.build()
.render(RenderingStrategies.MYBATIS3));
}
@Transactional
@Override
public void addBookContent(Long bookId, String indexName, String content, Long authorId) {
Book book = queryBookDetail(bookId);
if(!authorId.equals(book.getAuthorId())){
//并不是更新自己的小说
return;
}
Long lastIndexId = new IdWorker().nextId();
Date currentDate = new Date();
int wordCount = content.length();
//更新小说主表信息
bookMapper.update(update(BookDynamicSqlSupport.book)
.set(BookDynamicSqlSupport.lastIndexId)
.equalTo(lastIndexId)
.set(BookDynamicSqlSupport.lastIndexName)
.equalTo(indexName)
.set(BookDynamicSqlSupport.lastIndexUpdateTime)
.equalTo(currentDate)
.set(BookDynamicSqlSupport.wordCount)
.equalTo(book.getWordCount()+wordCount)
.where(id,isEqualTo(bookId))
.and(BookDynamicSqlSupport.authorId,isEqualTo(authorId))
.build()
.render(RenderingStrategies.MYBATIS3));
//更新小说目录表
int indexNum = 0;
if(book.getLastIndexId() != null){
indexNum = queryBookIndex(book.getLastIndexId()).getIndexNum()+1;
}
BookIndex lastBookIndex = new BookIndex();
lastBookIndex.setId(lastIndexId);
lastBookIndex.setWordCount(wordCount);
lastBookIndex.setIndexName(indexName);
lastBookIndex.setIndexNum(indexNum);
lastBookIndex.setBookId(bookId);
lastBookIndex.setIsVip(book.getStatus());
lastBookIndex.setCreateTime(currentDate);
lastBookIndex.setUpdateTime(currentDate);
bookIndexMapper.insertSelective(lastBookIndex);
//更新小说内容表
BookContent bookContent = new BookContent();
bookContent.setIndexId(lastIndexId);
bookContent.setContent(content);
bookContentMapper.insertSelective(bookContent);
}
}