mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-02 23:23:28 +08:00
添加管理添加书籍前校验用户token是否有效,同时修改了获取用户信息的部分代码(从Controller层挪到Service层)(还未测试)
This commit is contained in:
@@ -9,11 +9,14 @@ 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;
|
||||
import plus.bookshelf.Service.Impl.UserServiceImpl;
|
||||
import plus.bookshelf.Service.Model.BookModel;
|
||||
import plus.bookshelf.Service.Model.CategoryModel;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
import plus.bookshelf.Service.Service.BookService;
|
||||
|
||||
import java.util.List;
|
||||
@@ -26,6 +29,9 @@ public class BookController extends BaseController {
|
||||
@Autowired
|
||||
BookService bookService;
|
||||
|
||||
@Autowired
|
||||
UserServiceImpl userService;
|
||||
|
||||
@ApiOperation(value = "获取书籍信息", notes = "获取书籍信息")
|
||||
// @ApiImplicitParam(name = "book", value = "图书详细实体", required = true, dataType = "Book")
|
||||
@RequestMapping(value = "get", method = {RequestMethod.GET})
|
||||
@@ -66,6 +72,48 @@ public class BookController extends BaseController {
|
||||
return CommonReturnType.create(bookModels);
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation(value = "【管理员】添加书籍", notes = "管理员在后台添加书籍")
|
||||
@RequestMapping(value = "add", method = {RequestMethod.GET})
|
||||
@ResponseBody
|
||||
public CommonReturnType add(@RequestParam(value = "token", required = false) String token,
|
||||
@RequestParam(required = false, value = "bookName") String bookName,
|
||||
@RequestParam(required = false, value = "description") String description,
|
||||
@RequestParam(required = false, value = "categoryId") Integer categoryId,
|
||||
@RequestParam(required = false, value = "publishingHouse") String publishingHouse,
|
||||
@RequestParam(required = false, value = "language") String language,
|
||||
@RequestParam(required = false, value = "copyright") String copyright,
|
||||
@RequestParam(required = false, value = "isDelete") Boolean isDelete,
|
||||
@RequestParam(required = false, value = "thumbnail") String thumbnail,
|
||||
@RequestParam(required = false, value = "author") String author) throws BusinessException {
|
||||
// 已经在 getUserByToken 方法中判断了 token 为空、不合法;用户不存在情况,此处无需再判断
|
||||
UserModel userModel = userService.getUserByToken(redisTemplate, token);
|
||||
|
||||
BookModel bookModel = new BookModel();
|
||||
|
||||
bookModel.setBookName(bookName);
|
||||
bookModel.setDescription(description);
|
||||
bookModel.setPublishingHouse(publishingHouse);
|
||||
bookModel.setLanguage(language);
|
||||
bookModel.setCopyright(copyright);
|
||||
bookModel.setIsDelete(isDelete);
|
||||
bookModel.setThumbnail(thumbnail);
|
||||
bookModel.setAuthor(author);
|
||||
if (categoryId != null) {
|
||||
CategoryModel categoryModel = new CategoryModel();
|
||||
categoryModel.setId(categoryId);
|
||||
bookModel.setCategory(categoryModel);
|
||||
}
|
||||
|
||||
Integer affectRows = bookService.addBook(bookModel);
|
||||
if (affectRows > 0) {
|
||||
return CommonReturnType.create("success");
|
||||
} else {
|
||||
return CommonReturnType.create("failed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private BookVO convertFromModel(BookModel bookModel) {
|
||||
BookVO bookVO = new BookVO();
|
||||
BeanUtils.copyProperties(bookModel, bookVO);
|
||||
|
@@ -89,18 +89,8 @@ public class UserController extends BaseController {
|
||||
@RequestMapping(value = "getUserStatus", method = {RequestMethod.GET})
|
||||
@ResponseBody
|
||||
public CommonReturnType getUserStatus(@RequestParam(value = "token", required = false) 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 = userService.getUserById(userId);
|
||||
// 已经在 getUserByToken 方法中判断了 token 为空、不合法;用户不存在情况,此处无需再判断
|
||||
UserModel userModel = userService.getUserByToken(redisTemplate, token);
|
||||
UserVO userVO = convertFromService(userModel);
|
||||
return CommonReturnType.create(userVO);
|
||||
}
|
||||
|
@@ -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