完善notebook

This commit is contained in:
雨霖铃 2025-08-11 22:30:46 +08:00
parent dda272b9ab
commit 6e7dde5235
4 changed files with 51 additions and 24 deletions

View File

@ -9,6 +9,7 @@ import asia.yulinling.workflow.model.entity.Notebook;
import asia.yulinling.workflow.model.vo.NotebookVO; import asia.yulinling.workflow.model.vo.NotebookVO;
import asia.yulinling.workflow.service.NotebookService; import asia.yulinling.workflow.service.NotebookService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -36,14 +37,24 @@ public class NotebookController {
} }
@GetMapping("/notebooks/{id}") @GetMapping("/notebooks/{id}")
public void notebooksGet(@PathVariable Long id) { public ApiResponse<NotebookVO> notebooksGet(@PathVariable Long id) {
NotebookVO notebookVO = notebookService.getNotebook(id);
return ApiResponse.ofStatus(Status.SUCCESS, notebookVO);
} }
@PutMapping("/notebooks/{id}") @PutMapping("/notebooks/{userId}/?id={id}")
public void notebooksPut(@PathVariable Long id) { public ApiResponse<Object> notebooksPut(@PathVariable Long userId, @PathVariable @NonNull Long id, @Valid @RequestBody NotebookRequest notebookRequest) {
Notebook notebook = new Notebook();
notebook.setId(id);
notebook.setUserId(userId);
BeanUtils.copyProperties(notebookRequest, notebook);
notebookService.updateNotebook(notebook);
return ApiResponse.ofStatus(Status.SUCCESS);
} }
@DeleteMapping("/notebooks/{id}") @DeleteMapping("/notebooks/{id}")
public void notebooksDelete(@PathVariable Long id) { public ApiResponse<Object> notebooksDelete(@PathVariable Long id) {
notebookService.deleteNotebookById(id);
return ApiResponse.ofStatus(Status.SUCCESS);
} }
} }

View File

@ -15,23 +15,21 @@ public interface NotebookService {
* *
* @param notebook 笔记本 * @param notebook 笔记本
*/ */
int addNotebook(Notebook notebook); void addNotebook(Notebook notebook);
/** /**
* 删除笔记本 * 删除笔记本
* *
* @param notebookId 笔记本id * @param notebookId 笔记本id
* @return 结果
*/ */
int deleteNotebookById(Long notebookId); void deleteNotebookById(Long notebookId);
/** /**
* 更新笔记本 * 更新笔记本
* *
* @param notebook 笔记本 * @param notebook 笔记本
* @return 结果
*/ */
int updateNotebook(Notebook notebook); void updateNotebook(Notebook notebook);
/** /**
* 获取笔记本内容 * 获取笔记本内容
@ -39,7 +37,7 @@ public interface NotebookService {
* @param notebookId 笔记本id * @param notebookId 笔记本id
* @return 结果 * @return 结果
*/ */
Notebook getNotebook(Long notebookId); NotebookVO getNotebook(Long notebookId);
/** /**
* 获取笔记本列表 * 获取笔记本列表

View File

@ -27,40 +27,58 @@ public class NotebookServiceImpl implements NotebookService {
private final UserService userService; private final UserService userService;
@Override @Override
public int addNotebook(Notebook notebook) { public void addNotebook(Notebook notebook) {
if (!userService.checkUserIdExist(notebook.getUserId())) { if (userService.checkUserIdExist(notebook.getUserId())) {
throw new ServiceException("用户不存在"); throw new ServiceException("用户不存在");
} }
return notebookMapper.insert(notebook); int result = notebookMapper.insert(notebook);
if (result == 0) {
throw new ServiceException("添加笔记本失败");
}
} }
@Override @Override
public int deleteNotebookById(Long notebookId) { public void deleteNotebookById(Long notebookId) {
if (!userService.checkUserIdExist(notebookId)) { if (userService.checkUserIdExist(notebookId)) {
throw new ServiceException("笔记本不存在"); throw new ServiceException("笔记本不存在");
} }
return notebookMapper.deleteById(notebookId); int result = notebookMapper.deleteById(notebookId);
if (result == 0) {
throw new ServiceException("删除笔记本失败");
}
} }
@Override @Override
public int updateNotebook(Notebook notebook) { public void updateNotebook(Notebook notebook) {
if (!userService.checkUserIdExist(notebook.getUserId())) { if (userService.checkUserIdExist(notebook.getUserId())) {
throw new ServiceException("用户不存在");
}
if (!checkNotebookIdExist(notebook.getId())) {
throw new ServiceException("笔记本不存在"); throw new ServiceException("笔记本不存在");
} }
return notebookMapper.updateById(notebook); int result = notebookMapper.updateById(notebook);
if (result == 0) {
throw new ServiceException("更新笔记本失败");
}
} }
@Override @Override
public Notebook getNotebook(Long notebookId) { public NotebookVO getNotebook(Long notebookId) {
if (!userService.checkUserIdExist(notebookId)) { if (!checkNotebookIdExist(notebookId)) {
throw new ServiceException("笔记本不存在"); throw new ServiceException("笔记本不存在");
} }
return notebookMapper.selectById(notebookId); Notebook notebook = notebookMapper.selectById(notebookId);
NotebookVO notebookVO = new NotebookVO();
BeanUtils.copyProperties(notebook, notebookVO);
return notebookVO;
} }
/** /**
@ -94,7 +112,7 @@ public class NotebookServiceImpl implements NotebookService {
*/ */
@Override @Override
public List<Notebook> getNotebookListByUserId(Long userId) { public List<Notebook> getNotebookListByUserId(Long userId) {
if (!userService.checkUserIdExist(userId)) { if (userService.checkUserIdExist(userId)) {
throw new ServiceException("用户不存在"); throw new ServiceException("用户不存在");
} }

View File

@ -57,7 +57,7 @@ public class UserServiceImpl implements UserService {
*/ */
@Override @Override
public boolean checkUserIdExist(Long userId) { public boolean checkUserIdExist(Long userId) {
return userMapper.exists(new LambdaQueryWrapper<User>().eq(User::getId, userId)); return !userMapper.exists(new LambdaQueryWrapper<User>().eq(User::getId, userId));
} }
/** /**