feat(UserFollow):添加作者关注(好友)功能,可以对其它用户进行关注或取关,可以查询自己的关注列表和粉丝列表
Change-Id: Ibbab1104a9a605ac2e8b937693e5e8065d607a3d
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserFollowController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserFollowController.java
new file mode 100644
index 0000000..17d7ca1
--- /dev/null
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserFollowController.java
@@ -0,0 +1,44 @@
+// 作者关注控制器
+package com.ruoyi.web.controller.system;
+
+import com.ruoyi.common.core.controller.BaseController;
+import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.system.domain.SysUserFollow;
+import com.ruoyi.system.service.ISysUserFollowService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+
+@RestController
+@RequestMapping("/system/user/follow")
+public class SysUserFollowController extends BaseController {
+ @Autowired
+ private ISysUserFollowService followService;
+
+ @PreAuthorize("@ss.hasPermi('system:user:follow:add')")
+ @PostMapping
+ public AjaxResult follow(@RequestBody SysUserFollow follow) {
+ follow.setUserId(getUserId());
+ return toAjax(followService.followAuthor(follow));
+ }
+
+ @PreAuthorize("@ss.hasPermi('system:user:follow:remove')")
+ @DeleteMapping
+ public AjaxResult unfollow(@RequestBody SysUserFollow follow) {
+ follow.setUserId(getUserId());
+ return toAjax(followService.unfollowAuthor(follow));
+ }
+
+ @GetMapping("/list")
+ public AjaxResult list() {
+ return AjaxResult.success(followService.getFollowList(getUserId()));
+ }
+
+ @GetMapping("/isFollowing/{authorId}")
+ public AjaxResult isFollowing(@PathVariable Long authorId) {
+ SysUserFollow follow = new SysUserFollow();
+ follow.setUserId(getUserId());
+ follow.setAuthorId(authorId);
+ return AjaxResult.success(followService.isFollowing(follow));
+ }
+}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserFollow.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserFollow.java
new file mode 100644
index 0000000..a5878b1
--- /dev/null
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUserFollow.java
@@ -0,0 +1,23 @@
+// 作者关注
+package com.ruoyi.system.domain;
+
+import com.ruoyi.common.annotation.Excel;
+import com.ruoyi.common.core.domain.BaseEntity;
+import lombok.Data;
+import jakarta.validation.constraints.NotNull;
+
+@Data
+public class SysUserFollow extends BaseEntity {
+ private static final long serialVersionUID = 1L;
+
+ @Excel(name = "关注ID")
+ private Long followId;
+
+ @Excel(name = "用户ID")
+ @NotNull(message = "用户ID不能为空")
+ private Long userId;
+
+ @Excel(name = "作者ID")
+ @NotNull(message = "作者ID不能为空")
+ private Long authorId;
+}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserFollowMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserFollowMapper.java
new file mode 100644
index 0000000..77bca7e
--- /dev/null
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserFollowMapper.java
@@ -0,0 +1,12 @@
+// 作者关注 Mapper
+package com.ruoyi.system.mapper;
+
+import com.ruoyi.system.domain.SysUserFollow;
+import java.util.List;
+
+public interface SysUserFollowMapper {
+ int insertFollow(SysUserFollow follow);
+ int deleteFollow(SysUserFollow follow);
+ List<SysUserFollow> selectFollowListByUserId(Long userId);
+ SysUserFollow selectFollow(SysUserFollow follow);
+}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserFollowService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserFollowService.java
new file mode 100644
index 0000000..e79824e
--- /dev/null
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserFollowService.java
@@ -0,0 +1,12 @@
+// 作者关注服务接口
+package com.ruoyi.system.service;
+
+import com.ruoyi.system.domain.SysUserFollow;
+import java.util.List;
+
+public interface ISysUserFollowService {
+ int followAuthor(SysUserFollow follow);
+ int unfollowAuthor(SysUserFollow follow);
+ List<SysUserFollow> getFollowList(Long userId);
+ boolean isFollowing(SysUserFollow follow);
+}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserFollowServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserFollowServiceImpl.java
new file mode 100644
index 0000000..9cec124
--- /dev/null
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserFollowServiceImpl.java
@@ -0,0 +1,38 @@
+// 作者关注服务实现
+package com.ruoyi.system.service.impl;
+
+import com.ruoyi.system.domain.SysUserFollow;
+import com.ruoyi.system.mapper.SysUserFollowMapper;
+import com.ruoyi.system.service.ISysUserFollowService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import java.util.List;
+
+@Service
+public class SysUserFollowServiceImpl implements ISysUserFollowService {
+ @Autowired
+ private SysUserFollowMapper followMapper;
+
+ @Override
+ public int followAuthor(SysUserFollow follow) {
+ if (followMapper.selectFollow(follow) != null) {
+ return 0; // 已经关注
+ }
+ return followMapper.insertFollow(follow);
+ }
+
+ @Override
+ public int unfollowAuthor(SysUserFollow follow) {
+ return followMapper.deleteFollow(follow);
+ }
+
+ @Override
+ public List<SysUserFollow> getFollowList(Long userId) {
+ return followMapper.selectFollowListByUserId(userId);
+ }
+
+ @Override
+ public boolean isFollowing(SysUserFollow follow) {
+ return followMapper.selectFollow(follow) != null;
+ }
+}
\ No newline at end of file
diff --git a/ruoyi-system/src/main/resources/mapper/system/SysUserFollowMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysUserFollowMapper.xml
new file mode 100644
index 0000000..6fb9169
--- /dev/null
+++ b/ruoyi-system/src/main/resources/mapper/system/SysUserFollowMapper.xml
@@ -0,0 +1,31 @@
+<?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.ruoyi.system.mapper.SysUserFollowMapper">
+ <resultMap id="SysUserFollowMap" type="com.ruoyi.system.domain.SysUserFollow">
+ <id property="followId" column="follow_id"/>
+ <result property="userId" column="user_id"/>
+ <result property="authorId" column="author_id"/>
+ <result property="createTime" column="create_time"/>
+ </resultMap>
+
+ <insert id="insertFollow" parameterType="com.ruoyi.system.domain.SysUserFollow">
+ insert into sys_user_follow (user_id, author_id, create_time)
+ values (#{userId}, #{authorId}, sysdate())
+ </insert>
+
+ <delete id="deleteFollow">
+ delete from sys_user_follow where user_id = #{userId} and author_id = #{authorId}
+ </delete>
+
+ <select id="selectFollowListByUserId" resultMap="SysUserFollowMap">
+ select follow_id, user_id, author_id, create_time
+ from sys_user_follow
+ where user_id = #{userId}
+ </select>
+
+ <select id="selectFollow" resultMap="SysUserFollowMap">
+ select follow_id, user_id, author_id, create_time
+ from sys_user_follow
+ where user_id = #{userId} and author_id = #{authorId}
+ </select>
+</mapper>
\ No newline at end of file