Ver código fonte

Merge branch 'dbs333' of http://wcollector.idatabank.com:5230/dbs333/RealWorld into dbs347

Gayeon Park 3 anos atrás
pai
commit
9915868db9

+ 31 - 4
realworld/src/main/java/com/dbs/realworld/controller/ArticleController.java

@@ -28,6 +28,7 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 
 @Controller
 @RequestMapping("/article")
@@ -172,12 +173,38 @@ public class ArticleController {
     }
 
 
-    @GetMapping("/all")
-    public ResponseEntity getArticles() {
-        List<ArticleDTO> articleDTOs = this.articleService.findAll();
+    // @GetMapping("/all")
+    // public ResponseEntity getAllArticles() {
+    //     List<ArticleDTO> articleDTOs = this.articleService.findAll();
+    //     Map<String, Object> data = new HashMap<>();
+    //     data.put("articles", articleDTOs);
+
+    //     return ResponseEntity.ok().body(data);
+    // }
+
+
+    /**
+     * 필요한 것들
+     * - feed: your-feed or global-feed(디폴트)
+     * - articleId:   cursor (디폴트 -1, -1인 경우 최초 데이터 조회)
+     */
+    @GetMapping("/page")
+    public ResponseEntity getArticles(HttpServletRequest request, 
+        @RequestParam(defaultValue = "global-feed") String feed, 
+        @RequestParam(defaultValue = "-1") int articleId) {
+        int userId = (int) request.getSession().getAttribute("ssId");
+        
+        List<ArticleDTO> articleDTOs = this.articleService.find(articleId, feed, userId);
+        // List<ArticleDTO> articleDTOs = this.articleService.find(articleId, feed, 10);
+        
         Map<String, Object> data = new HashMap<>();
         data.put("articles", articleDTOs);
 
+        final int size = articleDTOs.size();
+        int lastItemArticleId = (size != 0) ? articleDTOs.get(size - 1).getId() : -1;
+        data.put("paging", this.articleService.calculatePagingInfo(articleDTOs, lastItemArticleId, feed, userId));
+        // data.put("paging", this.articleService.calculatePagingInfo(articleDTOs, lastItemArticleId, feed, 10));
+        
         return ResponseEntity.ok().body(data);
     }
 
@@ -223,4 +250,4 @@ public class ArticleController {
         this.commentService.remove(userId, articleId, commentId);
         return ResponseEntity.ok().body("성공적으로 삭제했습니다.");
     }
-}
+}

+ 3 - 0
realworld/src/main/java/com/dbs/realworld/mapper/ArticleMapper.java

@@ -1,6 +1,7 @@
 package com.dbs.realworld.mapper;
 
 import java.util.List;
+import java.util.Map;
 
 import com.dbs.realworld.dto.ArticleDTO;
 
