Compare commits

..

2 Commits

Author SHA1 Message Date
Hanserwei
3ce029ecce feat(user-relation): 初始化用户关系服务模块
- 新增用户关注和粉丝数据表结构及对应 DO、Mapper、Service 层代码
- 配置 RedisTemplate 以支持 JSON 格式的值序列化
- 添加全局异常处理器处理业务、参数校验等异常情况
- 完成模块基础配置文件(application.yml、bootstrap.yml)的初始化
- 在 SQL 脚本中新增 t_following 和 t_fans 表定义
- 更新 .gitignore、IDEA 编码设置以及 Maven 模块依赖关系
- 增加日志配置文件 logback-spring.xml 并区分环境输出方式
- 创建模块启动类 HannoteUserRelationBizApplication 并启用 MyBatis Mapper 扫描
2025-10-10 19:38:37 +08:00
Hanserwei
cede5282e8 feat(note): 新增笔记可见性与置顶功能
- 添加笔记仅对自己可见接口及实现逻辑
- 添加笔记置顶/取消置顶接口及实现逻辑
- 引入新的请求参数类 UpdateNoteVisibleOnlyMeReqVO 和 TopNoteReqVO
- 扩展响应码枚举以支持新功能的异常处理
- 使用通配符导入优化代码结构
- 更新服务层接口定义,增加对应方法声明
2025-10-10 18:57:24 +08:00
30 changed files with 783 additions and 29 deletions

1
.gitignore vendored
View File

@@ -49,3 +49,4 @@ build/
/han-note-kv/han-note-kv-biz/src/main/resources/application-prod.yml /han-note-kv/han-note-kv-biz/src/main/resources/application-prod.yml
/han-note-kv/han-note-kv-biz/logs/ /han-note-kv/han-note-kv-biz/logs/
/han-note-note/han-note-note-biz/src/main/resources/application-dev.yml /han-note-note/han-note-note-biz/src/main/resources/application-dev.yml
/han-note-user-relation/han-note-user-relation-biz/src/main/resources/application-dev.yml

6
.idea/encodings.xml generated
View File

@@ -29,6 +29,12 @@
<file url="file://$PROJECT_DIR$/han-note-oss/han-note-oss-biz/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/han-note-oss/han-note-oss-biz/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/han-note-oss/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/han-note-oss/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/han-note-oss/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/han-note-oss/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/han-note-user-relation/han-note-user-relation-api/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/han-note-user-relation/han-note-user-relation-api/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/han-note-user-relation/han-note-user-relation-biz/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/han-note-user-relation/han-note-user-relation-biz/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/han-note-user-relation/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/han-note-user-relation/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/han-note-user/han-note-user-api/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/han-note-user/han-note-user-api/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/han-note-user/han-note-user-api/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/han-note-user/han-note-user-api/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/han-note-user/han-note-user-biz/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/han-note-user/han-note-user-biz/src/main/java" charset="UTF-8" />

View File

@@ -2,10 +2,7 @@ package com.hanserwei.hannote.note.biz.controller;
import com.hanserwei.framework.biz.operationlog.aspect.ApiOperationLog; import com.hanserwei.framework.biz.operationlog.aspect.ApiOperationLog;
import com.hanserwei.framework.common.response.Response; import com.hanserwei.framework.common.response.Response;
import com.hanserwei.hannote.note.biz.model.vo.FindNoteDetailReqVO; import com.hanserwei.hannote.note.biz.model.vo.*;
import com.hanserwei.hannote.note.biz.model.vo.FindNoteDetailRspVO;
import com.hanserwei.hannote.note.biz.model.vo.PublishNoteReqVO;
import com.hanserwei.hannote.note.biz.model.vo.UpdateNoteReqVO;
import com.hanserwei.hannote.note.biz.service.NoteService; import com.hanserwei.hannote.note.biz.service.NoteService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -41,4 +38,16 @@ public class NoteController {
return noteService.updateNote(updateNoteReqVO); return noteService.updateNote(updateNoteReqVO);
} }
@PostMapping(value = "/visible/onlyme")
@ApiOperationLog(description = "笔记仅对自己可见")
public Response<?> visibleOnlyMe(@Validated @RequestBody UpdateNoteVisibleOnlyMeReqVO updateNoteVisibleOnlyMeReqVO) {
return noteService.visibleOnlyMe(updateNoteVisibleOnlyMeReqVO);
}
@PostMapping(value = "/top")
@ApiOperationLog(description = "置顶/取消置顶笔记")
public Response<?> topNote(@Validated @RequestBody TopNoteReqVO topNoteReqVO) {
return noteService.topNote(topNoteReqVO);
}
} }

