Skip to content

Commit 4a1d377

Browse files
committed
Added comment updation.
PubToolBox#60 following this pull request fix: PubToolBox@3ad60be
1 parent 6773e93 commit 4a1d377

6 files changed

Lines changed: 312 additions & 11 deletions

File tree

src/main/java/com/afrozaar/wordpress/wpapi/v2/Client.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ public Post deletePost(Post post) {
220220
return exchange.getBody();
221221
}
222222

223+
@Override
224+
public Comment updateComment(Comment comment) {
225+
final ResponseEntity<Comment> exchange =
226+
doExchange1(Request.COMMENT, HttpMethod.POST, Comment.class, forExpand(comment.getId()), ImmutableMap.of(), fieldsFrom(comment));
227+
return exchange.getBody();
228+
}
229+
223230
@Override
224231
public <T> PagedResponse<T> search(SearchRequest<T> search) {
225232
final URI uri = search.usingClient(this).build().toUri();
@@ -1068,6 +1075,56 @@ protected Map<String, Object> fieldsFrom(Post post) {
10681075
return builder.build();
10691076
}
10701077

1078+
@VisibleForTesting
1079+
@SuppressWarnings("unchecked")
1080+
protected Map<String, Object> fieldsFrom(Comment comment) {
1081+
ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
1082+
1083+
BiConsumer<String, Object> biConsumer = (key, value) -> ofNullable(value).ifPresent(v -> builder.put(key, v));
1084+
1085+
List<String> processableFields = Arrays.asList(
1086+
"author",
1087+
"author_email",
1088+
"author_ip",
1089+
"author_name",
1090+
"author_url",
1091+
"author_user_agent",
1092+
"content",
1093+
"date",
1094+
"date_gmt",
1095+
"parent",
1096+
"post",
1097+
// "status",
1098+
"meta"
1099+
);
1100+
1101+
// types ignored for now: slug, status, type
1102+
1103+
Arrays.stream(comment.getClass().getDeclaredFields())
1104+
.filter(field -> field.getAnnotationsByType(JsonProperty.class).length > 0)
1105+
.map(field -> tuple(field, field.getAnnotationsByType(JsonProperty.class)[0]))
1106+
.filter(fieldTuple -> processableFields.contains(fieldTuple.v2.value()))
1107+
.forEach(field -> {
1108+
try {
1109+
ReflectionUtils.makeAccessible(field.v1);
1110+
Object theField = field.v1.get(comment);
1111+
if (nonNull(theField)) {
1112+
final Object value;
1113+
if (theField instanceof RenderableField) {
1114+
value = ((RenderableField) theField).getRendered();
1115+
} else {
1116+
value = theField;
1117+
}
1118+
biConsumer.accept(field.v2.value(), value);
1119+
}
1120+
} catch (IllegalAccessException e) {
1121+
LOG.error("Error populating comment fields builder for field '{}'", field.v1.getName(), e);
1122+
}
1123+
});
1124+
1125+
return builder.build();
1126+
}
1127+
10711128
private <T, B> ResponseEntity<T> doExchange0(HttpMethod method, URI uri, Class<T> typeRef, B body, @Nullable MediaType mediaType) {
10721129
final RequestEntity.BodyBuilder builder = RequestEntity.method(method, uri)
10731130
.header(userAgentTuple.v1, userAgentTuple.v2);

src/main/java/com/afrozaar/wordpress/wpapi/v2/Wordpress.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
package com.afrozaar.wordpress.wpapi.v2;
22

3-
import com.afrozaar.wordpress.wpapi.v2.api.Categories;
4-
import com.afrozaar.wordpress.wpapi.v2.api.CustomCalls;
5-
import com.afrozaar.wordpress.wpapi.v2.api.Medias;
6-
import com.afrozaar.wordpress.wpapi.v2.api.Pages;
7-
import com.afrozaar.wordpress.wpapi.v2.api.PostMetas;
8-
import com.afrozaar.wordpress.wpapi.v2.api.Posts;
9-
import com.afrozaar.wordpress.wpapi.v2.api.Tags;
10-
import com.afrozaar.wordpress.wpapi.v2.api.Taxonomies;
11-
import com.afrozaar.wordpress.wpapi.v2.api.Terms;
12-
import com.afrozaar.wordpress.wpapi.v2.api.Users;
3+
import com.afrozaar.wordpress.wpapi.v2.api.*;
134
import com.afrozaar.wordpress.wpapi.v2.request.SearchRequest;
145
import com.afrozaar.wordpress.wpapi.v2.response.PagedResponse;
156

167
import java.net.URI;
178
import java.util.function.Function;
189

19-
public interface Wordpress extends Posts, PostMetas, Taxonomies, Terms, Medias, Pages, Users, Tags, Categories, CustomCalls {
10+
public interface Wordpress extends Posts, Comments, PostMetas, Taxonomies, Terms, Medias, Pages, Users, Tags, Categories, CustomCalls {
2011

2112
String getContext();
2213

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.afrozaar.wordpress.wpapi.v2.api;
2+
3+
import com.afrozaar.wordpress.wpapi.v2.model.Comment;
4+
import com.afrozaar.wordpress.wpapi.v2.request.Request;
5+
import com.afrozaar.wordpress.wpapi.v2.request.SearchRequest;
6+
7+
public interface Comments {
8+
9+
/**
10+
* Search request just returning the first page of comments.
11+
*/
12+
static SearchRequest.Builder<Comment> listBuilder() {
13+
return SearchRequest.Builder.aSearchRequest(Comment.class)
14+
.withUri(Request.COMMENTS);
15+
}
16+
17+
Comment updateComment(Comment comment);
18+
}
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
package com.afrozaar.wordpress.wpapi.v2.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.fasterxml.jackson.annotation.JsonInclude;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
9+
10+
@JsonInclude(JsonInclude.Include.NON_NULL)
11+
@JsonPropertyOrder({
12+
"id",
13+
"post",
14+
"parent",
15+
"author",
16+
"author_name",
17+
"author_url",
18+
"date",
19+
"date_gmt",
20+
"content",
21+
"link",
22+
"status",
23+
"type",
24+
"author_avatar_urls",
25+
"meta",
26+
"_links"
27+
})
28+
public class Comment {
29+
30+
@JsonProperty("id")
31+
private Integer id;
32+
@JsonProperty("post")
33+
private Integer post;
34+
@JsonProperty("parent")
35+
private Integer parent;
36+
@JsonProperty("author")
37+
private Integer author;
38+
@JsonProperty("author_name")
39+
private String authorName;
40+
@JsonProperty("author_email")
41+
private String authorEmail;
42+
@JsonProperty("author_url")
43+
private String authorUrl;
44+
@JsonProperty("date")
45+
private String date;
46+
@JsonProperty("date_gmt")
47+
private String dateGmt;
48+
@JsonProperty("content")
49+
private Content content;
50+
@JsonProperty("link")
51+
private String link;
52+
@JsonProperty("status")
53+
private String status;
54+
@JsonProperty("type")
55+
private String type;
56+
@JsonProperty("author_avatar_urls")
57+
private AvatarUrls avatarUrls;
58+
@JsonProperty("meta")
59+
private List<Object> meta = new ArrayList<>();
60+
@JsonProperty("_links")
61+
private Links links;
62+
63+
@JsonProperty("id")
64+
public Integer getId() {
65+
return id;
66+
}
67+
68+
@JsonProperty("id")
69+
public void setId(Integer id) {
70+
this.id = id;
71+
}
72+
73+
@JsonProperty("post")
74+
public Integer getPost() {
75+
return post;
76+
}
77+
78+
@JsonProperty("post")
79+
public void setPost(Integer post) {
80+
this.post = post;
81+
}
82+
83+
@JsonProperty("parent")
84+
public Integer getParent() {
85+
return parent;
86+
}
87+
88+
@JsonProperty("parent")
89+
public void setParent(Integer parent) {
90+
this.parent = parent;
91+
}
92+
93+
@JsonProperty("author")
94+
public Integer getAuthor() {
95+
return author;
96+
}
97+
98+
@JsonProperty("author")
99+
public void setAuthor(Integer author) {
100+
this.author = author;
101+
}
102+
103+
@JsonProperty("author_name")
104+
public String getAuthorName() {
105+
return authorName;
106+
}
107+
108+
@JsonProperty("author_name")
109+
public void setAuthorName(String authorName) {
110+
this.authorName = authorName;
111+
}
112+
113+
@JsonProperty("author_email")
114+
public String getAuthorEmail() {
115+
return authorEmail;
116+
}
117+
118+
@JsonProperty("author_email")
119+
public void setAuthorEmail(String authorEmail) {
120+
this.authorEmail = authorEmail;
121+
}
122+
123+
@JsonProperty("author_url")
124+
public String getAuthorUrl() {
125+
return authorUrl;
126+
}
127+
128+
@JsonProperty("author_url")
129+
public void setAuthorUrl(String authorUrl) {
130+
this.authorUrl = authorUrl;
131+
}
132+
133+
@JsonProperty("date")
134+
public String getDate() {
135+
return date;
136+
}
137+
138+
@JsonProperty("date")
139+
public void setDate(String date) {
140+
this.date = date;
141+
}
142+
143+
@JsonProperty("date_gmt")
144+
public String getDateGmt() {
145+
return dateGmt;
146+
}
147+
148+
@JsonProperty("date_gmt")
149+
public void setDateGmt(String dateGmt) {
150+
this.dateGmt = dateGmt;
151+
}
152+
153+
@JsonProperty("content")
154+
public Content getContent() {
155+
return content;
156+
}
157+
158+
@JsonProperty("content")
159+
public void setContent(Content content) {
160+
this.content = content;
161+
}
162+
163+
@JsonProperty("link")
164+
public String getLink() {
165+
return link;
166+
}
167+
168+
@JsonProperty("link")
169+
public void setLink(String link) {
170+
this.link = link;
171+
}
172+
173+
@JsonProperty("status")
174+
public String getStatus() {
175+
return status;
176+
}
177+
178+
@JsonProperty("status")
179+
public void setStatus(String status) {
180+
this.status = status;
181+
}
182+
183+
@JsonProperty("type")
184+
public String getType() {
185+
return type;
186+
}
187+
188+
@JsonProperty("type")
189+
public void setType(String type) {
190+
this.type = type;
191+
}
192+
193+
@JsonProperty("author_avatar_urls")
194+
public AvatarUrls getAvatarUrls() {
195+
return avatarUrls;
196+
}
197+
198+
@JsonProperty("author_avatar_urls")
199+
public void setAvatarUrls(AvatarUrls avatarUrls) {
200+
this.avatarUrls = avatarUrls;
201+
}
202+
203+
@JsonProperty("meta")
204+
public List<Object> getMeta() {
205+
return meta;
206+
}
207+
208+
@JsonProperty("meta")
209+
public void setMeta(List<Object> meta) {
210+
this.meta = meta;
211+
}
212+
213+
@JsonProperty("_links")
214+
public Links getLinks() {
215+
return links;
216+
}
217+
218+
@JsonProperty("_links")
219+
public void setLinks(Links links) {
220+
this.links = links;
221+
}
222+
223+
}

src/main/java/com/afrozaar/wordpress/wpapi/v2/model/Term.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.afrozaar.wordpress.wpapi.v2.model;
22

3+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
4+
import com.fasterxml.jackson.annotation.JsonIgnore;
35
import com.google.common.collect.ImmutableMap;
46

57
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@@ -28,6 +30,8 @@ public class Term {
2830
private String taxonomySlug;
2931
@JsonProperty("parent")
3032
private Long parentId;
33+
@JsonIgnore
34+
private Map<String, String> additionalProps;
3135

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

112+
@JsonAnyGetter
113+
public void setAdditionalProps(Map<String, String> additionalProps) {
114+
this.additionalProps = additionalProps;
115+
}
116+
108117
public Map<String, Object> asMap() {
109118
final ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
110119
BiConsumer<String, Object> c = (index, value) -> Optional.ofNullable(value).ifPresent(val -> builder.put(index, val));

src/main/java/com/afrozaar/wordpress/wpapi/v2/request/Request.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public abstract class Request {
2020
public static final String POST_TERM = "/posts/{postId}/{taxonomy}/{termId}";
2121
public static final String POST_TAGS = "/tags?post={postId}";
2222

23+
public static final String COMMENTS = "/comments";
24+
public static final String COMMENT = "/comments/{id}";
25+
2326
public static final String METAS = "/posts/{postId}/meta";
2427
public static final String META = "/posts/{postId}/meta/{metaId}";
2528
public static final String CUSTOM_POST_METAS = "/{postType}/{postId}/meta";

0 commit comments

Comments
 (0)