whiteship 2013-12-18
CommentThread: find by commitId
@e45b15ac86467539ecc626e3c18ebd99b0f372f5
app/models/CommentThread.java
--- app/models/CommentThread.java
+++ app/models/CommentThread.java
@@ -1,8 +1,11 @@
 package models;
 
+import play.data.format.Formats;
+import play.data.validation.Constraints;
 import play.db.ebean.Model;
 
 import javax.persistence.*;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -13,6 +16,8 @@
 public class CommentThread extends Model {
 
     private static final long serialVersionUID = 1L;
+
+    public static final Finder<Long, CommentThread> find = new Finder<>(Long.class, CommentThread.class);
 
     @Id
     public Long id;
@@ -26,6 +31,17 @@
     @Enumerated(EnumType.STRING)
     public ThreadState state;
 
+    @Constraints.Required
+    @Formats.DateTime(pattern = "YYYY/MM/DD/hh/mm/ss")
+    public Date createdDate;
+
+    public static List<CommentThread> findByCommitId(String commitId) {
+        return find.where()
+                .eq("commitId", commitId)
+                .order().desc("createdDate")
+                .findList();
+    }
+
     enum ThreadState {
         OPEN, CLOSED;
     }
 
conf/evolutions/default/69.sql (added)
+++ conf/evolutions/default/69.sql
@@ -0,0 +1,7 @@
+# --- !Ups
+
+alter table comment_thread add created_date timestamp;
+
+# --- !Downs
+
+alter table comment_thread drop column created_date;
test/models/CommentThreadTest.java
--- test/models/CommentThreadTest.java
+++ test/models/CommentThreadTest.java
@@ -1,6 +1,11 @@
 package models;
 
 import org.junit.Test;
+import utils.JodaDateUtil;
+
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
 
 /**
  * @author Keesun Baik
@@ -14,4 +19,63 @@
         thread.save();
     }
 
+    @Test
+    public void findByCommitId() {
+        // given
+        String commitId = "123123";
+        addTestData(commitId);
+
+        // when
+        List<CommentThread> threadList = CommentThread.findByCommitId(commitId);
+
+        // then
+        assertThat(threadList.size()).isEqualTo(2);
+        assertThat(threadList.get(0).createdDate).isEqualTo(JodaDateUtil.before(2));
+        assertThat(threadList.get(1).createdDate).isEqualTo(JodaDateUtil.before(3));
+    }
+
+    /**
+     * {@code commitId}를 가지는 {@link models.NonRangedCodeCommentThread} 한 개 저장.
+     *  - state: OPEN
+     *  - createDate: 3일전
+     * {@code commitId}를 가지는 {@link models.CodeCommentThread} 한 개 저장.
+     *  - state: CLOSED
+     *  - path: "readme.md"
+     *  - createdDate: 2일전
+     * 123321을 커밋 ID로 가지는 {@link models.NonRangedCodeCommentThread} 한 개 저장.
+     *  - state: OPEN
+     *  - createdDate: 1일전
+     *
+     * @param commitId
+     */
+    private void addTestData(String commitId) {
+        NonRangedCodeCommentThread thread1 = new NonRangedCodeCommentThread();
+        thread1.commitId = commitId;
+        thread1.state = CommentThread.ThreadState.OPEN;
+        thread1.createdDate = JodaDateUtil.before(3);
+        thread1.save();
+
+        CodeCommentThread thread2 = new CodeCommentThread();
+        thread2.commitId = commitId;
+        thread2.state = CommentThread.ThreadState.CLOSED;
+        CodeRange codeRange = new CodeRange();
+        codeRange.path = "readme.md";
+        codeRange.startColumn = 0;
+        codeRange.startLine = 1;
+        codeRange.startSide = CodeRange.Side.A;
+        codeRange.endColumn = 100;
+        codeRange.endLine = 10;
+        codeRange.endSide = CodeRange.Side.B;
+        thread2.codeRange = codeRange;
+        thread2.createdDate = JodaDateUtil.before(2);
+        thread2.save();
+
+        NonRangedCodeCommentThread thread3 = new NonRangedCodeCommentThread();
+        thread3.commitId = "123321";
+        thread3.state = CommentThread.ThreadState.OPEN;
+        thread3.createdDate = JodaDateUtil.before(1);
+        thread3.save();
+    }
+
+
 }
Add a comment
List