View File

@@ -18,7 +18,9 @@ public enum ResponseCodeEnum implements BaseExceptionInterface {
NOTE_NOT_FOUND("NOTE-20002", "笔记不存在"), NOTE_NOT_FOUND("NOTE-20002", "笔记不存在"),
NOTE_PRIVATE("NOTE-20003", "作者已将该笔记设置为仅自己可见"), NOTE_PRIVATE("NOTE-20003", "作者已将该笔记设置为仅自己可见"),
NOTE_UPDATE_FAIL("NOTE-20004", "笔记更新失败"), NOTE_UPDATE_FAIL("NOTE-20004", "笔记更新失败"),
TOPIC_NOT_FOUND("NOTE-20005", "话题不存在") TOPIC_NOT_FOUND("NOTE-20005", "话题不存在"),
NOTE_CANT_VISIBLE_ONLY_ME("NOTE-20006", "此笔记无法修改为仅自己可见"),
NOTE_CANT_OPERATE("NOTE-20007", "您无法操作该笔记"),
; ;
// 异常码 // 异常码

View File

@@ -0,0 +1,20 @@
package com.hanserwei.hannote.note.biz.model.vo;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TopNoteReqVO {
@NotNull(message = "笔记 ID 不能为空")
private Long id;
@NotNull(message = "置顶状态不能为空")
private Boolean isTop;
}

View File

@@ -0,0 +1,18 @@
package com.hanserwei.hannote.note.biz.model.vo;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UpdateNoteVisibleOnlyMeReqVO {
@NotNull(message = "笔记 ID 不能为空")
private Long id;
}

View File

@@ -3,10 +3,7 @@ package com.hanserwei.hannote.note.biz.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.hanserwei.framework.common.response.Response; import com.hanserwei.framework.common.response.Response;
import com.hanserwei.hannote.note.biz.domain.dataobject.NoteDO; import com.hanserwei.hannote.note.biz.domain.dataobject.NoteDO;
import com.hanserwei.hannote.note.biz.model.vo.FindNoteDetailReqVO; import com.hanserwei.hannote.note.biz.model.vo.*;
import com.hanserwei.hannote.note.biz.model.vo.FindNoteDetailRspVO;
import com.hanserwei.hannote.note.biz.model.vo.PublishNoteReqVO;
import com.hanserwei.hannote.note.biz.model.vo.UpdateNoteReqVO;
public interface NoteService extends IService<NoteDO> { public interface NoteService extends IService<NoteDO> {
@@ -31,4 +28,18 @@ public interface NoteService extends IService<NoteDO> {
*/ */
Response<?> updateNote(UpdateNoteReqVO updateNoteReqVO); Response<?> updateNote(UpdateNoteReqVO updateNoteReqVO);
/**
* 笔记仅对自己可见
* @param updateNoteVisibleOnlyMeReqVO 笔记仅对自己可见请求
* @return 笔记仅对自己可见结果
*/
Response<?> visibleOnlyMe(UpdateNoteVisibleOnlyMeReqVO updateNoteVisibleOnlyMeReqVO);
/**
* 笔记置顶 / 取消置顶
* @param topNoteReqVO 笔记置顶 / 取消置顶请求
* @return 笔记置顶 / 取消置顶结果
*/
Response<?> topNote(TopNoteReqVO topNoteReqVO);
} }

View File

@@ -20,10 +20,7 @@ import com.hanserwei.hannote.note.biz.enums.NoteStatusEnum;
import com.hanserwei.hannote.note.biz.enums.NoteTypeEnum; import com.hanserwei.hannote.note.biz.enums.NoteTypeEnum;
import com.hanserwei.hannote.note.biz.enums.NoteVisibleEnum; import com.hanserwei.hannote.note.biz.enums.NoteVisibleEnum;
import com.hanserwei.hannote.note.biz.enums.ResponseCodeEnum; import com.hanserwei.hannote.note.biz.enums.ResponseCodeEnum;
import com.hanserwei.hannote.note.biz.model.vo.FindNoteDetailReqVO; import com.hanserwei.hannote.note.biz.model.vo.*;
import com.hanserwei.hannote.note.biz.model.vo.FindNoteDetailRspVO;
import com.hanserwei.hannote.note.biz.model.vo.PublishNoteReqVO;
import com.hanserwei.hannote.note.biz.model.vo.UpdateNoteReqVO;
import com.hanserwei.hannote.note.biz.rpc.DistributedIdGeneratorRpcService; import com.hanserwei.hannote.note.biz.rpc.DistributedIdGeneratorRpcService;
import com.hanserwei.hannote.note.biz.rpc.KeyValueRpcService; import com.hanserwei.hannote.note.biz.rpc.KeyValueRpcService;
import com.hanserwei.hannote.note.biz.rpc.UserRpcService; import com.hanserwei.hannote.note.biz.rpc.UserRpcService;
@@ -312,7 +309,7 @@ public class NoteServiceImpl extends ServiceImpl<NoteDOMapper, NoteDO> implement
NoteTypeEnum noteTypeEnum = NoteTypeEnum.valueOf(type); NoteTypeEnum noteTypeEnum = NoteTypeEnum.valueOf(type);
// 判断笔记类型,如果非图文、视频笔记,则抛出异常 // 判断笔记类型,如果非图文、视频笔记,则抛出异常
if (Objects.isNull(noteTypeEnum)){ if (Objects.isNull(noteTypeEnum)) {
throw new ApiException(ResponseCodeEnum.NOTE_TYPE_ERROR); throw new ApiException(ResponseCodeEnum.NOTE_TYPE_ERROR);
} }
String imgUris = null; String imgUris = null;
@@ -340,14 +337,14 @@ public class NoteServiceImpl extends ServiceImpl<NoteDOMapper, NoteDO> implement
// 话题 // 话题
Long topicId = updateNoteReqVO.getTopicId(); Long topicId = updateNoteReqVO.getTopicId();
String topicName = null; String topicName = null;
if (Objects.nonNull(topicId)){ if (Objects.nonNull(topicId)) {
TopicDO topicDO = topicDOService.getById(topicId); TopicDO topicDO = topicDOService.getById(topicId);
if (Objects.isNull(topicDO)){ if (Objects.isNull(topicDO)) {
throw new ApiException(ResponseCodeEnum.TOPIC_NOT_FOUND); throw new ApiException(ResponseCodeEnum.TOPIC_NOT_FOUND);
} }
topicName = topicDO.getName(); topicName = topicDO.getName();
// 判断提交的话题是否真实存在 // 判断提交的话题是否真实存在
if (StringUtils.isBlank(topicName)){ if (StringUtils.isBlank(topicName)) {
throw new ApiException(ResponseCodeEnum.TOPIC_NOT_FOUND); throw new ApiException(ResponseCodeEnum.TOPIC_NOT_FOUND);
} }
} }
@@ -370,7 +367,7 @@ public class NoteServiceImpl extends ServiceImpl<NoteDOMapper, NoteDO> implement
.videoUri(videoUri) .videoUri(videoUri)
.build(); .build();
boolean updateResult = this.updateById(noteDO); boolean updateResult = this.updateById(noteDO);
if (!updateResult){ if (!updateResult) {
throw new ApiException(ResponseCodeEnum.NOTE_UPDATE_FAIL); throw new ApiException(ResponseCodeEnum.NOTE_UPDATE_FAIL);
} }
@@ -431,6 +428,59 @@ public class NoteServiceImpl extends ServiceImpl<NoteDOMapper, NoteDO> implement
return Response.success(); return Response.success();
} }
@Override
public Response<?> visibleOnlyMe(UpdateNoteVisibleOnlyMeReqVO updateNoteVisibleOnlyMeReqVO) {
// 笔记 ID
Long noteId = updateNoteVisibleOnlyMeReqVO.getId();
// 构建更新的实体类
NoteDO noteDO = NoteDO.builder()
.id(noteId)
.visible(NoteVisibleEnum.PRIVATE.getCode())
.updateTime(LocalDateTime.now())
.build();
// 仅仅更新status为1的数据
boolean updateResult = this.update(noteDO, new LambdaQueryWrapper<>(NoteDO.class).eq(NoteDO::getStatus, NoteStatusEnum.NORMAL.getCode()));
if (!updateResult) {
throw new ApiException(ResponseCodeEnum.NOTE_UPDATE_FAIL);
}
// 删除Redis缓存
String noteDetailRedisKey = RedisKeyConstants.buildNoteDetailKey(noteId);
redisTemplate.delete(noteDetailRedisKey);
// 同步广播模式 MQ将所有实例中的本地缓存都删除掉
rocketMQTemplate.syncSend(MQConstants.TOPIC_DELETE_NOTE_LOCAL_CACHE, noteId);
log.info("====> MQ笔记更新本地缓存发送成功...");
return Response.success();
}
@Override
public Response<?> topNote(TopNoteReqVO topNoteReqVO) {
Long noteId = topNoteReqVO.getId();
boolean isTop = topNoteReqVO.getIsTop();
//当前用户ID
Long currUserId = LoginUserContextHolder.getUserId();
NoteDO noteDO = NoteDO.builder()
.id(noteId)
.isTop(isTop)
.updateTime(LocalDateTime.now())
.creatorId(currUserId) // 只有笔记所有者,才能置顶/取消置顶笔记
.build();
boolean isUpdated = this.update(noteDO, new LambdaQueryWrapper<>(NoteDO.class).eq(NoteDO::getId, noteId).eq(NoteDO::getCreatorId, currUserId));
if (!isUpdated) {
throw new ApiException(ResponseCodeEnum.NOTE_UPDATE_FAIL);
}
// 删除Redis缓存
String noteDetailRedisKey = RedisKeyConstants.buildNoteDetailKey(noteId);
redisTemplate.delete(noteDetailRedisKey);
// 同步广播模式 MQ将所有实例中的本地缓存都删除掉
rocketMQTemplate.syncSend(MQConstants.TOPIC_DELETE_NOTE_LOCAL_CACHE, noteId);
log.info("====> 笔记置顶更新,本地缓存发送成功...");
return Response.success();
}
/** /**
* 校验笔记的可见性 * 校验笔记的可见性
* *

View File

@@ -0,0 +1,25 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 指定父项目 -->
<parent>
<groupId>com.hanserwei</groupId>
<artifactId>han-note-user-relation</artifactId>
<version>${revision}</version>
</parent>
<!-- 打包方式 -->
<packaging>jar</packaging>
<artifactId>han-note-user-relation-api</artifactId>
<name>${project.artifactId}</name>
<description>RPC层, 供其他服务调用</description>
<dependencies>
<dependency>
<groupId>com.hanserwei</groupId>
<artifactId>hanserwei-common</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,103 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 指定父项目 -->
<parent>
<groupId>com.hanserwei</groupId>
<artifactId>han-note-user-relation</artifactId>
<version>${revision}</version>
</parent>
<!-- 打包方式 -->
<packaging>jar</packaging>
<artifactId>han-note-user-relation-biz</artifactId>
<name>${project.artifactId}</name>
<description>用户关系服务业务模块</description>
<dependencies>
<dependency>
<groupId>com.hanserwei</groupId>
<artifactId>hanserwei-common</artifactId>
</dependency>
<!-- 业务接口日志组件 -->
<dependency>
<groupId>com.hanserwei</groupId>
<artifactId>hanserwei-spring-boot-starter-biz-operationlog</artifactId>
</dependency>
<!-- 上下文组件 -->
<dependency>
<groupId>com.hanserwei</groupId>
<artifactId>hanserwei-spring-boot-starter-biz-context</artifactId>
</dependency>
<!-- Jackson 组件 -->
<dependency>
<groupId>com.hanserwei</groupId>
<artifactId>hanserwei-spring-boot-starter-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!-- 服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- Druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
</dependency>
<!-- 提供 Redis 连接池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,31 @@
package com.hanserwei.hannote.user.relation.biz.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisTemplateConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 设置 RedisTemplate 的连接工厂
redisTemplate.setConnectionFactory(connectionFactory);
// 使用 StringRedisSerializer 来序列化和反序列化 redis 的 key 值,确保 key 是可读的字符串
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
// 使用 Jackson2JsonRedisSerializer 来序列化和反序列化 redis 的 value 值, 确保存储的是 JSON 格式
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
redisTemplate.setValueSerializer(serializer);
redisTemplate.setHashValueSerializer(serializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}

View File

@@ -0,0 +1,13 @@
package com.hanserwei.hannote.user.relation.biz.domain;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.hanserwei.hannote.user.relation.biz.domain.mapper")
public class HannoteUserRelationBizApplication {
public static void main(String[] args) {
SpringApplication.run(HannoteUserRelationBizApplication.class, args);
}
}

View File

@@ -0,0 +1,46 @@
package com.hanserwei.hannote.user.relation.biz.domain.dataobject;
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 lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
/**
* 用户粉丝表
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "t_fans")
public class FansDO {
/**
* 主键ID
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 用户ID
*/
@TableField(value = "user_id")
private Long userId;
/**
* 粉丝的用户ID
*/
@TableField(value = "fans_user_id")
private Long fansUserId;
/**
* 创建时间
*/
@TableField(value = "create_time")
private LocalDateTime createTime;
}

