mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-01 22:53:29 +08:00
添加用户收藏书籍、取消收藏书籍、获取书籍收藏状态功能(后端+数据库);更新Api文档
This commit is contained in:
@@ -18,8 +18,15 @@ public enum BusinessErrorCode implements CommonError {
|
||||
// 40000开头为第三方登录相关错误定义
|
||||
THIRD_PARTY_LOGIN_FAIL(40001, "第三方登录失败"),
|
||||
THIRD_PARTY_ACCOUNT_ALREADY_BOUND(40002, "该账号已被其他账号绑定"),
|
||||
THIRD_PARTY_UNBIND_FAIL(40003, "第三方账号解绑失败");
|
||||
THIRD_PARTY_UNBIND_FAIL(40003, "第三方账号解绑失败"),
|
||||
|
||||
// 50000开头为书籍相关错误定义
|
||||
BOOK_NOT_EXIST(50001, "书籍不存在"),
|
||||
BOOK_FAVORITES_ALREADY_EXIST(50002, "书籍已经在收藏夹中"),
|
||||
BOOK_FAVORITES_NOT_EXIST(50003, "书籍不在收藏夹中"),
|
||||
|
||||
// 占位
|
||||
PLACE_HOLDER(99999, "这是一个占位符错误");
|
||||
|
||||
private BusinessErrorCode(int errCode, String errMsg) {
|
||||
this.errCode = errCode;
|
||||
|
@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import plus.bookshelf.Common.Error.BusinessErrorCode;
|
||||
import plus.bookshelf.Common.Error.BusinessException;
|
||||
import plus.bookshelf.Common.Response.CommonReturnType;
|
||||
import plus.bookshelf.Controller.VO.BookVO;
|
||||
@@ -19,6 +20,7 @@ import plus.bookshelf.Service.Model.UserModel;
|
||||
import plus.bookshelf.Service.Service.BookService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Api(tags = "书籍信息")
|
||||
@Controller("book")
|
||||
@@ -71,6 +73,49 @@ public class BookController extends BaseController {
|
||||
return CommonReturnType.create(bookModels);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "【用户】收藏/取消收藏书籍", notes = "用户收藏书籍")
|
||||
@RequestMapping(value = "toggleFavorites", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
@ResponseBody
|
||||
public CommonReturnType addFavorites(@RequestParam(value = "token", required = false) String token,
|
||||
@RequestParam(value = "bookId", required = false) Integer bookId,
|
||||
@RequestParam(value = "status", required = true) Boolean isFavoritesNow) throws BusinessException {
|
||||
|
||||
// 已经在 getUserByToken 方法中判断了 token 为空、不合法;用户不存在情况,此处无需再判断
|
||||
UserModel userModel = userService.getUserByToken(redisTemplate, token);
|
||||
|
||||
if (isFavoritesNow) {
|
||||
// 这里查询一次书籍的用处是为了防止用户收藏的书籍不存在
|
||||
BookModel bookModel = bookService.getBookById(bookId);
|
||||
if (bookModel == null) {
|
||||
throw new BusinessException(BusinessErrorCode.BOOK_NOT_EXIST, "书籍不存在");
|
||||
}
|
||||
|
||||
Boolean isSuccess = bookService.addFavorites(userModel.getId(), bookModel.getId());
|
||||
if (!isSuccess) {
|
||||
throw new BusinessException(BusinessErrorCode.BOOK_FAVORITES_ALREADY_EXIST, "书籍已经收藏");
|
||||
}
|
||||
} else {
|
||||
// 不用担心书籍不存在的情况,因为书籍不存在数据库中不会删除成功
|
||||
Boolean isSuccess = bookService.removeFavorites(userModel.getId(), bookId);
|
||||
if (!isSuccess) {
|
||||
throw new BusinessException(BusinessErrorCode.BOOK_FAVORITES_NOT_EXIST, "书籍尚未收藏");
|
||||
}
|
||||
}
|
||||
return CommonReturnType.create("success");
|
||||
}
|
||||
|
||||
@ApiOperation(value = "【用户】收藏/取消收藏书籍", notes = "用户收藏书籍")
|
||||
@RequestMapping(value = "getFavoritesStatus", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
@ResponseBody
|
||||
public CommonReturnType addFavorites(@RequestParam(value = "token", required = false) String token,
|
||||
@RequestParam(value = "bookId", required = false) Integer bookId) throws BusinessException {
|
||||
|
||||
// 已经在 getUserByToken 方法中判断了 token 为空、不合法;用户不存在情况,此处无需再判断
|
||||
UserModel userModel = userService.getUserByToken(redisTemplate, token);
|
||||
|
||||
Map favoritesStatus = bookService.getFavoritesStatus(userModel.getId(), bookId);
|
||||
return CommonReturnType.create(favoritesStatus);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "【管理员】添加书籍", notes = "管理员在后台添加书籍")
|
||||
@RequestMapping(value = "add", method = {RequestMethod.GET})
|
||||
|
@@ -0,0 +1,137 @@
|
||||
package plus.bookshelf.Dao.DO;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class UserFavoritesDO {
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_book_favorites_relation.id
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_book_favorites_relation.user_id
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
private Integer userId;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_book_favorites_relation.book_id
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
private Integer bookId;
|
||||
|
||||
/**
|
||||
*
|
||||
* This field was generated by MyBatis Generator.
|
||||
* This field corresponds to the database column user_book_favorites_relation.create_time
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_book_favorites_relation.id
|
||||
*
|
||||
* @return the value of user_book_favorites_relation.id
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_book_favorites_relation.id
|
||||
*
|
||||
* @param id the value for user_book_favorites_relation.id
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_book_favorites_relation.user_id
|
||||
*
|
||||
* @return the value of user_book_favorites_relation.user_id
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public Integer getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_book_favorites_relation.user_id
|
||||
*
|
||||
* @param userId the value for user_book_favorites_relation.user_id
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public void setUserId(Integer userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_book_favorites_relation.book_id
|
||||
*
|
||||
* @return the value of user_book_favorites_relation.book_id
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public Integer getBookId() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_book_favorites_relation.book_id
|
||||
*
|
||||
* @param bookId the value for user_book_favorites_relation.book_id
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public void setBookId(Integer bookId) {
|
||||
this.bookId = bookId;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method returns the value of the database column user_book_favorites_relation.create_time
|
||||
*
|
||||
* @return the value of user_book_favorites_relation.create_time
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public Date getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method sets the value of the database column user_book_favorites_relation.create_time
|
||||
*
|
||||
* @param createTime the value for user_book_favorites_relation.create_time
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
public void setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
package plus.bookshelf.Dao.Mapper;
|
||||
|
||||
import plus.bookshelf.Dao.DO.UserFavoritesDO;
|
||||
|
||||
public interface UserFavoritesDOMapper {
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_book_favorites_relation
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
int deleteByPrimaryKey(Integer id);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_book_favorites_relation
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
int insert(UserFavoritesDO record);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_book_favorites_relation
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
int insertSelective(UserFavoritesDO record);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_book_favorites_relation
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
UserFavoritesDO selectByPrimaryKey(Integer id);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_book_favorites_relation
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
int updateByPrimaryKeySelective(UserFavoritesDO record);
|
||||
|
||||
/**
|
||||
* This method was generated by MyBatis Generator.
|
||||
* This method corresponds to the database table user_book_favorites_relation
|
||||
*
|
||||
* @mbg.generated
|
||||
*/
|
||||
int updateByPrimaryKey(UserFavoritesDO record);
|
||||
|
||||
/**
|
||||
* 通过用户id和书籍id删除用户收藏状态
|
||||
* @param userId
|
||||
* @param bookId
|
||||
* @return
|
||||
*/
|
||||
int deleteByUserIdAndBookId(Integer userId, Integer bookId);
|
||||
|
||||
/**
|
||||
* 通过用户id和书籍id获取用户收藏状态
|
||||
* @param userId
|
||||
* @param bookId
|
||||
* @return
|
||||
*/
|
||||
UserFavoritesDO selectCountByUserIdAndBookId(Integer userId, Integer bookId);
|
||||
}
|
@@ -1,8 +1,6 @@
|
||||
package plus.bookshelf.Service.Impl;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -12,14 +10,18 @@ import plus.bookshelf.Common.Validator.ValidationResult;
|
||||
import plus.bookshelf.Common.Validator.ValidatorImpl;
|
||||
import plus.bookshelf.Dao.DO.BookDO;
|
||||
import plus.bookshelf.Dao.DO.BookDOExample;
|
||||
import plus.bookshelf.Dao.DO.UserFavoritesDO;
|
||||
import plus.bookshelf.Dao.Mapper.BookDOMapper;
|
||||
import plus.bookshelf.Dao.Mapper.UserFavoritesDOMapper;
|
||||
import plus.bookshelf.Service.Model.BookModel;
|
||||
import plus.bookshelf.Service.Model.CategoryModel;
|
||||
import plus.bookshelf.Service.Service.BookService;
|
||||
import plus.bookshelf.Service.Service.CategoryService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class BookServiceImpl implements BookService {
|
||||
@@ -30,6 +32,9 @@ public class BookServiceImpl implements BookService {
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
@Autowired
|
||||
private UserFavoritesDOMapper userFavoritesDOMapper;
|
||||
|
||||
@Autowired
|
||||
ValidatorImpl validator;
|
||||
|
||||
@@ -83,6 +88,62 @@ public class BookServiceImpl implements BookService {
|
||||
return bookDOMapper.insertSelective(bookDO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户收藏书籍
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @param bookId 书籍id
|
||||
* @return
|
||||
* @throws BusinessException
|
||||
*/
|
||||
@Override
|
||||
public Boolean addFavorites(Integer userId, Integer bookId) throws BusinessException {
|
||||
UserFavoritesDO userFavoritesDO = new UserFavoritesDO();
|
||||
userFavoritesDO.setBookId(bookId);
|
||||
userFavoritesDO.setUserId(userId);
|
||||
int affectRows = userFavoritesDOMapper.insert(userFavoritesDO);
|
||||
return affectRows > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户取消收藏书籍
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @param bookId 书籍id
|
||||
* @return
|
||||
* @throws BusinessException
|
||||
*/
|
||||
@Override
|
||||
public Boolean removeFavorites(Integer userId, Integer bookId) throws BusinessException {
|
||||
int affectRows = userFavoritesDOMapper.deleteByUserIdAndBookId(userId, bookId);
|
||||
return affectRows > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户收藏状态
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @param bookId 书籍id
|
||||
* @return
|
||||
* @throws BusinessException
|
||||
*/
|
||||
@Override
|
||||
public Map getFavoritesStatus(Integer userId, Integer bookId) throws BusinessException {
|
||||
UserFavoritesDO userFavoritesDO = userFavoritesDOMapper.selectCountByUserIdAndBookId(userId, bookId);
|
||||
|
||||
Map<String, String> result = new HashMap<>();
|
||||
if (userFavoritesDO == null) {
|
||||
// 用户未收藏
|
||||
result.put("status", "0");
|
||||
result.put("time", null);
|
||||
} else {
|
||||
// 用户已收藏,返回收藏时间
|
||||
result.put("status", "1");
|
||||
result.put("time", userFavoritesDO.getCreateTime().toString());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private BookModel convertFromDataObjecct(BookDO bookDO) {
|
||||
if (bookDO == null) {
|
||||
return null;
|
||||
|
@@ -5,6 +5,7 @@ import plus.bookshelf.Dao.DO.BookDO;
|
||||
import plus.bookshelf.Service.Model.BookModel;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface BookService {
|
||||
/**
|
||||
@@ -29,4 +30,30 @@ public interface BookService {
|
||||
* @return
|
||||
*/
|
||||
Integer addBook(BookModel bookModel) throws BusinessException;
|
||||
|
||||
/**
|
||||
* 用户收藏书籍
|
||||
*
|
||||
* @param bookId 书籍id
|
||||
* @return
|
||||
*/
|
||||
Boolean addFavorites(Integer userId, Integer bookId) throws BusinessException;
|
||||
|
||||
/**
|
||||
* 用户取消收藏书籍
|
||||
* @param userId
|
||||
* @param bookId
|
||||
* @return
|
||||
* @throws BusinessException
|
||||
*/
|
||||
Boolean removeFavorites(Integer userId, Integer bookId) throws BusinessException;
|
||||
|
||||
/**
|
||||
* 获取用户收藏状态
|
||||
* @param userId 用户id
|
||||
* @param bookId 书籍id
|
||||
* @return
|
||||
* @throws BusinessException
|
||||
*/
|
||||
Map getFavoritesStatus(Integer userId, Integer bookId) throws BusinessException;
|
||||
}
|
||||
|
Reference in New Issue
Block a user