mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-01 22:53:29 +08:00
文件关联书籍功能完成
This commit is contained in:
@@ -30,6 +30,7 @@ public enum BusinessErrorCode implements CommonError {
|
||||
|
||||
// 60000开头为文件、文件对象相关错误定义
|
||||
FILE_ALREADY_EXIST(60001, "文件已存在"),
|
||||
FILE_NOT_EXIST(60002, "文件不存在"),
|
||||
|
||||
// 占位
|
||||
PLACE_HOLDER(99999, "这是一个占位符错误");
|
||||
|
@@ -16,6 +16,7 @@ import plus.bookshelf.Config.QCloudCosConfig;
|
||||
import plus.bookshelf.Controller.VO.FileObjectVO;
|
||||
import plus.bookshelf.Controller.VO.FileVO;
|
||||
import plus.bookshelf.Service.Impl.*;
|
||||
import plus.bookshelf.Service.Model.BookModel;
|
||||
import plus.bookshelf.Service.Model.FileModel;
|
||||
import plus.bookshelf.Service.Model.FileObjectModel;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
@@ -140,6 +141,44 @@ public class FileController extends BaseController {
|
||||
return CommonReturnType.create(fileVO);
|
||||
}
|
||||
|
||||
@Autowired
|
||||
BookServiceImpl bookService;
|
||||
|
||||
@ApiOperation(value = "【管理员】将书籍和文件进行绑定", notes = "")
|
||||
@RequestMapping(value = "bindBook", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
|
||||
@ResponseBody
|
||||
public CommonReturnType bindBook(@RequestParam(value = "token", required = false) String token,
|
||||
@RequestParam(value = "fileId", required = true) Integer fileId,
|
||||
@RequestParam(value = "bookId", required = true) Integer bookId) throws BusinessException {
|
||||
|
||||
UserModel userModel = userService.getUserByToken(redisTemplate, token);
|
||||
if (userModel == null || !Objects.equals(userModel.getGroup(), "ADMIN")) {
|
||||
throw new BusinessException(BusinessErrorCode.OPERATION_NOT_ALLOWED, "非管理员用户无权进行此操作");
|
||||
}
|
||||
|
||||
if (fileId == null || bookId == null) {
|
||||
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR);
|
||||
}
|
||||
|
||||
FileModel fileModel = fileService.getFileById(fileId);
|
||||
if (fileModel == null) {
|
||||
throw new BusinessException(BusinessErrorCode.FILE_NOT_EXIST);
|
||||
}
|
||||
|
||||
BookModel bookModel = bookService.getBookById(bookId);
|
||||
if (bookModel == null) {
|
||||
throw new BusinessException(BusinessErrorCode.BOOK_NOT_EXIST);
|
||||
}
|
||||
|
||||
fileModel.setBookId(bookModel.getId());
|
||||
Integer affectRows = fileService.updateSelective(fileModel);
|
||||
if (affectRows > 0) {
|
||||
return CommonReturnType.create("success");
|
||||
} else {
|
||||
throw new BusinessException(BusinessErrorCode.UNKNOWN_ERROR, "绑定失败,未知错误");
|
||||
}
|
||||
}
|
||||
|
||||
private FileVO convertFileVOFromModel(FileModel fileModel) {
|
||||
if (fileModel == null) {
|
||||
return null;
|
||||
|
@@ -164,6 +164,22 @@ public class FileServiceImpl implements FileService {
|
||||
return fileDO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文件
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Integer updateSelective(FileModel fileModel) {
|
||||
FileDO fileDO = convertFromFileModel(fileModel);
|
||||
|
||||
// 如果文件 id 不对,那么不能更新
|
||||
if (fileDO.getId() == 0 || fileDO.getId() == null) {
|
||||
return 0;
|
||||
}
|
||||
return fileDOMapper.updateByPrimaryKeySelective(fileDO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消文件和书籍的关联
|
||||
*
|
||||
|
@@ -60,6 +60,14 @@ public interface FileService {
|
||||
*/
|
||||
Boolean addFile(FileModel fileModel) throws InvocationTargetException, IllegalAccessException;
|
||||
|
||||
/**
|
||||
* 更新文件对象
|
||||
*
|
||||
* @param fileModel
|
||||
* @return
|
||||
*/
|
||||
Integer updateSelective(FileModel fileModel);
|
||||
|
||||
/**
|
||||
* 取消文件和书籍的关联
|
||||
*
|
||||
|
Reference in New Issue
Block a user