View File

@@ -0,0 +1,46 @@
package com.hanserwei.hannote.user.relation.biz.domain.dataobject;
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 lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
/**
* 用户关注表
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "t_following")
public class FollowingDO {
/**
* 主键ID
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 用户ID
*/
@TableField(value = "user_id")
private Long userId;
/**
* 关注的用户ID
*/
@TableField(value = "following_user_id")
private Long followingUserId;
/**
* 创建时间
*/
@TableField(value = "create_time")
private LocalDateTime createTime;
}

View File

@@ -0,0 +1,9 @@
package com.hanserwei.hannote.user.relation.biz.domain.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hanserwei.hannote.user.relation.biz.domain.dataobject.FansDO;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface FansDOMapper extends BaseMapper<FansDO> {
}

View File

@@ -0,0 +1,9 @@
package com.hanserwei.hannote.user.relation.biz.domain.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hanserwei.hannote.user.relation.biz.domain.dataobject.FollowingDO;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface FollowingDOMapper extends BaseMapper<FollowingDO> {
}

View File

@@ -0,0 +1,23 @@
package com.hanserwei.hannote.user.relation.biz.enums;
import com.hanserwei.framework.common.exception.BaseExceptionInterface;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum ResponseCodeEnum implements BaseExceptionInterface {
// ----------- 通用异常状态码 -----------
SYSTEM_ERROR("RELATION-10000", "出错啦,后台小哥正在努力修复中..."),
PARAM_NOT_VALID("RELATION-10001", "参数错误"),
// ----------- 业务异常状态码 -----------
;
// 异常码
private final String errorCode;
// 错误信息
private final String errorMsg;
}