@@ -12,6 +13,8 @@ import org.springframework.stereotype.Repository;
 public interface ArticleMapper {
     void insertArticle(ArticleDTO dto);
     List<ArticleDTO> selectAll();
+    List<ArticleDTO> select(int articleId, String feed, int userId);
+    Map<String, Object> selectPagingInfo(int articleId, String feed, int userId);
     ArticleDTO selectByArticleId(int articleId);
     void deleteByArticleId(int articleId);
     int updateArticle(ArticleDTO dto);

+ 26 - 0
realworld/src/main/java/com/dbs/realworld/service/ArticleService.java

@@ -1,6 +1,7 @@
 package com.dbs.realworld.service;
 
 import java.util.List;
+import java.util.Map;
 
 import com.dbs.realworld.dto.ArticleDTO;
 import com.dbs.realworld.dto.CommentDTO;
@@ -33,6 +34,30 @@ public class ArticleService {
         return this.articleMapper.selectAll();
     }
 
+    /**
+     * 
+     * @param feed  feed 타입, 'your-feed', 'global-feed'
+     * @param id    cursor(articleId), 다음 페이지에 필요한 데이터를 위한 기준
+     */
+    public List<ArticleDTO>  find(int articleId, String feed, int userId) {
+        return this.articleMapper.select(articleId, feed, userId);
+    }
+
+    public Map<String, Object> calculatePagingInfo(List<ArticleDTO> articleDTOs, int articleId, String feed, int userId) {
+        Map<String, Object> paging = this.articleMapper.selectPagingInfo(articleId, feed, userId);
+        paging.put("size", articleDTOs.size());
+        if (articleId == -1) {
+            paging.put("isNext", false);
+            paging.put("firstId", null);
+            paging.put("lastId", null);
+        } else {
+            paging.put("isNext", ((Long) paging.get("nextItemNum")).intValue() > 0);
+            paging.put("firstId", articleDTOs.get(0).getId());
+            paging.put("lastId", articleDTOs.get(articleDTOs.size() - 1).getId());
+        }
+        return paging;
+    }
+
     public ArticleDTO findByArticleId(int articleId) {
         ArticleDTO articleDTO = this.articleMapper.selectByArticleId(articleId);
         List<CommentDTO> commentDTOs = this.commentService.findAllByArticleId(articleId);
@@ -40,6 +65,7 @@ public class ArticleService {
         return articleDTO;
     }
 
+
     public void remove(int articleId) {
         this.articleMapper.deleteByArticleId(articleId);
     }

+ 59 - 2
realworld/src/main/resources/mybatis/mapper/user/ArticleMapper.xml

@@ -33,7 +33,64 @@
         ]]>	
     </update>
 
-    
+    <!-- 
+      커서 기반 페이징 
+      - articleId는 커서 역할
+        - -1은 초깃값이며 이 경우 최초의 페이지 데이터들을 조회
+      - feed는 현재 사용자가 보고 있는 feed
+        - 'your-feed' 혹은 'global-feed' 값을 가지며 기본 값은 'global-feed'
+      - 시간별 최신 정렬 및 페이지 당 10개의 데이터 조회
+    -->
+    <select id="select" parameterType="hashmap" resultType="ArticleDTO">
+      <![CDATA[
+        SELECT
+          AR.id               AS id,
+          AR.title            AS title,
+          AR.subtitle         AS subtitle,
+          AR.content          AS content,
+          AR.tags             AS tags,
+          AR.create_datetime  AS created,
+          AR.user_id          AS writerId,
+          USR.name            AS writerName,
+          USR.email           AS writerEmail
+        FROM article_mst AR
+        LEFT JOIN user_mst USR
+        ON AR.user_id = USR.id
+        WHERE 1=1
+      ]]>
+      <if test='articleId != -1'>
+        <![CDATA[
+          AND AR.id < #{articleId}
+        ]]>
+      </if>
+      <if test='feed == "your-feed"'>
+        <![CDATA[
+          AND AR.user_id = #{userId}
+        ]]>
+      </if>
+      <![CDATA[
+        ORDER BY create_datetime DESC
+        LIMIT 10
+      ]]>
+    </select>
+
+
+     <select id="selectPagingInfo" parameterType="hashmap" resultType="hashmap">
+        <![CDATA[
+          SELECT 
+            COUNT(*)                                 AS total,
+            COUNT( IF(id < #{articleId}, 1, null) )  AS nextItemNum
+          FROM article_mst AR
+        ]]>
+        <if test='feed == "your-feed"'>
+          <![CDATA[
+            WHERE     AR.user_id = #{userId}
+            GROUP BY  AR.user_id
+          ]]>
+        </if>
+     </select>
+
+
     <select id="selectAll" parameterType="ArticleDTO" resultType="ArticleDTO">
       <![CDATA[
         SELECT
@@ -50,7 +107,7 @@
         LEFT JOIN user_mst USR
         ON AR.user_id = USR.id
         ORDER BY create_datetime DESC
-        LIMIT 30
+        LIMIT 10
       ]]>
     </select>