feat(grouping): 新增分组功能并优化 Markdown 文件操作
- 新增分组实体、控制器、服务和映射器 - 实现分组创建、获取、更新和删除接口 - 优化 Markdown 文件创建、获取和删除接口- 新增全局异常处理和日志记录 - 更新数据库表结构和字段类型 - 重构前端页面,支持分组和 Markdown 文件展示
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.test.bijihoudaun.service;
|
||||
|
||||
import com.test.bijihoudaun.entity.Grouping;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface GroupingService {
|
||||
Grouping createGrouping(Grouping grouping);
|
||||
List<Grouping> getAllGroupings();
|
||||
Grouping updateGrouping(Grouping grouping);
|
||||
void deleteGrouping(Long id);
|
||||
}
|
||||
@@ -10,28 +10,20 @@ import java.util.List;
|
||||
public interface ImageService extends IService<Image> {
|
||||
/**
|
||||
* 上传图片
|
||||
* @param userId 用户ID
|
||||
* @param markdownId Markdown文件ID(可选)
|
||||
* @param file 图片文件
|
||||
* @return 上传的图片对象
|
||||
* @throws IOException 文件操作异常
|
||||
*/
|
||||
Image uploadImage(Long userId, Long markdownId, MultipartFile file) throws IOException;
|
||||
Image uploadImage(Long markdownId, MultipartFile file) throws IOException;
|
||||
|
||||
/**
|
||||
* 删除图片
|
||||
* @param id 图片ID
|
||||
* @param userId 用户ID(用于权限验证)
|
||||
* @return 是否删除成功
|
||||
*/
|
||||
boolean deleteImage(Long id, Long userId);
|
||||
boolean deleteImage(Long id);
|
||||
|
||||
/**
|
||||
* 获取用户的图片列表
|
||||
* @param userId 用户ID
|
||||
* @return 图片列表
|
||||
*/
|
||||
List<Image> getUserImages(Long userId);
|
||||
|
||||
/**
|
||||
* 获取Markdown文件关联的图片
|
||||
|
||||
@@ -8,13 +8,13 @@ import java.util.List;
|
||||
public interface MarkdownFileService extends IService<MarkdownFile> {
|
||||
/**
|
||||
* 创建Markdown文件
|
||||
* @param userId 用户ID
|
||||
* @param groupingId 分组ID
|
||||
* @param title 文件标题
|
||||
* @param fileName 文件名
|
||||
* @param content 文件内容
|
||||
* @return 创建的文件对象
|
||||
*/
|
||||
MarkdownFile createMarkdownFile(Long userId, String title, String fileName, String content);
|
||||
MarkdownFile createMarkdownFile(Long groupingId, String title, String fileName, String content);
|
||||
|
||||
/**
|
||||
* 更新Markdown内容
|
||||
@@ -31,10 +31,26 @@ public interface MarkdownFileService extends IService<MarkdownFile> {
|
||||
*/
|
||||
MarkdownFile getMarkdownById(Long id);
|
||||
|
||||
|
||||
/**
|
||||
* 获取用户的所有Markdown文件
|
||||
* @param userId 用户ID
|
||||
* 根据分组ID获取Markdown文件
|
||||
* @param groupingId 分组ID
|
||||
* @return 文件列表
|
||||
*/
|
||||
List<MarkdownFile> getUserFiles(Long userId);
|
||||
List<MarkdownFile> getFilesByGroupingId(String groupingId);
|
||||
|
||||
/**
|
||||
* 删除Markdown文件
|
||||
* @param id 文件ID
|
||||
* @return 是否删除成功
|
||||
*/
|
||||
boolean deleteMarkdownFile(Long id);
|
||||
|
||||
List<MarkdownFile> test();
|
||||
|
||||
/**
|
||||
* 获取所有Markdown文件
|
||||
* @return 文件列表
|
||||
*/
|
||||
List<MarkdownFile> getAllMarkdownFiles();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.test.bijihoudaun.service.impl;
|
||||
|
||||
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 org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class GroupingServiceImpl
|
||||
extends ServiceImpl<GroupingMapper, Grouping>
|
||||
implements GroupingService {
|
||||
|
||||
@Override
|
||||
public Grouping createGrouping(Grouping grouping) {
|
||||
this.save(grouping);
|
||||
return grouping;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Grouping> getAllGroupings() {
|
||||
return this.list();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Grouping updateGrouping(Grouping grouping) {
|
||||
this.updateById(grouping);
|
||||
return grouping;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteGrouping(Long id) {
|
||||
this.removeById(id);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.test.bijihoudaun.entity.Image;
|
||||
import com.test.bijihoudaun.mapper.ImageMapper;
|
||||
import com.test.bijihoudaun.service.ImageService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -15,6 +16,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -25,9 +27,11 @@ public class ImageServiceImpl
|
||||
|
||||
@Value("${file.upload-dir}")
|
||||
private String uploadDir;
|
||||
@Resource
|
||||
private ImageMapper imageMapper;
|
||||
|
||||
@Override
|
||||
public Image uploadImage(Long userId, Long markdownId, MultipartFile file) throws IOException {
|
||||
public Image uploadImage(Long markdownId, MultipartFile file) throws IOException {
|
||||
// 创建上传目录
|
||||
Path uploadPath = Paths.get(uploadDir);
|
||||
if (!Files.exists(uploadPath)) {
|
||||
@@ -45,30 +49,24 @@ public class ImageServiceImpl
|
||||
|
||||
// 创建图片实体
|
||||
Image image = new Image();
|
||||
image.setUserId(userId);
|
||||
image.setMarkdownId(markdownId);
|
||||
image.setOriginalName(originalFilename);
|
||||
image.setStoredName(storedName);
|
||||
image.setUrl("/uploads/" + storedName);
|
||||
image.setSize(file.getSize());
|
||||
image.setContentType(file.getContentType());
|
||||
image.setCreatedAt(LocalDateTime.now());
|
||||
image.setCreatedAt(new Date());
|
||||
|
||||
this.save(image);
|
||||
return image;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteImage(Long id, Long userId) {
|
||||
QueryWrapper<Image> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("id", id)
|
||||
.eq("user_id", userId);
|
||||
|
||||
Image image = this.getOne(queryWrapper);
|
||||
public boolean deleteImage(Long id) {
|
||||
Image image = imageMapper.selectById(id);
|
||||
if (image == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
// 删除文件系统中的图片
|
||||
Path filePath = Paths.get(uploadDir, image.getStoredName());
|
||||
@@ -82,14 +80,6 @@ public class ImageServiceImpl
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Image> getUserImages(Long userId) {
|
||||
QueryWrapper<Image> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("user_id", userId)
|
||||
.orderByDesc("created_at");
|
||||
return this.list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Image> getMarkdownImages(Long markdownId) {
|
||||
QueryWrapper<Image> queryWrapper = new QueryWrapper<>();
|
||||
|
||||
@@ -6,10 +6,13 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.test.bijihoudaun.entity.MarkdownFile;
|
||||
import com.test.bijihoudaun.mapper.MarkdownFileMapper;
|
||||
import com.test.bijihoudaun.service.MarkdownFileService;
|
||||
import com.test.bijihoudaun.util.SnowflakeIdGenerator;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@@ -17,17 +20,23 @@ public class MarkdownFileServiceImpl
|
||||
extends ServiceImpl<MarkdownFileMapper, MarkdownFile>
|
||||
implements MarkdownFileService {
|
||||
|
||||
@Autowired
|
||||
MarkdownFileMapper markdownFileMapper;
|
||||
@Resource
|
||||
SnowflakeIdGenerator snowflakeIdGenerator;
|
||||
|
||||
@Override
|
||||
public MarkdownFile createMarkdownFile(Long userId, String title, String fileName, String content) {
|
||||
public MarkdownFile createMarkdownFile(Long groupingId, String title, String fileName, String content) {
|
||||
MarkdownFile file = new MarkdownFile();
|
||||
file.setUserId(userId);
|
||||
file.setId(snowflakeIdGenerator.nextId());
|
||||
file.setGroupingId(groupingId);
|
||||
file.setTitle(title);
|
||||
file.setFileName(fileName);
|
||||
file.setContent(content);
|
||||
file.setCreatedAt(LocalDateTime.now());
|
||||
file.setUpdatedAt(LocalDateTime.now());
|
||||
file.setCreatedAt(new Date());
|
||||
file.setUpdatedAt(new Date());
|
||||
|
||||
this.save(file);
|
||||
markdownFileMapper.insert(file);
|
||||
return file;
|
||||
}
|
||||
|
||||
@@ -36,7 +45,7 @@ public class MarkdownFileServiceImpl
|
||||
MarkdownFile file = this.getById(id);
|
||||
if (file != null) {
|
||||
file.setContent(content);
|
||||
file.setUpdatedAt(LocalDateTime.now());
|
||||
file.setUpdatedAt(new Date());
|
||||
this.updateById(file);
|
||||
}
|
||||
return file;
|
||||
@@ -47,11 +56,28 @@ public class MarkdownFileServiceImpl
|
||||
return this.getById(id);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<MarkdownFile> getUserFiles(Long userId) {
|
||||
public List<MarkdownFile> getFilesByGroupingId(String groupingId) {
|
||||
QueryWrapper<MarkdownFile> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("user_id", userId)
|
||||
queryWrapper.eq("grouping_id", groupingId)
|
||||
.orderByDesc("updated_at");
|
||||
return this.list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteMarkdownFile(Long id) {
|
||||
return this.removeById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MarkdownFile> test() {
|
||||
List<MarkdownFile> markdownFiles = markdownFileMapper.selectList(null);
|
||||
return markdownFiles;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MarkdownFile> getAllMarkdownFiles() {
|
||||
return markdownFileMapper.selectList(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
|
||||
@@ -36,7 +38,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
user.setUsername(username);
|
||||
user.setPassword(encrypt);
|
||||
user.setEmail(email);
|
||||
user.setCreatedAt(LocalDateTime.now());
|
||||
user.setCreatedAt(new Date());
|
||||
userMapper.insert(user);
|
||||
return user;
|
||||
}
|
||||
@@ -52,7 +54,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
}
|
||||
user.setToken(UuidV7.uuidNoHyphen());
|
||||
// 过期时间:当前时间+3天的时间
|
||||
user.setTokenEnddata(LocalDateTime.now().plusDays(3));
|
||||
// 修改时间计算方式
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.add(Calendar.DAY_OF_MONTH, 3); // 增加3天
|
||||
user.setTokenEnddata(calendar.getTime());
|
||||
userMapper.updateById(user);
|
||||
return user;
|
||||
}
|
||||
@@ -68,8 +73,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
queryWrapper.eq(User::getId, id)
|
||||
.eq(User::getToken, token);
|
||||
User user = getOne(queryWrapper);
|
||||
// 新增过期检查
|
||||
return user != null && LocalDateTime.now().isBefore(user.getTokenEnddata());
|
||||
|
||||
// 修改过期检查逻辑
|
||||
return user != null && new Date().before(user.getTokenEnddata());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user