View File

@@ -0,0 +1,103 @@
package com.hanserwei.hannote.user.relation.biz.exception;
import com.hanserwei.framework.common.exception.ApiException;
import com.hanserwei.framework.common.response.Response;
import com.hanserwei.hannote.user.relation.biz.enums.ResponseCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Optional;
@SuppressWarnings("LoggingSimilarMessage")
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/**
* 捕获自定义业务异常
*
* @return Response.fail(e)
*/
@ExceptionHandler({ApiException.class})
@ResponseBody
public Response<Object> handleApiException(HttpServletRequest request, ApiException e) {
log.warn("{} request fail, errorCode: {}, errorMessage: {}", request.getRequestURI(), e.getErrorCode(), e.getErrorMsg());
return Response.fail(e);
}
/**
* 捕获参数校验异常
*
* @return Response.fail(errorCode, errorMessage)
*/
@ExceptionHandler({MethodArgumentNotValidException.class})
@ResponseBody
public Response<Object> handleMethodArgumentNotValidException(HttpServletRequest request, MethodArgumentNotValidException e) {
// 参数错误异常码
String errorCode = ResponseCodeEnum.PARAM_NOT_VALID.getErrorCode();
// 获取 BindingResult
BindingResult bindingResult = e.getBindingResult();
StringBuilder sb = new StringBuilder();
// 获取校验不通过的字段,并组合错误信息,格式为: email 邮箱格式不正确, 当前值: '123124qq.com';
Optional.of(bindingResult.getFieldErrors()).ifPresent(errors -> {
errors.forEach(error ->
sb.append(error.getField())
.append(" ")
.append(error.getDefaultMessage())
.append(", 当前值: '")
.append(error.getRejectedValue())
.append("'; ")
);
});
// 错误信息
String errorMessage = sb.toString();
log.warn("{} request error, errorCode: {}, errorMessage: {}", request.getRequestURI(), errorCode, errorMessage);
return Response.fail(errorCode, errorMessage);
}
/**
* 捕获 guava 参数校验异常
*
* @return Response.fail(ResponseCodeEnum.PARAM_NOT_VALID)
*/
@ExceptionHandler({IllegalArgumentException.class})
@ResponseBody
public Response<Object> handleIllegalArgumentException(HttpServletRequest request, IllegalArgumentException e) {
// 参数错误异常码
String errorCode = ResponseCodeEnum.PARAM_NOT_VALID.getErrorCode();
// 错误信息
String errorMessage = e.getMessage();
log.warn("{} request error, errorCode: {}, errorMessage: {}", request.getRequestURI(), errorCode, errorMessage);
return Response.fail(errorCode, errorMessage);
}
/**
* 其他类型异常
*
* @param request 请求
* @param e 异常
* @return Response.fail(ResponseCodeEnum.SYSTEM_ERROR)
*/
@ExceptionHandler({Exception.class})
@ResponseBody
public Response<Object> handleOtherException(HttpServletRequest request, Exception e) {
log.error("{} request error, ", request.getRequestURI(), e);
return Response.fail(ResponseCodeEnum.SYSTEM_ERROR);
}
}

