mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-10-09 01:05:14 +08:00
添加管理添加书籍前校验用户token是否有效,同时修改了获取用户信息的部分代码(从Controller层挪到Service层)(还未测试)
This commit is contained in:
@@ -70,11 +70,24 @@ public class BookServiceImpl implements BookService {
|
||||
return convertFromDataObjecctList(bookDOs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer addBook(BookModel bookModel) throws BusinessException {
|
||||
|
||||
// 校验入参
|
||||
ValidationResult result = validator.validate(bookModel);
|
||||
if (result.isHasErrors()) {
|
||||
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
|
||||
}
|
||||
|
||||
BookDO bookDO = convertToDataObjecct(bookModel);
|
||||
return bookDOMapper.insertSelective(bookDO);
|
||||
}
|
||||
|
||||
private BookModel convertFromDataObjecct(BookDO bookDO) {
|
||||
BookModel bookModel = new BookModel();
|
||||
if (bookDO == null) {
|
||||
return null;
|
||||
}
|
||||
BookModel bookModel = new BookModel();
|
||||
bookModel.setId(bookDO.getId());
|
||||
bookModel.setBookName(bookDO.getBookName());
|
||||
bookModel.setDescription(bookDO.getDescription());
|
||||
@@ -92,6 +105,24 @@ public class BookServiceImpl implements BookService {
|
||||
return bookModel;
|
||||
}
|
||||
|
||||
private BookDO convertToDataObjecct(BookModel bookModel) {
|
||||
if (bookModel == null) {
|
||||
return null;
|
||||
}
|
||||
BookDO bookDO = new BookDO();
|
||||
bookDO.setId(bookModel.getId());
|
||||
bookDO.setBookName(bookModel.getBookName());
|
||||
bookDO.setDescription(bookModel.getDescription());
|
||||
bookDO.setAuthor(bookModel.getAuthor());
|
||||
bookDO.setPublishingHouse(bookModel.getPublishingHouse());
|
||||
bookDO.setCopyright(bookModel.getCopyright());
|
||||
bookDO.setIsDelete(bookModel.getIsDelete());
|
||||
bookDO.setThumbnail(bookModel.getThumbnail());
|
||||
bookDO.setLanguage(bookModel.getLanguage());
|
||||
bookDO.setCategoryId(bookModel.getCategory().getId());
|
||||
return bookDO;
|
||||
}
|
||||
|
||||
private List<BookModel> convertFromDataObjecctList(List<BookDO> bookDOs) {
|
||||
List<BookModel> bookModels = new ArrayList<>();
|
||||
for (BookDO bookDO : bookDOs) {
|
||||
|
@@ -1,7 +1,12 @@
|
||||
package plus.bookshelf.Service.Impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import plus.bookshelf.Common.Error.BusinessErrorCode;
|
||||
import plus.bookshelf.Common.Error.BusinessException;
|
||||
import plus.bookshelf.Common.SessionManager.RedisSessionManager;
|
||||
import plus.bookshelf.Controller.VO.UserVO;
|
||||
import plus.bookshelf.Dao.DO.UserDO;
|
||||
import plus.bookshelf.Dao.Mapper.UserDOMapper;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
@@ -29,6 +34,27 @@ public class UserServiceImpl implements UserService {
|
||||
return userModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserModel getUserByToken(RedisTemplate redisTemplate, String token) throws BusinessException {
|
||||
// token 未传入
|
||||
if (token == null || "".equals(token)) {
|
||||
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "用户令牌未传入");
|
||||
}
|
||||
|
||||
// token 已过期
|
||||
Object userIdObject = RedisSessionManager.getInstance(redisTemplate).getValue(token);
|
||||
if (userIdObject == null) {
|
||||
throw new BusinessException(BusinessErrorCode.USER_TOKEN_EXPIRED, "登陆过期啦,请重新登录");
|
||||
}
|
||||
|
||||
Integer userId = (Integer) userIdObject;
|
||||
UserModel userModel = getUserById(userId);
|
||||
if (userModel == null) {
|
||||
throw new BusinessException(BusinessErrorCode.USER_NOT_EXIST);
|
||||
}
|
||||
return userModel;
|
||||
}
|
||||
|
||||
private UserModel convertFromDataObject(UserDO userDO) {
|
||||
if (userDO == null) {
|
||||
return null;
|
||||
|
@@ -9,6 +9,7 @@ import java.util.List;
|
||||
public interface BookService {
|
||||
/**
|
||||
* 通过书籍Id获取书籍
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@@ -16,7 +17,16 @@ public interface BookService {
|
||||
|
||||
/**
|
||||
* 通过搜索条件获取书籍列表
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<BookModel> searchBooks(BookModel bookModel) throws BusinessException;
|
||||
|
||||
/**
|
||||
* 添加书籍
|
||||
*
|
||||
* @param bookModel
|
||||
* @return
|
||||
*/
|
||||
Integer addBook(BookModel bookModel) throws BusinessException;
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ import plus.bookshelf.Service.Model.CategoryModel;
|
||||
public interface CategoryService {
|
||||
/**
|
||||
* 通过分类Id获取分类
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package plus.bookshelf.Service.Service;
|
||||
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import plus.bookshelf.Common.Error.BusinessException;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
|
||||
public interface UserService {
|
||||
@@ -13,8 +15,18 @@ public interface UserService {
|
||||
|
||||
/**
|
||||
* 通过用户Id获取用户
|
||||
*
|
||||
* @param id 用户Id
|
||||
* @return
|
||||
*/
|
||||
UserModel getUserById(Integer id);
|
||||
|
||||
/**
|
||||
* 检查用户令牌是否有效,并返回令牌对应的用户 UserModel
|
||||
* (令牌无效直接抛出异常)
|
||||
*
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
UserModel getUserByToken(RedisTemplate redisTemplate, String token) throws BusinessException;
|
||||
}
|
||||
|
Reference in New Issue
Block a user