Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added comment updation. #60

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/main/java/com/afrozaar/wordpress/wpapi/v2/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ public Post deletePost(Post post) {
return exchange.getBody();
}

@Override
public Comment updateComment(Comment comment) {
final ResponseEntity<Comment> exchange =
doExchange1(Request.COMMENT, HttpMethod.POST, Comment.class, forExpand(comment.getId()), ImmutableMap.of(), fieldsFrom(comment));
return exchange.getBody();
}

@Override
public <T> PagedResponse<T> search(SearchRequest<T> search) {
final URI uri = search.usingClient(this).build().toUri();
Expand Down Expand Up @@ -992,6 +999,56 @@ protected Map<String, Object> fieldsFrom(Post post) {
return builder.build();
}

@VisibleForTesting
@SuppressWarnings("unchecked")
protected Map<String, Object> fieldsFrom(Comment comment) {
ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();

BiConsumer<String, Object> biConsumer = (key, value) -> ofNullable(value).ifPresent(v -> builder.put(key, v));

List<String> processableFields = Arrays.asList(
"author",
"author_email",
"author_ip",
"author_name",
"author_url",
"author_user_agent",
"content",
"date",
"date_gmt",
"parent",
"post",
// "status",
"meta"
);

// types ignored for now: slug, status, type

Arrays.stream(comment.getClass().getDeclaredFields())
.filter(field -> field.getAnnotationsByType(JsonProperty.class).length > 0)
.map(field -> tuple(field, field.getAnnotationsByType(JsonProperty.class)[0]))
.filter(fieldTuple -> processableFields.contains(fieldTuple.v2.value()))
.forEach(field -> {
try {
ReflectionUtils.makeAccessible(field.v1);
Object theField = field.v1.get(comment);
if (nonNull(theField)) {
final Object value;
if (theField instanceof RenderableField) {
value = ((RenderableField) theField).getRendered();
} else {
value = theField;
}
biConsumer.accept(field.v2.value(), value);
}
} catch (IllegalAccessException e) {
LOG.error("Error populating comment fields builder for field '{}'", field.v1.getName(), e);
}
});

return builder.build();
}

private <T, B> ResponseEntity<T> doExchange0(HttpMethod method, URI uri, Class<T> typeRef, B body, @Nullable MediaType mediaType) {
final RequestEntity.BodyBuilder builder = RequestEntity.method(method, uri)
.header(userAgentTuple.v1, userAgentTuple.v2);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/afrozaar/wordpress/wpapi/v2/Wordpress.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.afrozaar.wordpress.wpapi.v2;

import com.afrozaar.wordpress.wpapi.v2.api.Categories;
import com.afrozaar.wordpress.wpapi.v2.api.Comments;
import com.afrozaar.wordpress.wpapi.v2.api.CustomCalls;
import com.afrozaar.wordpress.wpapi.v2.api.Medias;
import com.afrozaar.wordpress.wpapi.v2.api.Pages;
Expand All @@ -16,7 +17,7 @@
import java.net.URI;
import java.util.function.Function;

public interface Wordpress extends Posts, PostMetas, Taxonomies, Terms, Medias, Pages, Users, Tags, Categories, CustomCalls {
public interface Wordpress extends Posts, Comments, PostMetas, Taxonomies, Terms, Medias, Pages, Users, Tags, Categories, CustomCalls {

String getContext();

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/afrozaar/wordpress/wpapi/v2/api/Comments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.afrozaar.wordpress.wpapi.v2.api;

import com.afrozaar.wordpress.wpapi.v2.model.Comment;
import com.afrozaar.wordpress.wpapi.v2.request.Request;
import com.afrozaar.wordpress.wpapi.v2.request.SearchRequest;

public interface Comments {

/**
* Search request just returning the first page of comments.
*/
static SearchRequest.Builder<Comment> listBuilder() {
return SearchRequest.Builder.aSearchRequest(Comment.class)
.withUri(Request.COMMENTS);
}

Comment updateComment(Comment comment);
}
223 changes: 223 additions & 0 deletions src/main/java/com/afrozaar/wordpress/wpapi/v2/model/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package com.afrozaar.wordpress.wpapi.v2.model;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"post",
"parent",
"author",
"author_name",
"author_url",
"date",
"date_gmt",
"content",
"link",
"status",
"type",
"author_avatar_urls",
"meta",
"_links"
})
public class Comment {

@JsonProperty("id")
private Integer id;
@JsonProperty("post")
private Integer post;
@JsonProperty("parent")
private Integer parent;
@JsonProperty("author")
private Integer author;
@JsonProperty("author_name")
private String authorName;
@JsonProperty("author_email")
private String authorEmail;
@JsonProperty("author_url")
private String authorUrl;
@JsonProperty("date")
private String date;
@JsonProperty("date_gmt")
private String dateGmt;
@JsonProperty("content")
private Content content;
@JsonProperty("link")
private String link;
@JsonProperty("status")
private String status;
@JsonProperty("type")
private String type;
@JsonProperty("author_avatar_urls")
private AvatarUrls avatarUrls;
@JsonProperty("meta")
private List<Object> meta = new ArrayList<>();
@JsonProperty("_links")
private Links links;

@JsonProperty("id")
public Integer getId() {
return id;
}

@JsonProperty("id")
public void setId(Integer id) {
this.id = id;
}

@JsonProperty("post")
public Integer getPost() {
return post;
}

@JsonProperty("post")
public void setPost(Integer post) {
this.post = post;
}

@JsonProperty("parent")
public Integer getParent() {
return parent;
}

@JsonProperty("parent")
public void setParent(Integer parent) {
this.parent = parent;
}

@JsonProperty("author")
public Integer getAuthor() {
return author;
}

@JsonProperty("author")
public void setAuthor(Integer author) {
this.author = author;
}

@JsonProperty("author_name")
public String getAuthorName() {
return authorName;
}

@JsonProperty("author_name")
public void setAuthorName(String authorName) {
this.authorName = authorName;
}

@JsonProperty("author_email")
public String getAuthorEmail() {
return authorEmail;
}

@JsonProperty("author_email")
public void setAuthorEmail(String authorEmail) {
this.authorEmail = authorEmail;
}

@JsonProperty("author_url")
public String getAuthorUrl() {
return authorUrl;
}

@JsonProperty("author_url")
public void setAuthorUrl(String authorUrl) {
this.authorUrl = authorUrl;
}

@JsonProperty("date")
public String getDate() {
return date;
}

@JsonProperty("date")
public void setDate(String date) {
this.date = date;
}

@JsonProperty("date_gmt")
public String getDateGmt() {
return dateGmt;
}

@JsonProperty("date_gmt")
public void setDateGmt(String dateGmt) {
this.dateGmt = dateGmt;
}

@JsonProperty("content")
public Content getContent() {
return content;
}

@JsonProperty("content")
public void setContent(Content content) {
this.content = content;
}

@JsonProperty("link")
public String getLink() {
return link;
}

@JsonProperty("link")
public void setLink(String link) {
this.link = link;
}

@JsonProperty("status")
public String getStatus() {
return status;
}

@JsonProperty("status")
public void setStatus(String status) {
this.status = status;
}

@JsonProperty("type")
public String getType() {
return type;
}

@JsonProperty("type")
public void setType(String type) {
this.type = type;
}

@JsonProperty("author_avatar_urls")
public AvatarUrls getAvatarUrls() {
return avatarUrls;
}

@JsonProperty("author_avatar_urls")
public void setAvatarUrls(AvatarUrls avatarUrls) {
this.avatarUrls = avatarUrls;
}

@JsonProperty("meta")
public List<Object> getMeta() {
return meta;
}

@JsonProperty("meta")
public void setMeta(List<Object> meta) {
this.meta = meta;
}

@JsonProperty("_links")
public Links getLinks() {
return links;
}

@JsonProperty("_links")
public void setLinks(Links links) {
this.links = links;
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/afrozaar/wordpress/wpapi/v2/model/Term.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.afrozaar.wordpress.wpapi.v2.model;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.ImmutableMap;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down Expand Up @@ -28,6 +30,8 @@ public class Term {
private String taxonomySlug;
@JsonProperty("parent")
private Long parentId;
@JsonIgnore
private Map<String, String> additionalProps;

@JsonProperty("meta") //TODO: Keep an eye on https://github.com/WP-API/WP-API/issues/2859 and fix once it is resolved.
private List<Long> meta;
Expand Down Expand Up @@ -105,6 +109,11 @@ public void setMeta(List<Long> meta) {
this.meta = meta;
}

@JsonAnyGetter
public void setAdditionalProps(Map<String, String> additionalProps) {
this.additionalProps = additionalProps;
}

public Map<String, Object> asMap() {
final ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
BiConsumer<String, Object> c = (index, value) -> Optional.ofNullable(value).ifPresent(val -> builder.put(index, val));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public abstract class Request {
public static final String POST_TERM = "/posts/{postId}/{taxonomy}/{termId}";
public static final String POST_TAGS = "/tags?post={postId}";

public static final String COMMENTS = "/comments";
public static final String COMMENT = "/comments/{id}";

public static final String METAS = "/posts/{postId}/meta";
public static final String META = "/posts/{postId}/meta/{metaId}";
public static final String CUSTOM_POST_METAS = "/{postType}/{postId}/meta";
Expand Down