View File

@@ -0,0 +1,8 @@
package com.hanserwei.hannote.user.relation.biz.service;
import com.hanserwei.hannote.user.relation.biz.domain.dataobject.FansDO;
import com.baomidou.mybatisplus.extension.service.IService;
public interface FansDOService extends IService<FansDO>{
}

View File

@@ -0,0 +1,8 @@
package com.hanserwei.hannote.user.relation.biz.service;
import com.hanserwei.hannote.user.relation.biz.domain.dataobject.FollowingDO;
import com.baomidou.mybatisplus.extension.service.IService;
public interface FollowingDOService extends IService<FollowingDO>{
}

View File

@@ -0,0 +1,13 @@
package com.hanserwei.hannote.user.relation.biz.service.impl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanserwei.hannote.user.relation.biz.domain.dataobject.FansDO;
import com.hanserwei.hannote.user.relation.biz.domain.mapper.FansDOMapper;
import com.hanserwei.hannote.user.relation.biz.service.FansDOService;
@Service
public class FansDOServiceImpl extends ServiceImpl<FansDOMapper, FansDO> implements FansDOService{
}

View File

@@ -0,0 +1,13 @@
package com.hanserwei.hannote.user.relation.biz.service.impl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanserwei.hannote.user.relation.biz.domain.mapper.FollowingDOMapper;
import com.hanserwei.hannote.user.relation.biz.domain.dataobject.FollowingDO;
import com.hanserwei.hannote.user.relation.biz.service.FollowingDOService;
@Service
public class FollowingDOServiceImpl extends ServiceImpl<FollowingDOMapper, FollowingDO> implements FollowingDOService{
}

