feattrash: 优化删除功能和回收站逻辑

- 修改 Markdown 文件和分组的删除逻辑,使用软删除方式
- 更新回收站相关接口和页面展示
-优化前端保存逻辑,支持新建文件和更新文件
- 调整后端 API 接口,使用更合适的 HTTP 方法
This commit is contained in:
ikmkj
2025-07-31 23:58:13 +08:00
parent 1491cfc330
commit c448ababa9
10 changed files with 67 additions and 34 deletions

View File

@@ -61,10 +61,9 @@ public class MarkdownController {
@Parameters({
@Parameter(name = "id", description = "Markdown文件ID", required = true),
})
@PostMapping("/delete")
public R<Void> deleteMarkdown(String id) {
long l = Long.parseLong(id);
if (markdownFileService.deleteMarkdownFile(l)) {
@DeleteMapping("/{id}")
public R<Void> deleteMarkdown(@PathVariable Long id) {
if (markdownFileService.deleteMarkdownFile(id)) {
return R.success();
}
return R.fail();

View File

@@ -13,7 +13,7 @@ public class TrashItemVo {
private String id;
@Schema(description = "项目名称(笔记标题或分组名称)")
private String name;
private String title;
@Schema(description = "项目类型note 或 group")
private String type;

View File

@@ -3,7 +3,13 @@ package com.test.bijihoudaun.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test.bijihoudaun.entity.Grouping;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface GroupingMapper extends BaseMapper<Grouping> {
@Select("SELECT * FROM grouping WHERE is_deleted = 1")
List<Grouping> selectDeleted();
}

View File

@@ -15,6 +15,7 @@ public interface MarkdownFileMapper extends BaseMapper<MarkdownFile> {
@Select("SELECT mf.*, g.grouping as groupingName " +
"FROM markdown_file mf " +
"LEFT JOIN grouping g ON mf.grouping_id = g.id " +
"WHERE mf.is_deleted = 0 " +
"ORDER BY mf.updated_at DESC " +
"LIMIT #{limit}")
List<MarkdownFileVO> selectRecentWithGrouping(@Param("limit") int limit);
@@ -22,7 +23,10 @@ public interface MarkdownFileMapper extends BaseMapper<MarkdownFile> {
@Select("SELECT mf.*, g.grouping as groupingName " +
"FROM markdown_file mf " +
"LEFT JOIN grouping g ON mf.grouping_id = g.id " +
"WHERE mf.grouping_id = #{groupingId} " +
"WHERE mf.grouping_id = #{groupingId} AND mf.is_deleted = 0 " +
"ORDER BY mf.updated_at DESC")
List<MarkdownFileVO> selectByGroupingIdWithGrouping(@Param("groupingId") String groupingId);
@Select("SELECT * FROM markdown_file WHERE is_deleted = 1")
List<MarkdownFile> selectDeleted();
}

View File

@@ -54,12 +54,19 @@ public class GroupingServiceImpl
@Override
@Transactional
public void deleteGrouping(Long id) {
LambdaUpdateWrapper<MarkdownFile> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(MarkdownFile::getGroupingId, id)
.set(MarkdownFile::getGroupingId, 999L);
markdownFileMapper.update(null, updateWrapper);
// 1. 使用 LambdaUpdateWrapper 软删除分组本身,确保 isDeleted 和 deletedAt 都被更新
LambdaUpdateWrapper<Grouping> groupingUpdateWrapper = new LambdaUpdateWrapper<>();
groupingUpdateWrapper.eq(Grouping::getId, id)
.set(Grouping::getIsDeleted, 1)
.set(Grouping::getDeletedAt, new java.util.Date());
this.update(groupingUpdateWrapper);
this.removeById(id);
// 2. 将该分组下的所有笔记也一并软删除
LambdaUpdateWrapper<MarkdownFile> markdownFileUpdateWrapper = new LambdaUpdateWrapper<>();
markdownFileUpdateWrapper.eq(MarkdownFile::getGroupingId, id)
.set(MarkdownFile::getIsDeleted, 1)
.set(MarkdownFile::getDeletedAt, new java.util.Date());
markdownFileMapper.update(null, markdownFileUpdateWrapper);
}
@Override

View File

@@ -1,6 +1,7 @@
package com.test.bijihoudaun.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.bijihoudaun.entity.MarkdownFile;
import com.test.bijihoudaun.entity.MarkdownFileVO;
@@ -52,7 +53,11 @@ public class MarkdownFileServiceImpl
@Override
public boolean deleteMarkdownFile(Long id) {
return this.removeById(id);
LambdaUpdateWrapper<MarkdownFile> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(MarkdownFile::getId, id)
.set(MarkdownFile::getIsDeleted, 1)
.set(MarkdownFile::getDeletedAt, new Date());
return this.update(updateWrapper);
}
@Override

View File

@@ -26,13 +26,13 @@ public class TrashServiceImpl implements TrashService {
@Override
public List<TrashItemVo> getTrashItems() {
// 查询已删除的笔记
List<TrashItemVo> deletedNotes = markdownFileMapper.selectList(new QueryWrapper<MarkdownFile>().eq("is_deleted", 1))
// 调用自定义的Mapper方法查询已删除的笔记绕过全局逻辑删除过滤器
List<TrashItemVo> deletedNotes = markdownFileMapper.selectDeleted()
.stream()
.map(file -> {
TrashItemVo vo = new TrashItemVo();
vo.setId(String.valueOf(file.getId()));
vo.setName(file.getTitle());
vo.setTitle(file.getTitle());
vo.setType("note");
vo.setDeletedAt(file.getDeletedAt());
vo.setDeletedBy(String.valueOf(file.getDeletedBy()));
@@ -40,13 +40,13 @@ public class TrashServiceImpl implements TrashService {
})
.collect(Collectors.toList());
// 查询已删除的分组
List<TrashItemVo> deletedGroups = groupingMapper.selectList(new QueryWrapper<Grouping>().eq("is_deleted", 1))
// 调用自定义的Mapper方法查询已删除的分组
List<TrashItemVo> deletedGroups = groupingMapper.selectDeleted()
.stream()
.map(group -> {
TrashItemVo vo = new TrashItemVo();
vo.setId(String.valueOf(group.getId()));
vo.setName(group.getGrouping());
vo.setTitle(group.getGrouping());
vo.setType("group");
vo.setDeletedAt(group.getDeletedAt());
vo.setDeletedBy(String.valueOf(group.getDeletedBy()));