feat(qianduan): 优化笔记功能和图片处理
- 新增批量删除图片接口和功能- 实现笔记中图片的上传和删除 - 优化笔记保存时的图片处理逻辑 -调整分组展示和Markdown文件加载方式
This commit is contained in:
@@ -4,6 +4,8 @@ import com.test.bijihoudaun.common.response.R;
|
||||
import com.test.bijihoudaun.entity.Grouping;
|
||||
import com.test.bijihoudaun.service.GroupingService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.Parameters;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -26,9 +28,16 @@ public class GroupingController {
|
||||
}
|
||||
|
||||
@Operation(summary = "获取全部分组")
|
||||
@Parameters({
|
||||
@Parameter(name = "parentId", description = "0是一级,其他的是看它的父级id", required = true)
|
||||
})
|
||||
@GetMapping
|
||||
public R<List<Grouping>> getAllGroupings() {
|
||||
List<Grouping> groupings = groupingService.getAllGroupings();
|
||||
public R<List<Grouping>> getAllGroupings(String parentId) {
|
||||
if (parentId == null) {
|
||||
return R.fail("参数不能为空");
|
||||
}
|
||||
long l = Long.parseLong(parentId);
|
||||
List<Grouping> groupings = groupingService.getAllGroupings(l);
|
||||
return R.success(groupings);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,17 +25,14 @@ public class ImageController {
|
||||
|
||||
@Operation(summary = "上传图片")
|
||||
@Parameters({
|
||||
@Parameter(name = "markdownId", description = "markdownid", required = true),
|
||||
@Parameter(name = "file", description = "图片文件", required = true)
|
||||
})
|
||||
@PostMapping
|
||||
public R<Image> uploadImage(
|
||||
@RequestParam(required = false) String markdownId,
|
||||
@RequestParam("file") MultipartFile file) {
|
||||
|
||||
try {
|
||||
Long markdownIdLong = Long.parseLong(markdownId);
|
||||
Image image = imageService.uploadImage(markdownIdLong, file);
|
||||
Image image = imageService.uploadImage(file);
|
||||
return R.success(image);
|
||||
} catch (IOException e) {
|
||||
return R.fail();
|
||||
@@ -43,7 +40,7 @@ public class ImageController {
|
||||
}
|
||||
|
||||
@Operation(summary = "根据id删除图片")
|
||||
@DeleteMapping("/{id}")
|
||||
@PostMapping("/{id}")
|
||||
public R<Void> deleteImage(@PathVariable Long id) {
|
||||
boolean result = imageService.deleteImage(id);
|
||||
if (result) {
|
||||
@@ -54,7 +51,7 @@ public class ImageController {
|
||||
}
|
||||
|
||||
@Operation(summary = "根据url删除图片")
|
||||
@DeleteMapping
|
||||
@PostMapping("/deleteByUrl")
|
||||
public R<Void> deleteImageByUrl(@RequestParam String url) {
|
||||
boolean result = imageService.deleteImageByUrl(url);
|
||||
if (result) {
|
||||
@@ -64,5 +61,16 @@ public class ImageController {
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "根据url批量删除图片")
|
||||
@PostMapping("/batch")
|
||||
public R<Void> deleteImageByUrls(@RequestBody List<String> urls) {
|
||||
boolean result = imageService.deleteImageByUrls(urls);
|
||||
if (result) {
|
||||
return R.success();
|
||||
} else {
|
||||
return R.fail();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -86,9 +86,13 @@ public class MarkdownController {
|
||||
}
|
||||
|
||||
@Operation(summary = "删除Markdown文件")
|
||||
@DeleteMapping("/{id}")
|
||||
public R<Void> deleteMarkdown(@PathVariable Long id) {
|
||||
if (markdownFileService.deleteMarkdownFile(id)) {
|
||||
@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)) {
|
||||
return R.success();
|
||||
}
|
||||
return R.fail();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.test.bijihoudaun.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
@@ -15,6 +16,12 @@ public class Grouping {
|
||||
@TableId(type = IdType.AUTO)
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING) // 仅作用于此字段
|
||||
private Long id;
|
||||
|
||||
@Schema(description ="上级id",implementation = Long.class)
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
@TableField("parentId")
|
||||
private Long parentId;
|
||||
|
||||
@Schema(description = "分组名称",implementation = String.class)
|
||||
private String grouping;
|
||||
}
|
||||
|
||||
@@ -5,8 +5,28 @@ import com.test.bijihoudaun.entity.Grouping;
|
||||
import java.util.List;
|
||||
|
||||
public interface GroupingService {
|
||||
/**
|
||||
* 创建分组
|
||||
* @param grouping
|
||||
* @return
|
||||
*/
|
||||
Grouping createGrouping(Grouping grouping);
|
||||
List<Grouping> getAllGroupings();
|
||||
|
||||
/**
|
||||
* 获取所有分组
|
||||
* @param parentId
|
||||
* @return
|
||||
*/
|
||||
List<Grouping> getAllGroupings(Long parentId);
|
||||
/**
|
||||
* 更新分组名称
|
||||
* @param grouping
|
||||
* @return
|
||||
*/
|
||||
Grouping updateGrouping(Grouping grouping);
|
||||
/**
|
||||
* 删除分组
|
||||
* @param id
|
||||
*/
|
||||
void deleteGrouping(Long id);
|
||||
}
|
||||
|
||||
@@ -10,12 +10,11 @@ import java.util.List;
|
||||
public interface ImageService extends IService<Image> {
|
||||
/**
|
||||
* 上传图片
|
||||
* @param markdownId Markdown文件ID(可选)
|
||||
* @param file 图片文件
|
||||
* @return 上传的图片对象
|
||||
* @throws IOException 文件操作异常
|
||||
*/
|
||||
Image uploadImage(Long markdownId, MultipartFile file) throws IOException;
|
||||
Image uploadImage(MultipartFile file) throws IOException;
|
||||
|
||||
/**
|
||||
* 删除图片
|
||||
@@ -24,14 +23,6 @@ public interface ImageService extends IService<Image> {
|
||||
*/
|
||||
boolean deleteImage(Long id);
|
||||
|
||||
|
||||
/**
|
||||
* 获取Markdown文件关联的图片
|
||||
* @param markdownId Markdown文件ID
|
||||
* @return 图片列表
|
||||
*/
|
||||
List<Image> getMarkdownImages(Long markdownId);
|
||||
|
||||
/**
|
||||
* 根据URL删除图片
|
||||
* @param url
|
||||
@@ -40,10 +31,9 @@ public interface ImageService extends IService<Image> {
|
||||
boolean deleteImageByUrl(String url);
|
||||
|
||||
/**
|
||||
* 根据URL批量更新图片ID
|
||||
* @param list
|
||||
* @param markdownId
|
||||
* 根据URL批量删除图片
|
||||
* @param urls
|
||||
* @return
|
||||
*/
|
||||
boolean updateImageId(List<String> list, Long markdownId);
|
||||
boolean deleteImageByUrls(List<String> urls);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package com.test.bijihoudaun.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.test.bijihoudaun.entity.Grouping;
|
||||
import com.test.bijihoudaun.mapper.GroupingMapper;
|
||||
import com.test.bijihoudaun.service.GroupingService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
@@ -13,6 +15,10 @@ public class GroupingServiceImpl
|
||||
extends ServiceImpl<GroupingMapper, Grouping>
|
||||
implements GroupingService {
|
||||
|
||||
@Resource
|
||||
private GroupingMapper groupingMapper;
|
||||
|
||||
|
||||
@Override
|
||||
public Grouping createGrouping(Grouping grouping) {
|
||||
this.save(grouping);
|
||||
@@ -20,8 +26,10 @@ public class GroupingServiceImpl
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Grouping> getAllGroupings() {
|
||||
return this.list();
|
||||
public List<Grouping> getAllGroupings(Long parentId) {
|
||||
// return groupingMapper.selectList(new LambdaQueryWrapper<Grouping>()
|
||||
// .eq(Grouping::getParentId, parentId));
|
||||
return groupingMapper.selectList(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -33,7 +33,7 @@ public class ImageServiceImpl
|
||||
private ImageMapper imageMapper;
|
||||
|
||||
@Override
|
||||
public Image uploadImage(Long markdownId, MultipartFile file) throws IOException {
|
||||
public Image uploadImage( MultipartFile file) throws IOException {
|
||||
// 创建上传目录
|
||||
Path uploadPath = Paths.get(uploadDir);
|
||||
if (!Files.exists(uploadPath)) {
|
||||
@@ -51,7 +51,6 @@ public class ImageServiceImpl
|
||||
|
||||
// 创建图片实体
|
||||
Image image = new Image();
|
||||
image.setMarkdownId(markdownId);
|
||||
image.setOriginalName(originalFilename);
|
||||
image.setStoredName(storedName);
|
||||
image.setUrl("/uploads/" + storedName);
|
||||
@@ -82,14 +81,6 @@ public class ImageServiceImpl
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Image> getMarkdownImages(Long markdownId) {
|
||||
QueryWrapper<Image> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("markdown_id", markdownId)
|
||||
.orderByDesc("created_at");
|
||||
return this.list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteImageByUrl(String url) {
|
||||
Image image = imageMapper.selectOne(new QueryWrapper<Image>().eq("url", url));
|
||||
@@ -108,17 +99,15 @@ public class ImageServiceImpl
|
||||
throw new RuntimeException("删除图片失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateImageId(List<String> list, Long markdownId) {
|
||||
if (CollUtil.isEmpty( list)) {
|
||||
public boolean deleteImageByUrls(List<String> urls) {
|
||||
if (CollUtil.isEmpty(urls)) {
|
||||
return false;
|
||||
}
|
||||
for (String url : list) {
|
||||
for (String url : urls) {
|
||||
Image image = imageMapper.selectOne(new QueryWrapper<Image>().eq("url", url));
|
||||
if (image != null) {
|
||||
image.setMarkdownId(markdownId);
|
||||
this.updateById(image);
|
||||
this.deleteImageByUrl(url);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user