Forráskód Böngészése

新增收藏接口

fangzhen 9 hónapja
szülő
commit
5dac458540

+ 38 - 2
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/CommunityArticleController.java

@@ -5,6 +5,7 @@ import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.generator.domain.Community.CommunityArticle;
+import com.ruoyi.generator.domain.Community.CommunityArticleCollect;
 import com.ruoyi.generator.domain.Community.CommunityLike;
 import com.ruoyi.generator.service.ICommunityArticleService;
 import com.ruoyi.generator.vo.CommunityArticleVo;
@@ -52,18 +53,53 @@ public class CommunityArticleController extends BaseController {
             AjaxResult.error("文章或用户信息异常!");
         }
         boolean isSuccess = true;
+        String message = "";
         CommunityLike like = communityArticleService.selectCommunityLikeById(communityLike);
         if (Objects.isNull(like)) {
             //新增点赞
             isSuccess = communityArticleService.insertCommunityLikeById(communityLike);
+            message = "感谢点赞";
         } else {
             //删除点赞
             isSuccess = communityArticleService.deleteCommunityLikeById(like);
+            message = "取消点赞";
         }
 
         if (!isSuccess) {
-            return AjaxResult.error("点赞失败!");
+            message = "操作失败!";
+            return AjaxResult.error(message);
         }
-        return AjaxResult.success("操作成功!");
+        return AjaxResult.success(message);
+    }
+
+
+    /**
+     * 获取文章列表信息
+     */
+    @ApiOperation("文章收藏")
+    @PostMapping("/collect")
+    //@Anonymous
+    public AjaxResult collect(@RequestBody CommunityArticleCollect communityArticleCollect) {
+        if (Objects.isNull(communityArticleCollect.getArticleId()) || Objects.isNull(communityArticleCollect.getUserId())) {
+            AjaxResult.error("文章或用户信息异常!");
+        }
+        boolean isSuccess = true;
+        String message = "";
+        CommunityArticleCollect collect = communityArticleService.selectCommunityCollectById(communityArticleCollect);
+        if (Objects.isNull(collect)) {
+            //新增收藏
+            isSuccess = communityArticleService.insertCommunityCollectById(collect);
+            message = "收藏成功";
+        } else {
+            //删除收藏
+            isSuccess = communityArticleService.deleteCommunityCollectById(collect);
+            message = "取消收藏";
+        }
+
+        if (!isSuccess) {
+            message = "操作失败!";
+            return AjaxResult.error(message);
+        }
+        return AjaxResult.success(message);
     }
 }

+ 25 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/community/CommunityArticleMapper.java

@@ -56,6 +56,31 @@ public interface CommunityArticleMapper {
      * @return 删除是否成功
      */
     boolean deleteCommunityLikeById(CommunityLike communityLike);
+
+    /**
+     * 查询该文章对应的用户是否已经收藏
+     *
+     * @param collect 收藏信息
+     * @return 收藏信息
+     */
+    CommunityArticleCollect selectCommunityCollectById(CommunityArticleCollect collect);
+
+    /**
+     * 新增收藏
+     *
+     * @param collect 收藏信息
+     * @return 收藏是否成功
+     */
+    boolean insertCommunityCollectById(CommunityArticleCollect collect);
+
+
+    /**
+     * 删除收藏
+     *
+     * @param collect 收藏信息
+     * @return 删除是否成功
+     */
+    boolean deleteCommunityCollectById(CommunityArticleCollect collect);
 }
 
 

+ 34 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/service/CommunityArticleServiceImpl.java

@@ -100,4 +100,38 @@ public class CommunityArticleServiceImpl implements ICommunityArticleService {
     public boolean deleteCommunityLikeById(CommunityLike communityLike) {
         return communityArticleMapper.deleteCommunityLikeById(communityLike);
     }
+
+
+    /**
+     * 查询该文章对应的用户是否已经收藏
+     *
+     * @param collect 收藏信息
+     * @return 收藏信息
+     */
+    @Override
+    public CommunityArticleCollect selectCommunityCollectById(CommunityArticleCollect collect) {
+        return null;
+    }
+
+    /**
+     * 新增收藏
+     *
+     * @param collect 收藏信息
+     * @return 收藏是否成功
+     */
+    @Override
+    public boolean insertCommunityCollectById(CommunityArticleCollect collect) {
+        return communityArticleMapper.insertCommunityCollectById(collect);
+    }
+
+    /**
+     * 删除收藏
+     *
+     * @param collect 收藏信息
+     * @return 删除是否成功
+     */
+    @Override
+    public boolean deleteCommunityCollectById(CommunityArticleCollect collect) {
+        return communityArticleMapper.deleteCommunityCollectById(collect);
+    }
 }

+ 25 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/service/ICommunityArticleService.java

@@ -1,6 +1,7 @@
 package com.ruoyi.generator.service;
 
 import com.ruoyi.generator.domain.Community.CommunityArticle;
+import com.ruoyi.generator.domain.Community.CommunityArticleCollect;
 import com.ruoyi.generator.domain.Community.CommunityLike;
 import com.ruoyi.generator.vo.CommunityArticleVo;
 
@@ -43,4 +44,28 @@ public interface ICommunityArticleService {
      * @return 删除是否成功
      */
     boolean deleteCommunityLikeById(CommunityLike communityLike);
+
+    /**
+     * 查询该文章对应的用户是否已经收藏
+     *
+     * @param collect 收藏信息
+     * @return 收藏信息
+     */
+    CommunityArticleCollect selectCommunityCollectById(CommunityArticleCollect collect);
+
+    /**
+     * 新增收藏
+     *
+     * @param collect 收藏信息
+     * @return 收藏是否成功
+     */
+    boolean insertCommunityCollectById(CommunityArticleCollect collect);
+
+    /**
+     * 删除收藏
+     *
+     * @param collect 收藏信息
+     * @return 删除是否成功
+     */
+    boolean deleteCommunityCollectById(CommunityArticleCollect collect);
 }

+ 27 - 0
ruoyi-generator/src/main/resources/mapper/community/ArticleMapper.xml

@@ -150,6 +150,7 @@
         </where>
     </select>
 
+    <!--点赞-->
     <select id="selectCommunityArticleLikeById" resultMap="CommunityLikeResult">
         select like_id, article_id, user_id, create_by, create_time, update_by, update_time from
         community_like
@@ -174,4 +175,30 @@
         where user_id = #{userId}
           and article_id = #{articleId};
     </delete>
+
+    <!--收藏-->
+    <select id="selectCommunityCollectById" resultMap="CommunityCollectResult">
+        select collect_id, article_id, user_id, create_by, create_time, update_by, update_time from
+        community_article_collect
+        <where>
+            <if test="userId != null and userId != ''">
+                user_id = #{userId}
+            </if>
+            <if test="articleId != null and articleId != ''">
+                and article_id = #{articleId}
+            </if>
+        </where>
+    </select>
+
+    <insert id="insertCommunityCollectById" parameterType="communityArticleCollect">
+        insert into community_article_collect (article_id, user_id, create_by, create_time, update_by, update_time)
+        values (#{articleId}, #{userId}, #{userId}, now(), #{userId}, now());
+    </insert>
+
+    <delete id="deleteCommunityCollectById" parameterType="communityArticleCollect">
+        delete
+        from community_article_collect
+        where user_id = #{userId}
+          and article_id = #{articleId};
+    </delete>
 </mapper>