ソースを参照

feat:完成了一些交流的功能

DingXiaoYu 3 年 前
コミット
3421a9f9a3

+ 4 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/repository/CommunicationRepository.java

@@ -1,6 +1,7 @@
 package cn.seecoder.seecoderportalserver.repository;
 
 import cn.seecoder.seecoderportalserver.domain.Communication;
+import org.apache.ibatis.annotations.Select;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.JpaRepository;
@@ -14,4 +15,7 @@ public interface CommunicationRepository extends JpaRepository<Communication,Lon
 
 
     Page<Communication> findByPublisherId(Long publisherId,Pageable pageable);
+
+    @Select("select * from communication A where exist (select * from communication_star B where A.id=B.communicationId and B.userId=${userId})")
+    Page<Communication> findByStarUserId(Long userId, Pageable pageable);
 }

+ 7 - 1
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/repository/CommunicationStarRepository.java

@@ -1,15 +1,21 @@
 package cn.seecoder.seecoderportalserver.repository;
 
 import cn.seecoder.seecoderportalserver.domain.CommunicationStar;
+import org.springframework.data.jpa.repository.JpaRepository;
 
+import java.util.List;
 import java.util.Optional;
 
 /**
  * @author fanyanpeng
  * @date 2023/7/12 16:06
  */
-public interface CommunicationStarRepository {
+public interface CommunicationStarRepository extends JpaRepository<CommunicationStar,Long> {
 
 
     Optional<CommunicationStar> findByUserIdAndCommunicationId(Long userId, Long id);
+
+    void deleteByUserIdAndCommunicationId(Long userId,Long communicationId);
+
+    List<CommunicationStar> findByCommunicationId(Long communicationId);
 }

+ 31 - 5
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/serviceImpl/CommunicationServiceImpl.java

@@ -6,10 +6,14 @@ import cn.seecoder.seecoderportalcommon.vo.CommunicationVO;
 import cn.seecoder.seecoderportalcommon.vo.PageVO;
 import cn.seecoder.seecoderportalcommon.vo.UserVO;
 import cn.seecoder.seecoderportalserver.domain.Communication;
+import cn.seecoder.seecoderportalserver.domain.CommunicationStar;
 import cn.seecoder.seecoderportalserver.mapper.CommunicationFileMapper;
 import cn.seecoder.seecoderportalserver.mapper.CommunicationMapper;
+import cn.seecoder.seecoderportalserver.mapper.UserMapper;
 import cn.seecoder.seecoderportalserver.repository.CommunicationRepository;
+import cn.seecoder.seecoderportalserver.repository.CommunicationStarRepository;
 import cn.seecoder.seecoderportalserver.service.CommunicationService;
+import cn.seecoder.seecoderportalserver.service.UserService;
 import cn.seecoder.seecoderportalserver.utility.OssUtil;
 import cn.seecoder.seecoderportalserver.web.rest.errors.CustomErrorException;
 import org.springframework.data.domain.Page;
@@ -25,6 +29,7 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 /**
  * @author fanyanpeng
@@ -37,14 +42,23 @@ public class CommunicationServiceImpl implements CommunicationService {
     private final CommunicationMapper communicationMapper;
     private final CommunicationFileMapper communicationFileMapper;
 
+    private final CommunicationStarRepository communicationStarRepository;
+
     private final OssUtil ossUtil;
 
+    private final UserService userService;
+
+    private final UserMapper userMapper;
+
 
-    public CommunicationServiceImpl(CommunicationRepository communicationRepository, CommunicationMapper communicationMapper, CommunicationFileMapper communicationFileMapper, OssUtil ossUtil){
+    public CommunicationServiceImpl(CommunicationRepository communicationRepository, CommunicationMapper communicationMapper, CommunicationFileMapper communicationFileMapper, CommunicationStarRepository communicationStarRepository, OssUtil ossUtil, UserService userService, UserMapper userMapper){
         this.communicationRepository = communicationRepository;
         this.communicationMapper = communicationMapper;
         this.communicationFileMapper = communicationFileMapper;
+        this.communicationStarRepository = communicationStarRepository;
         this.ossUtil = ossUtil;
+        this.userService = userService;
+        this.userMapper = userMapper;
     }
 
 
@@ -160,22 +174,34 @@ public class CommunicationServiceImpl implements CommunicationService {
 
     @Override
     public Boolean starCommunication(Long communicationId, Long userId) {
-        return null;
+        if (communicationStarRepository.findByUserIdAndCommunicationId(userId,communicationId).isPresent()){
+            throw new CustomErrorException("已经收藏过");
+        }
+        CommunicationStar communicationStar=new CommunicationStar();
+        communicationStar.setCommunicationId(communicationId);
+        communicationStar.setUserId(userId);
+        communicationStarRepository.save(communicationStar);
+        return true;
     }
 
     @Override
     public Boolean cancelStarCommunication(Long communicationId, Long userId) {
-        return null;
+        communicationStarRepository.deleteByUserIdAndCommunicationId(userId,communicationId);
+        return true;
     }
 
     @Override
     public PageVO<CommunicationVO> getStarredCommunicationPage(Long userId, Integer page, Integer size) {
-        return null;
+        Pageable pageable = PageRequest.of(page,size);
+        Page<Communication> communications=communicationRepository.findByStarUserId(userId,pageable);
+        return toPageVO(communications,userId);
     }
 
     @Override
     public List<UserVO> starredUsers(Long communicationId) {
-        return null;
+        List<CommunicationStar> communicationStars=communicationStarRepository.findByCommunicationId(communicationId);
+        return communicationStars.stream()
+                .map(communicationStar->userService.findUserById(communicationStar.getUserId())).collect(Collectors.toList());
     }