doortts doortts 2017-06-29
api: Update project import APIs - issue comment
@7b17f900e5e17515488125ff8fc791beac802326
app/controllers/AbstractPostingApp.java
--- app/controllers/AbstractPostingApp.java
+++ app/controllers/AbstractPostingApp.java
@@ -236,7 +236,7 @@
     }
 
     public static void attachUploadFilesToPost(JsonNode files, Resource resource) {
-        if(files.isArray() && files.size() > 0){
+        if(files != null && files.isArray() && files.size() > 0){
             String [] fileIds = new String[files.size()];
             int idx = 0;
             for (JsonNode fileNo : files) {
app/controllers/api/IssueApi.java
--- app/controllers/api/IssueApi.java
+++ app/controllers/api/IssueApi.java
@@ -6,21 +6,26 @@
 
 package controllers.api;
 
+import actions.NullProjectCheckAction;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
-import controllers.AbstractPostingApp;
-import controllers.UserApp;
+import controllers.*;
 import controllers.annotation.IsAllowed;
 import controllers.annotation.IsCreatable;
+import controllers.routes;
 import models.*;
 import models.enumeration.Operation;
 import models.enumeration.ResourceType;
 import org.joda.time.DateTime;
+import play.data.Form;
 import play.db.ebean.Transactional;
 import play.libs.Json;
+import play.mvc.Call;
 import play.mvc.Result;
-import utils.JodaDateUtil;
+import play.mvc.With;
+import utils.*;
 
+import java.io.IOException;
 import java.util.Date;
 import java.util.HashSet;
 import java.util.Set;
@@ -62,10 +67,8 @@
     }
 
     @Transactional
-    @IsCreatable(ResourceType.BOARD_POST)
+    @IsCreatable(ResourceType.ISSUE_POST)
     public static Result newIssueByJson(String owner, String projectName) {
-        ObjectNode result = Json.newObject();
-        System.out.println("-----------" + request().body().asJson().textValue());
         JsonNode json = request().body().asJson();
         if(json == null) {
             return badRequest("Expecting Json data");
@@ -91,6 +94,46 @@
         }
         attachUploadFilesToPost(files, issue.asResource());
 
+        ObjectNode result = Json.newObject();
+        result.put("status", 200);
+        result.put("location", controllers.routes.IssueApp.issue(project.owner, project.name, issue.getNumber()).toString());
+        return ok(result);
+    }
+
+    @Transactional
+    @IsCreatable(ResourceType.ISSUE_COMMENT)
+    public static Result newIssueCommentByJson(String ownerName, String projectName, Long number)
+            throws IOException {
+        JsonNode json = request().body().asJson();
+        if(json == null) {
+            return badRequest("Expecting Json data");
+        }
+
+        Project project = Project.findByOwnerAndProjectName(ownerName, projectName);
+        final Issue issue = Issue.findByNumber(project, number);
+
+        if (!AccessControl.isResourceCreatable(
+                UserApp.currentUser(), issue.asResource(), ResourceType.ISSUE_COMMENT)) {
+            return forbidden(ErrorViews.Forbidden.render("error.forbidden", project));
+        }
+
+        User user = findAuthor(json.findValue("author"));
+        String body = json.findValue("body").asText();
+
+        final IssueComment comment = new IssueComment(issue, user, body);
+
+        comment.createdDate = getCreatedDate(json.findValue("createdAt").asLong());
+        comment.setAuthor(user);
+        comment.issue = issue;
+        comment.save();
+
+        play.Logger.warn(json.findValue("temporaryUploadFiles").asText());
+        attachUploadFilesToPost(json.findValue("temporaryUploadFiles"), comment.asResource());
+
+        ObjectNode result = Json.newObject();
+        result.put("status", 200);
+        result.put("location", RouteUtil.getUrl(comment));
+
         return ok(result);
     }
 
app/controllers/api/ProjectApi.java
--- app/controllers/api/ProjectApi.java
+++ app/controllers/api/ProjectApi.java
@@ -24,7 +24,6 @@
 import java.util.Set;
 import java.util.stream.Collectors;
 
-import static controllers.MigrationApp.composePlainCommentsJson;
 import static controllers.MigrationApp.*;
 import static models.AbstractPosting.findByProject;
 import static play.libs.Json.toJson;
@@ -78,6 +77,7 @@
             Issue issue = ((Issue)posting);
             Optional.ofNullable(issue.assignee)
                     .ifPresent(assignee -> json.put("assignee", composeAssigneeJson(issue)));
+            json.put("state", issue.state.toString());
             Optional.ofNullable(issue.getLabels()).ifPresent(labels -> {
                 if (labels.size() > 0) {
                     json.put("labels", composeLabelJson(labels));
@@ -94,7 +94,7 @@
             json.put("attachments", toJson(attachments));
         }
 
-        List <ObjectNode> comments = composePlainCommentsJson(posting, ResourceType.NONISSUE_COMMENT);
+        List <ObjectNode> comments = composePlainCommentsJson(posting);
         if(comments.size() > 0){
             json.put("comments", toJson(comments));
         }
@@ -150,4 +150,24 @@
         }
         return toJson(labels);
     }
+
+    public static List<ObjectNode> composePlainCommentsJson(AbstractPosting posting) {
+        List<ObjectNode> comments = new ArrayList<>();
+        for (Comment comment : posting.getComments()) {
+            ObjectNode commentNode = Json.newObject();
+            commentNode.put("id", comment.id);
+            commentNode.put("type", comment.asResource().getType().toString());
+            User commentAuthor = User.find.byId(comment.authorId);
+            commentNode.put("author", composeAuthorJson(commentAuthor));
+            commentNode.put("createdAt",comment.createdDate.getTime());
+            commentNode.put("body", comment.contents);
+
+            List<Attachment> attachments = Attachment.findByContainer(comment.asResource());
+            if(attachments.size() > 0) {
+                commentNode.put("attachments", toJson(attachments));
+            }
+            comments.add(commentNode);
+        }
+        return comments;
+    }
 }
conf/routes
--- conf/routes
+++ conf/routes
@@ -44,6 +44,7 @@
 POST           /-_-api/v1/owners/:owner/projects/:projectName/posts                    controllers.api.BoardApi.newPostByJson(owner:String, projectName:String)
 POST           /-_-api/v1/owners/:owner/projects/:projectName/postlabel/:number        controllers.api.BoardApi.updatePostLabel(owner:String, projectName:String, number:Long)
 POST           /-_-api/v1/owners/:owner/projects/:projectName/issues                   controllers.api.IssueApi.newIssueByJson(owner:String, projectName:String)
+POST           /-_-api/v1/owners/:owner/projects/:projectName/issues/:number/comments  controllers.api.IssueApi.newIssueCommentByJson(owner:String, projectName:String, number:Long)
 POST           /-_-api/v1/owners/:owner/projects/:projectName/issuelabel/:number       controllers.api.IssueApi.updateIssueLabel(owner:String, projectName:String, number:Long)
 GET            /-_-api/v1/hello                                                        controllers.api.GlobalApi.hello()
 GET            /-_-api/v1/favoriteProjects                                             controllers.api.UserApi.getFoveriteProjects
Add a comment
List