View File

@@ -0,0 +1,31 @@
server:
port: 8089 # 项目启动的端口
spring:
profiles:
active: dev # 默认激活 dev 本地开发环境
servlet:
multipart:
max-file-size: 20MB # 单个文件最大大小
max-request-size: 100MB # 单次请求最大大小(包含多个文件)
data:
redis:
database: 5 # Redis 数据库索引(默认为 0
host: 127.0.0.1 # Redis 服务器地址
port: 6379 # Redis 服务器连接端口
password: redis # Redis 服务器连接密码(默认为空)
timeout: 5s # 读超时时间
connect-timeout: 5s # 链接超时时间
lettuce:
pool:
max-active: 200 # 连接池最大连接数
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
min-idle: 0 # 连接池中的最小空闲连接
max-idle: 10 # 连接池中的最大空闲连接
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
global-config:
banner: false
mapper-locations: classpath*:/mapperxml/*.xml

View File

@@ -0,0 +1,12 @@
spring:
application:
name: han-note-user-relation # 应用名称
profiles:
active: dev # 默认激活 dev 本地开发环境
cloud:
nacos:
discovery:
enabled: true # 启用服务发现
group: DEFAULT_GROUP # 所属组
namespace: han-note # 命名空间
server-addr: 127.0.0.1:8848 # 指定 Nacos 配置中心的服务器地址

View File

@@ -0,0 +1,58 @@
<configuration>
<!-- 引用 Spring Boot 的 logback 基础配置 -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<!-- 应用名称 -->
<property scope="context" name="appName" value="user-relation"/>
<!-- 自定义日志输出路径,以及日志名称前缀 -->
<property name="LOG_FILE" value="./logs/${appName}.%d{yyyy-MM-dd}"/>
<!-- 每行日志输出的格式 -->
<property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"/>
<!-- 文件输出 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 日志文件的命名格式 -->
<fileNamePattern>${LOG_FILE}-%i.log</fileNamePattern>
<!-- 保留 30 天的日志文件 -->
<maxHistory>30</maxHistory>
<!-- 单个日志文件最大大小 -->
<maxFileSize>10MB</maxFileSize>
<!-- 日志文件的总大小0 表示不限制 -->
<totalSizeCap>0</totalSizeCap>
<!-- 重启服务时,是否清除历史日志,不推荐清理 -->
<cleanHistoryOnStart>false</cleanHistoryOnStart>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 异步写入日志,提升性能 -->
<appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
<!-- 是否丢弃日志, 0 表示不丢弃。默认情况下,如果队列满 80%, 会丢弃 TRACE、DEBUG、INFO 级别的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 队列大小。默认值为 256 -->
<queueSize>256</queueSize>
<appender-ref ref="FILE"/>
</appender>
<!-- 本地 dev 开发环境 -->
<springProfile name="dev">
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/> <!-- 输出控制台日志 -->
<appender-ref ref="ASYNC_FILE"/> <!-- 打印日志到文件中。PS: 本地环境下,如果不想打印日志到文件,可注释掉此行 -->
</root>
</springProfile>
<!-- 其它环境 -->
<springProfile name="prod">
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="INFO">
<appender-ref ref="ASYNC_FILE"/> <!-- 生产环境下,仅打印日志到文件中 -->
</root>
</springProfile>
</configuration>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hanserwei.hannote.user.relation.biz.domain.mapper.FansDOMapper">
<resultMap id="BaseResultMap" type="com.hanserwei.hannote.user.relation.biz.domain.dataobject.FansDO">
<!--@mbg.generated-->
<!--@Table t_fans-->
<id column="id" jdbcType="BIGINT" property="id" />
<result column="user_id" jdbcType="BIGINT" property="userId" />
<result column="fans_user_id" jdbcType="BIGINT" property="fansUserId" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, user_id, fans_user_id, create_time
</sql>
</mapper>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hanserwei.hannote.user.relation.biz.domain.mapper.FollowingDOMapper">
<resultMap id="BaseResultMap" type="com.hanserwei.hannote.user.relation.biz.domain.dataobject.FollowingDO">
<!--@mbg.generated-->
<!--@Table t_following-->
<id column="id" jdbcType="BIGINT" property="id" />
<result column="user_id" jdbcType="BIGINT" property="userId" />
<result column="following_user_id" jdbcType="BIGINT" property="followingUserId" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, user_id, following_user_id, create_time
</sql>
</mapper>

View File

@@ -0,0 +1,26 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 指定父项目 -->
<parent>
<groupId>com.hanserwei</groupId>
<artifactId>han-note</artifactId>
<version>${revision}</version>
</parent>
<!-- 多模块项目需要配置打包方式为 pom -->
<packaging>pom</packaging>
<!-- 子模块管理 -->
<modules>
<module>han-note-user-relation-api</module>
<module>han-note-user-relation-biz</module>
</modules>
<artifactId>han-note-user-relation</artifactId>
<!-- 项目名称 -->
<name>${project.artifactId}</name>
<!-- 项目描述 -->
<description>用户关系服务</description>
</project>

View File

@@ -20,6 +20,7 @@
<module>han-note-distributed-id-generator</module> <module>han-note-distributed-id-generator</module>
<module>han-note-note</module> <module>han-note-note</module>
<module>han-note-note/han-note-note-biz</module> <module>han-note-note/han-note-note-biz</module>
<module>han-note-user-relation</module>
</modules> </modules>
<properties> <properties>

View File

@@ -87,7 +87,7 @@ CREATE TABLE `t_role_permission_rel`
-- 表t_channel -- 表t_channel
CREATE TABLE `t_channel` CREATE TABLE `t_channel`
( (
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID', `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '频道名称', `name` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '频道名称',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
@@ -100,7 +100,7 @@ CREATE TABLE `t_channel`
-- 表t_topic -- 表t_topic
CREATE TABLE `t_topic` CREATE TABLE `t_topic`
( (
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID', `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '话题名称', `name` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '话题名称',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
@@ -116,8 +116,8 @@ CREATE TABLE `t_channel_topic_rel`
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID', `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`channel_id` bigint unsigned NOT NULL COMMENT '频道ID', `channel_id` bigint unsigned NOT NULL COMMENT '频道ID',
`topic_id` bigint unsigned NOT NULL COMMENT '话题ID', `topic_id` bigint unsigned NOT NULL COMMENT '话题ID',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB ) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4 DEFAULT CHARSET = utf8mb4
@@ -126,20 +126,20 @@ CREATE TABLE `t_channel_topic_rel`
-- 表t_note -- 表t_note
CREATE TABLE `t_note` CREATE TABLE `t_note`
( (
`id` bigint unsigned NOT NULL COMMENT '主键ID', `id` bigint unsigned NOT NULL COMMENT '主键ID',
`title` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '标题', `title` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '标题',
`is_content_empty` bit(1) NOT NULL DEFAULT b'0' COMMENT '内容是否为空(0不为空 1空)', `is_content_empty` bit(1) NOT NULL DEFAULT b'0' COMMENT '内容是否为空(0不为空 1空)',
`creator_id` bigint unsigned NOT NULL COMMENT '发布者ID', `creator_id` bigint unsigned NOT NULL COMMENT '发布者ID',
`topic_id` bigint unsigned DEFAULT NULL COMMENT '话题ID', `topic_id` bigint unsigned DEFAULT NULL COMMENT '话题ID',
`topic_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '话题名称', `topic_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '话题名称',
`is_top` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否置顶(0未置顶 1置顶)', `is_top` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否置顶(0未置顶 1置顶)',
`type` tinyint DEFAULT '0' COMMENT '类型(0图文 1视频)', `type` tinyint DEFAULT '0' COMMENT '类型(0图文 1视频)',
`img_uris` varchar(660) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '笔记图片链接(逗号隔开)', `img_uris` varchar(660) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '笔记图片链接(逗号隔开)',
`video_uri` varchar(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '视频链接', `video_uri` varchar(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '视频链接',
`visible` tinyint DEFAULT '0' COMMENT '可见范围(0公开,所有人可见 1仅对自己可见)', `visible` tinyint DEFAULT '0' COMMENT '可见范围(0公开,所有人可见 1仅对自己可见)',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
`status` tinyint NOT NULL DEFAULT '0' COMMENT '状态(0待审核 1正常展示 2被删除(逻辑删除) 3被下架)', `status` tinyint NOT NULL DEFAULT '0' COMMENT '状态(0待审核 1正常展示 2被删除(逻辑删除) 3被下架)',
PRIMARY KEY (`id`) USING BTREE, PRIMARY KEY (`id`) USING BTREE,
KEY `idx_creator_id` (`creator_id`), KEY `idx_creator_id` (`creator_id`),
KEY `idx_topic_id` (`topic_id`), KEY `idx_topic_id` (`topic_id`),
@@ -148,4 +148,31 @@ CREATE TABLE `t_note`
DEFAULT CHARSET = utf8mb4 DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci COMMENT ='笔记表'; COLLATE = utf8mb4_unicode_ci COMMENT ='笔记表';
ALTER table t_note add column `content_uuid` varchar(36) DEFAULT '' COMMENT '笔记内容UUID'; ALTER table t_note
add column `content_uuid` varchar(36) DEFAULT '' COMMENT '笔记内容UUID';
CREATE TABLE `t_following`
(
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` bigint unsigned NOT NULL COMMENT '用户ID',
`following_user_id` bigint unsigned NOT NULL COMMENT '关注的用户ID',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE,
KEY `idx_user_id` (`user_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci COMMENT ='用户关注表';
CREATE TABLE `t_fans`
(
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` bigint unsigned NOT NULL COMMENT '用户ID',
`fans_user_id` bigint unsigned NOT NULL COMMENT '粉丝的用户ID',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE,
KEY `idx_user_id` (`user_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci COMMENT ='用户粉丝表';