Skip to content

Commit

Permalink
api: Refactorings
Browse files Browse the repository at this point in the history
- Remove json postfix from method name
- Extract some local methods
- Exporting feature: Project all Labels

and so on..
  • Loading branch information
doortts committed Jul 8, 2017
1 parent d484f59 commit 79a92f1
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 40 deletions.
109 changes: 81 additions & 28 deletions app/controllers/api/BoardApi.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/**
* Yona, Project Hosting SW
*
* <p>
* 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.*;
import controllers.annotation.IsAllowed;
import controllers.annotation.IsCreatable;
import models.*;
Expand All @@ -19,27 +18,31 @@
import play.db.ebean.Transactional;
import play.libs.Json;
import play.mvc.Result;
import utils.AccessControl;
import utils.ErrorViews;
import utils.JodaDateUtil;
import utils.RouteUtil;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.io.IOException;
import java.util.*;

import static controllers.api.IssueApi.findAuthor;
import static controllers.api.IssueApi.parseDateString;
import static play.libs.Json.toJson;

public class BoardApi extends AbstractPostingApp {

@Transactional
public static Result updatePostLabel(String owner, String projectName, Long number) {
JsonNode json = request().body().asJson();
if(json == null) {
if (json == null) {
return badRequest("Expecting Json data");
}
Project project = Project.findByOwnerAndProjectName(owner, projectName);
Posting posting = Posting.findByNumber(project, number);
Set<IssueLabel> labels = new HashSet<>();

for(JsonNode node: json){
for (JsonNode node : json) {
Long labelId = Long.parseLong(node.asText());
labels.add(IssueLabel.finder.byId(labelId));
}
Expand Down Expand Up @@ -67,40 +70,90 @@ public static Result getPosts(String owner, String projectName, Long number) {

@Transactional
@IsCreatable(ResourceType.BOARD_POST)
public static Result newPostByJson(String owner, String projectName) {
public static Result newPostings(String owner, String projectName) {
ObjectNode result = Json.newObject();
JsonNode json = request().body().asJson();
if(json == null) {
return badRequest("Expecting Json data");
if (json == null) {
return badRequest(result.put("message", "Expecting Json data"));
}

JsonNode postingsNode = json.findValue("posts");
if (postingsNode == null || !postingsNode.isArray()) {
return badRequest(result.put("message", "No posts key exists or value wasn't array!"));
}

Project project = Project.findByOwnerAndProjectName(owner, projectName);

User user = User.findUserIfTokenExist(UserApp.currentUser());
List<JsonNode> createdPostings = new ArrayList<>();
for (JsonNode postingNode : postingsNode) {
createdPostings.add(createPostingNode(postingNode, project));
}

return created(toJson(createdPostings));
}

private static JsonNode createPostingNode(JsonNode json, Project project) {
JsonNode files = json.findValue("temporaryUploadFiles");

final Posting post = new Posting();
final Posting posting = new Posting();

post.createdDate = getCreatedDate(json.findValue("created").asLong());
post.updatedDate = getCreatedDate(json.findValue("created").asLong());
post.setAuthor(user);
post.project = project;
post.title = json.findValue("title").asText();
post.body = json.findValue("body").asText();
if(json.findValue("id") != null && json.findValue("id").asLong() > 0){
post.saveWithNumber(json.findValue("id").asLong());
posting.setAuthor(findAuthor(json.findValue("author")));
posting.project = project;
posting.title = json.findValue("title").asText();
posting.body = json.findValue("body").asText();
posting.createdDate = parseDateString(json.findValue("createdAt"));
posting.updatedDate = parseDateString(json.findValue("updatedAt"));
posting.numOfComments = 0;

if (json.findValue("number") != null && json.findValue("number").asLong() > 0) {
posting.saveWithNumber(json.findValue("number").asLong());
} else {
post.save();
posting.save();
}
attachUploadFilesToPost(files, post.asResource());
attachUploadFilesToPost(files, posting.asResource());

ObjectNode result = Json.newObject();
result.put("status", 201);
result.put("location",
controllers.routes.BoardApp.post(project.owner, project.name, posting.getNumber()).toString());
return result;

return ok(result);
}

private static Date getCreatedDate(long timestamp){
if(timestamp == 0){
return JodaDateUtil.now();
@Transactional
@IsCreatable(ResourceType.NONISSUE_COMMENT)
public static Result newPostingComment(String ownerName, String projectName, Long number)
throws IOException {
JsonNode json = request().body().asJson();
if(json == null) {
return badRequest("Expecting Json data");
}
return new DateTime(timestamp * 1000).toDate();

Project project = Project.findByOwnerAndProjectName(ownerName, projectName);
final Posting posting = Posting.findByNumber(project, number);

if (!AccessControl.isResourceCreatable(
UserApp.currentUser(), posting.asResource(), ResourceType.NONISSUE_COMMENT)) {
return forbidden(ErrorViews.Forbidden.render("error.forbidden", project));
}

User user = findAuthor(json.findValue("author"));
String body = json.findValue("body").asText();

final PostingComment comment = new PostingComment(posting, user, body);

comment.createdDate = parseDateString(json.findValue("createdAt"));
comment.setAuthor(user);
comment.posting = posting;
comment.save();

play.Logger.warn(json.findValue("temporaryUploadFiles").asText());
attachUploadFilesToPost(json.findValue("temporaryUploadFiles"), comment.asResource());

ObjectNode result = Json.newObject();
result.put("status", 201);
result.put("location", RouteUtil.getUrl(comment));

return created(result);
}
}
15 changes: 10 additions & 5 deletions app/controllers/api/IssueApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.text.SimpleDateFormat;
import java.util.*;

import static controllers.api.UserApi.createUserNode;
import static play.libs.Json.toJson;

public class IssueApi extends AbstractPostingApp {
Expand Down Expand Up @@ -66,7 +67,7 @@ public static Result getIssue(String owner, String projectName, Long number) {

@Transactional
@IsCreatable(ResourceType.ISSUE_POST)
public static Result newIssueByJson(String owner, String projectName) {
public static Result newIssues(String owner, String projectName) {
ObjectNode result = Json.newObject();
JsonNode json = request().body().asJson();
if (json == null) {
Expand Down Expand Up @@ -103,6 +104,7 @@ private static JsonNode createIssuesNode(JsonNode json, Project project) {
issue.assignee = findAssginee(json.findValue("assignees"), project);
issue.milestone = findMilestone(json.findValue("milestoneTitle"), project);
issue.dueDate = findDueDate(json.findValue("dueDate"));
issue.numOfComments = 0;

if(json.findValue("number") != null && json.findValue("number").asLong() > 0){
issue.saveWithNumber(json.findValue("number").asLong());
Expand Down Expand Up @@ -149,7 +151,7 @@ private static State findIssueState(JsonNode json){

@Transactional
@IsCreatable(ResourceType.ISSUE_COMMENT)
public static Result newIssueCommentByJson(String ownerName, String projectName, Long number)
public static Result newIssueComment(String ownerName, String projectName, Long number)
throws IOException {
JsonNode json = request().body().asJson();
if(json == null) {
Expand Down Expand Up @@ -178,18 +180,21 @@ public static Result newIssueCommentByJson(String ownerName, String projectName,
attachUploadFilesToPost(json.findValue("temporaryUploadFiles"), comment.asResource());

ObjectNode result = Json.newObject();
result.put("status", 200);
result.put("status", 201);
result.put("location", RouteUtil.getUrl(comment));

return ok(result);
return created(result);
}

private static User findAuthor(JsonNode authorNode){
public static User findAuthor(JsonNode authorNode){
if (authorNode != null) {
String email = authorNode.findValue("email").asText();
User originalAuthor = User.findByEmail(email);
if (originalAuthor != null) {
return originalAuthor;
} else {
createUserNode(authorNode);
return User.findByEmail(email);
}
}

Expand Down
14 changes: 13 additions & 1 deletion app/controllers/api/ProjectApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Result exports(String owner, String projectName) {
json.put("issueCount", project.issues.size());
json.put("postCount", project.posts.size());
json.put("milestoneCount", project.milestones.size());
json.put("labels", projectLabels(project));
json.put("labels", getAllLabels(project.issueLabels));
json.put("issues", composePosts(project, Issue.finder));
json.put("posts", composePosts(project, Posting.finder));
json.put("milestones", toJson(project.milestones.stream()
Expand Down Expand Up @@ -369,6 +369,18 @@ private static JsonNode composeLabelJson(Set<IssueLabel> issueLabels) {
return toJson(labels);
}

public static JsonNode getAllLabels(List<IssueLabel> issueLabels) {
List<ObjectNode> labels = new ArrayList<>();
for(IssueLabel label: issueLabels){
ObjectNode labelNode = Json.newObject();
labelNode.put("labelName", label.name);
labelNode.put("labelColor", label.color);
labelNode.put("labelCategory", label.category.name);
labels.add(labelNode);
}
return toJson(labels);
}

public static List<ObjectNode> composePlainCommentsJson(AbstractPosting posting) {
List<ObjectNode> comments = new ArrayList<>();
for (Comment comment : posting.getComments()) {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/UserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static Result newUser() {
return created(toJson(createdUsers));
}

private static JsonNode createUserNode(JsonNode userNode) {
public static JsonNode createUserNode(JsonNode userNode) {
ObjectNode createdUserNode = Json.newObject();

String loginId = userNode.findValue("loginId").asText();
Expand Down
8 changes: 6 additions & 2 deletions app/models/AbstractPosting.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,12 @@ public void saveWithNumber(long number) {

@Transactional
public void update() {
numOfComments = computeNumOfComments();
super.update();
try {
numOfComments = computeNumOfComments();
super.update();
} catch (OptimisticLockException ole) {
play.Logger.warn("OptimisticLockException: " + ole.getMessage());
}
updateMention();
}

Expand Down
1 change: 1 addition & 0 deletions app/models/Assignee.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package models;

import org.apache.commons.collections.CollectionUtils;
import play.data.validation.Constraints.Required;
import play.db.ebean.Model;

Expand Down
7 changes: 4 additions & 3 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ GET /-_-api
GET /-_-api/v1/ controllers.Application.index()
GET /-_-api/v1/owners/:owner/projects/:projectName/exports controllers.api.ProjectApi.exports(owner:String, projectName:String)
GET /-_-api/v1/owners/:owner/projects/:projectName/posts/$number<[0-9]+>/watchers controllers.api.WatcherApi.getWatchers(owner:String, projectName:String, number:Long)
POST /-_-api/v1/owners/:owner/projects/:projectName/posts controllers.api.BoardApi.newPostByJson(owner:String, projectName:String)
POST /-_-api/v1/owners/:owner/projects/:projectName/posts controllers.api.BoardApi.newPostings(owner:String, projectName:String)
POST /-_-api/v1/owners/:owner/projects/:projectName/posts/:number/comments controllers.api.BoardApi.newPostingComment(owner:String, projectName:String, number:Long)
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/issues controllers.api.IssueApi.newIssues(owner:String, projectName:String)
POST /-_-api/v1/owners/:owner/projects/:projectName/issues/:number/comments controllers.api.IssueApi.newIssueComment(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()
Expand Down

0 comments on commit 79a92f1

Please sign in to comment.