Browse Source

feat: 添加mapper,page的转换流程

191250028 3 years ago
parent
commit
5a4f3d1505

+ 54 - 1
seecoder-portal-common/src/main/java/cn/seecoder/seecoderportalcommon/vo/PageVO.java

@@ -16,7 +16,7 @@ public class PageVO<T> {
     private List<T> content;
 
     @ApiModelProperty("总元素个数")
-    private Integer totalElements;
+    private Long totalElements;
 
     @ApiModelProperty("当前页数")
     private Integer pageNumber;
@@ -30,4 +30,57 @@ public class PageVO<T> {
     @ApiModelProperty("是否为最后一页")
     private Boolean last;
 
+    public List<T> getContent() {
+        return content;
+    }
+
+    public PageVO<T> setContent(List<T> content) {
+        this.content = content;
+        return this;
+    }
+
+    public Long getTotalElements() {
+        return totalElements;
+    }
+
+    public PageVO<T> setTotalElements(Long totalElements) {
+        this.totalElements = totalElements;
+        return this;
+    }
+
+    public Integer getPageNumber() {
+        return pageNumber;
+    }
+
+    public PageVO<T> setPageNumber(Integer pageNumber) {
+        this.pageNumber = pageNumber;
+        return this;
+    }
+
+    public Integer getNumberOfElements() {
+        return numberOfElements;
+    }
+
+    public PageVO<T> setNumberOfElements(Integer numberOfElements) {
+        this.numberOfElements = numberOfElements;
+        return this;
+    }
+
+    public Integer getTotalPages() {
+        return totalPages;
+    }
+
+    public PageVO<T> setTotalPages(Integer totalPages) {
+        this.totalPages = totalPages;
+        return this;
+    }
+
+    public Boolean getLast() {
+        return last;
+    }
+
+    public PageVO<T> setLast(Boolean last) {
+        this.last = last;
+        return this;
+    }
 }

+ 16 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/mapper/CommunicationMapper.java

@@ -0,0 +1,16 @@
+package cn.seecoder.seecoderportalserver.mapper;
+
+import cn.seecoder.seecoderportalcommon.vo.CommunicationVO;
+import cn.seecoder.seecoderportalserver.domain.Communication;
+
+/**
+ * @author fanyanpeng
+ * @date 2023/7/12 11:35
+ */
+public interface CommunicationMapper {
+
+    CommunicationVO toCommunicationVO(Communication communication);
+
+    Communication toCommunication(CommunicationVO communicationVO);
+
+}

+ 32 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/mapper/impl/CommunicationMapperImpl.java

@@ -0,0 +1,32 @@
+package cn.seecoder.seecoderportalserver.mapper.impl;
+
+import cn.seecoder.seecoderportalcommon.vo.CommunicationVO;
+import cn.seecoder.seecoderportalserver.domain.Communication;
+import cn.seecoder.seecoderportalserver.mapper.CommunicationMapper;
+import org.springframework.beans.BeanUtils;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author fanyanpeng
+ * @date 2023/7/12 11:40
+ */
+@Component
+public class CommunicationMapperImpl implements CommunicationMapper {
+
+    @Override
+    public CommunicationVO toCommunicationVO(Communication communication) {
+        CommunicationVO communicationVO = new CommunicationVO();
+        BeanUtils.copyProperties(communication,communicationVO);
+        return communicationVO;
+    }
+
+    @Override
+    public Communication toCommunication(CommunicationVO communicationVO) {
+        Communication communication = new Communication();
+        BeanUtils.copyProperties(communicationVO,communication);
+        //todo: 2023/7/12 未完成 补充file文件的逐个转换,补充收藏情况的验证
+
+
+        return communication;
+    }
+}

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

@@ -0,0 +1,16 @@
+package cn.seecoder.seecoderportalserver.repository;
+
+import cn.seecoder.seecoderportalserver.domain.Communication;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+/**
+ * @author fanyanpeng
+ * @date 2023/7/12 11:47
+ */
+public interface CommunicationRepository extends JpaRepository<Communication,Long> {
+
+
+    Page<Communication> findByPublisherId(Pageable pageable);
+}

+ 14 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/service/CommunicationService.java

@@ -1,8 +1,22 @@
 package cn.seecoder.seecoderportalserver.service;
 
+import cn.seecoder.seecoderportalcommon.vo.CommunicationVO;
+import cn.seecoder.seecoderportalcommon.vo.PageVO;
+import cn.seecoder.seecoderportalserver.utility.OkResponse;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestParam;
+
 /**
  * @author fanyanpeng
  * @date 2023/7/11 12:21
  */
 public interface CommunicationService {
+
+
+    PageVO<CommunicationVO> getCommunicationPageByUserId(String userId,
+                                                         Integer page,
+                                                         Integer size);
+
 }

+ 61 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/serviceImpl/CommunicationServiceImpl.java

@@ -1,10 +1,71 @@
 package cn.seecoder.seecoderportalserver.serviceImpl;
 
+import cn.seecoder.seecoderportalcommon.vo.CommunicationVO;
+import cn.seecoder.seecoderportalcommon.vo.PageVO;
+import cn.seecoder.seecoderportalserver.domain.Communication;
+import cn.seecoder.seecoderportalserver.mapper.CommunicationMapper;
+import cn.seecoder.seecoderportalserver.repository.CommunicationRepository;
 import cn.seecoder.seecoderportalserver.service.CommunicationService;
+import cn.seecoder.seecoderportalserver.utility.OkResponse;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * @author fanyanpeng
  * @date 2023/7/11 12:21
  */
+@Service
 public class CommunicationServiceImpl implements CommunicationService {
+
+
+    private final CommunicationRepository communicationRepository;
+    private final CommunicationMapper communicationMapper;
+
+    public CommunicationServiceImpl(CommunicationRepository communicationRepository, CommunicationMapper communicationMapper){
+        this.communicationRepository = communicationRepository;
+        this.communicationMapper = communicationMapper;
+    }
+
+
+    @Override
+    public PageVO<CommunicationVO> getCommunicationPageByUserId(String userId, Integer page, Integer size) {
+        Pageable pageable = PageRequest.of(page,size);
+        Page<Communication> communicationPage = communicationRepository.findByPublisherId(pageable);
+        return toPageVO(communicationPage);
+    }
+
+    /**
+     *
+     * @author   fanyanpeng
+     * @date 2023/7/12 12:00
+     * @param communicationPage
+     * @return cn.seecoder.seecoderportalcommon.vo.PageVO<cn.seecoder.seecoderportalcommon.vo.CommunicationVO>
+     */
+    private PageVO<CommunicationVO> toPageVO(Page<Communication> communicationPage){
+        PageVO<CommunicationVO> communicationVOPageVO = new PageVO<>();
+
+        //转换分页的内容
+        List<Communication> communicationList = communicationPage.getContent();
+        List<CommunicationVO> communicationVOList = new ArrayList<>();
+        for(Communication communication:communicationList){
+            communicationVOList.add(communicationMapper.toCommunicationVO(communication));
+        }
+        communicationVOPageVO.setContent(communicationVOList);
+
+        //设置分页的参数
+        communicationVOPageVO.setTotalElements(communicationPage.getTotalElements());
+        communicationVOPageVO.setPageNumber(communicationPage.getNumber());
+        communicationVOPageVO.setNumberOfElements(communicationPage.getSize());
+        communicationVOPageVO.setTotalPages(communicationPage.getTotalPages());
+
+        return communicationVOPageVO;
+    }
+
+
 }

+ 5 - 2
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/web/rest/CommunicationController.java

@@ -1,5 +1,6 @@
 package cn.seecoder.seecoderportalserver.web.rest;
 import cn.seecoder.seecoderportalcommon.enums.CommunicationSortStrategyEnum;
+import cn.seecoder.seecoderportalcommon.vo.CommunicationFileVO;
 import cn.seecoder.seecoderportalcommon.vo.CommunicationVO;
 import cn.seecoder.seecoderportalcommon.vo.PageVO;
 import cn.seecoder.seecoderportalcommon.vo.UserVO;
@@ -20,7 +21,7 @@ public class CommunicationController {
 
     @ApiOperation("上传交流文件")
     @PostMapping("/upload")
-    public OkResponse<UserVO> uploadCommunicationFile(@RequestParam MultipartFile file) {
+    public OkResponse<CommunicationFileVO> uploadCommunicationFile(@RequestParam MultipartFile file) {
         return null;
     }
 
@@ -67,7 +68,9 @@ public class CommunicationController {
 
     @ApiOperation("分页查看个人收藏夹")
     @GetMapping("/stars/users/{userId}")
-    public OkResponse<PageVO<CommunicationVO>> getStarredCommunicationPage(@PathVariable(value = "userId") String userId){
+    public OkResponse<PageVO<CommunicationVO>> getStarredCommunicationPage(@PathVariable(value = "userId")String userId,
+                                                                           @RequestParam(value = "page") Integer page,
+                                                                           @RequestParam(value = "size")Integer size){
         return null;
     }