doortts doortts 2017-07-04
api: Update APIs - milestone
@8a845c7da05621f672ebd854f8259dd612bc3491
 
app/controllers/api/MilestoneApi.java (added)
+++ app/controllers/api/MilestoneApi.java
@@ -0,0 +1,111 @@
+/**
+ * Yona, Project Hosting SW
+ *
+ * Copyright 2016 the original author or authors.
+ */
+
+package controllers.api;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import controllers.AbstractPostingApp;
+import controllers.UserApp;
+import controllers.annotation.IsAllowed;
+import controllers.annotation.IsCreatable;
+import models.*;
+import models.enumeration.Operation;
+import models.enumeration.ResourceType;
+import models.enumeration.State;
+import org.joda.time.DateTime;
+import play.db.ebean.Transactional;
+import play.i18n.Messages;
+import play.libs.Json;
+import play.mvc.Result;
+import utils.*;
+
+import java.io.IOException;
+import java.text.DateFormat;
+import java.util.*;
+
+import static controllers.MigrationApp.getMilestoneNode;
+import static controllers.api.IssueApi.getCreatedDate;
+import static play.libs.Json.toJson;
+
+public class MilestoneApi extends AbstractPostingApp {
+    @Transactional
+    @IsCreatable(ResourceType.MILESTONE)
+    public static Result newMilestone(String owner, String projectName) {
+        ObjectNode result = Json.newObject();
+        JsonNode json = request().body().asJson();
+        if (json == null) {
+            return badRequest(result.put("message", "Expecting Json data"));
+        }
+
+        JsonNode milestonesNode = json.findValue("milestones");
+        if (milestonesNode == null || !milestonesNode.isArray()) {
+            return badRequest(result.put("message", "No milestones key exists or value wasn't array!"));
+        }
+
+        Project project = Project.findByOwnerAndProjectName(owner, projectName);
+        List<JsonNode> createdMilestones = new ArrayList<>();
+        for (JsonNode milestoneNode : milestonesNode) {
+            createdMilestones.add(createMilestoneNode(milestoneNode, project));
+        }
+
+        return created(toJson(createdMilestones));
+    }
+
+    private static JsonNode createMilestoneNode(JsonNode milestoneNode, Project project) {
+        ObjectNode result = Json.newObject();
+        if (!Milestone.isUniqueProjectIdAndTitle(project.id, milestoneNode.findValue("title").asText())) {
+            result.put("milestone", milestoneNode);
+            return result.put("message", Messages.get("milestone.title.duplicated"));
+        }
+
+        Milestone newMilestone = new Milestone();
+        newMilestone.title = parseMilestoneTitle(milestoneNode);
+        newMilestone.contents = parseMilestoneContents(milestoneNode);
+        newMilestone.project = project;
+        newMilestone.dueDate = parseDuedate(milestoneNode);
+        newMilestone.state = parseMilestoneState(milestoneNode);
+
+        Milestone.create(newMilestone);
+        return getMilestoneNode(newMilestone);
+    }
+
+    private static State parseMilestoneState(JsonNode json) {
+        JsonNode stateNode = json.findValue("state");
+        if (stateNode == null) {
+            return State.OPEN;
+        }
+        if ("closed".equalsIgnoreCase(stateNode.asText())) {
+            return State.CLOSED;
+        }
+        return State.OPEN;
+    }
+
+    private static Date parseDuedate(JsonNode json) {
+        JsonNode dueOnNode = json.findValue("due_on");
+        if (dueOnNode == null) {
+            return null;
+        }
+        DateTime dateTime = new DateTime(dueOnNode.asText());
+        return JodaDateUtil.lastSecondOfDay(dateTime.toDate());
+    }
+
+    private static String parseMilestoneTitle(JsonNode json) {
+        JsonNode contentsNode = json.findValue("title");
+        if (contentsNode == null) {
+            return "No title";
+        }
+        return contentsNode.asText();
+    }
+
+    private static String parseMilestoneContents(JsonNode json) {
+        JsonNode contentsNode = json.findValue("description");
+        if (contentsNode == null) {
+            return "";
+        }
+        return contentsNode.asText();
+    }
+}
app/models/Milestone.java
--- app/models/Milestone.java
+++ app/models/Milestone.java
@@ -30,12 +30,14 @@
 import models.support.OrderParams;
 import models.support.SearchParams;
 import org.apache.commons.lang3.time.DateUtils;
+import org.apache.shiro.util.CollectionUtils;
 import play.data.format.Formats;
 import play.data.validation.Constraints;
 import play.db.ebean.Model;
 import play.i18n.Messages;
 import utils.JodaDateUtil;
 
+import javax.annotation.Nonnull;
 import javax.persistence.*;
 import java.text.SimpleDateFormat;
 import java.util.*;
@@ -150,6 +152,14 @@
         return Milestone.findMilestones(projectId, State.OPEN);
     }
 
+    public static Milestone findMilestoneByTitle(@Nonnull Project project, String title) {
+        List<Milestone> milestones = find.where().eq("project.id", project.id).eq("title", title).findList();
+        if (CollectionUtils.isEmpty(milestones)) {
+            return null;
+        }
+        return milestones.get(0);
+    }
+
     /**
      * convert mildestone due date string into yyyy-MM-dd format
      *
conf/routes
--- conf/routes
+++ conf/routes
@@ -46,6 +46,7 @@
 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)
+POST           /-_-api/v1/owners/:owner/projects/:projectName/milestones               controllers.api.MilestoneApi.newMilestone(owner:String, projectName:String)
 GET            /-_-api/v1/hello                                                        controllers.api.GlobalApi.hello()
 GET            /-_-api/v1/favoriteProjects                                             controllers.api.UserApi.getFoveriteProjects
 POST           /-_-api/v1/favoriteProjects/:projectId                                  controllers.api.UserApi.toggleFoveriteProject(projectId:String)
Add a comment
List