1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-01 22:53:29 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

用户收藏列表后端完成

This commit is contained in:
2022-04-07 10:38:25 +08:00
parent 1d93ade6ff
commit af88f65679
5 changed files with 68 additions and 7 deletions

View File

@@ -76,9 +76,9 @@ public class BookController extends BaseController {
@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 {
public CommonReturnType toggleFavorites(@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);
@@ -107,8 +107,8 @@ public class BookController extends BaseController {
@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 {
public CommonReturnType getFavoritesStatus(@RequestParam(value = "token", required = false) String token,
@RequestParam(value = "bookId", required = false) Integer bookId) throws BusinessException {
// 已经在 getUserByToken 方法中判断了 token 为空、不合法;用户不存在情况,此处无需再判断
UserModel userModel = userService.getUserByToken(redisTemplate, token);
@@ -117,6 +117,18 @@ public class BookController extends BaseController {
return CommonReturnType.create(favoritesStatus);
}
@ApiOperation(value = "【用户】用户收藏书籍列表", notes = "获取用户的收藏书籍列表")
@RequestMapping(value = "getFavoritesList", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType getFavoritesList(@RequestParam(value = "token", required = false) String token) throws BusinessException {
// 已经在 getUserByToken 方法中判断了 token 为空、不合法;用户不存在情况,此处无需再判断
UserModel userModel = userService.getUserByToken(redisTemplate, token);
List<BookModel> favoritesList = bookService.getFavoritesList(userModel.getId());
return CommonReturnType.create(favoritesList);
}
@ApiOperation(value = "【管理员】添加/修改书籍", notes = "管理员在后台添加/修改书籍bookId 传 0 或 null 或 不传 即为添加)")
@RequestMapping(value = "detail", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody

View File

@@ -79,4 +79,11 @@ public interface BookDOMapper {
* @mbg.generated
*/
int updateByPrimaryKey(BookDO record);
/**
* 通过用户id获取用户收藏书籍列表
* @param userId
* @return
*/
BookDO[] selectFavoritesListByUserId(Integer userId);
}

View File

@@ -37,6 +37,7 @@ public class BookServiceImpl implements BookService {
/**
* 通过 书籍id 查找图书
*
* @param id
* @return
*/
@@ -51,6 +52,7 @@ public class BookServiceImpl implements BookService {
/**
* 查找图书
*
* @param bookModel
* @return
* @throws BusinessException
@@ -85,6 +87,7 @@ public class BookServiceImpl implements BookService {
/**
* 添加图书
*
* @param bookModel
* @return
* @throws BusinessException
@@ -103,6 +106,7 @@ public class BookServiceImpl implements BookService {
/**
* 修改图书信息
*
* @param bookModel
* @return
* @throws BusinessException
@@ -121,13 +125,14 @@ public class BookServiceImpl implements BookService {
/**
* 通过 id 删除图书
*
* @param bookId
* @return
* @throws BusinessException
*/
@Override
public Integer deleteBook(Integer bookId) throws BusinessException {
if(bookId == null || bookId == 0) {
if (bookId == null || bookId == 0) {
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "书籍id不能为空");
}
return bookDOMapper.deleteByPrimaryKey(bookId);
@@ -165,6 +170,25 @@ public class BookServiceImpl implements BookService {
return affectRows > 0;
}
/**
* 获取用户收藏书籍列表
*
* @param userId 用户id
* @return
* @throws BusinessException
*/
@Override
public List<BookModel> getFavoritesList(Integer userId) throws BusinessException {
BookDO[] bookDOS = bookDOMapper.selectFavoritesListByUserId(userId);
List<BookModel> bookModels = new ArrayList<>();
for (BookDO bookDO : bookDOS) {
BookModel bookModel = convertFromDataObjecct(bookDO);
bookModels.add(bookModel);
}
return bookModels;
}
/**
* 获取用户收藏状态
*
@@ -212,6 +236,7 @@ public class BookServiceImpl implements BookService {
return bookModel;
}
private BookDO convertToDataObjecct(BookModel bookModel) {
if (bookModel == null) {
return null;

View File

@@ -1,7 +1,6 @@
package plus.bookshelf.Service.Service;
import plus.bookshelf.Common.Error.BusinessException;
import plus.bookshelf.Dao.DO.BookDO;
import plus.bookshelf.Service.Model.BookModel;
import java.util.List;
@@ -64,6 +63,14 @@ public interface BookService {
*/
Boolean removeFavorites(Integer userId, Integer bookId) throws BusinessException;
/**
* 获取用户收藏书籍列表
* @param userId
* @return
* @throws BusinessException
*/
List<BookModel> getFavoritesList(Integer userId) throws BusinessException;
/**
* 获取用户收藏状态
* @param userId 用户id

View File

@@ -286,4 +286,14 @@
author = #{author,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectFavoritesListByUserId" parameterType="java.lang.Integer" resultMap="ResultMapWithBLOBs">
select
book_info.id, book_name, category_id, publishing_house, `language`, copyright, is_delete, thumbnail
<!--<include refid="Base_Column_List" />-->
,
<include refid="Blob_Column_List" />,
from user_book_favorites_relation
left join book_info on user_book_favorites_relation.book_id = book_info.id
where user_book_favorites_relation.user_id = #{userId,jdbcType=INTEGER}
</select>
</mapper>