-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74bb6d8
commit d4acb0f
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
blogs-analyzer/src/main/java/com/nashtech/blogs/analyzer/config/AppConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.nashtech.blogs.analyzer.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class AppConfig { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
blogs-analyzer/src/main/java/com/nashtech/blogs/analyzer/controller/WordPressController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.nashtech.blogs.analyzer.controller; | ||
|
||
import com.nashtech.blogs.analyzer.model.Post; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@RestController | ||
@RequestMapping("/api/wordpress") | ||
public class WordPressController { | ||
|
||
@Autowired | ||
private RestTemplate restTemplate; | ||
|
||
private final String WORDPRESS_API_BASE_URL = "https://blog.nashtechglobal.com/wp-json/wp/v2/"; | ||
|
||
@GetMapping("/posts/{id}") | ||
public ResponseEntity<String> getPostById(@PathVariable Long id) { | ||
String url = WORDPRESS_API_BASE_URL + "posts/" + id; | ||
String response = restTemplate.getForObject(url, String.class); | ||
|
||
JSONObject jsonResponse = new JSONObject(response); | ||
String content = jsonResponse.getJSONObject("content").getString("rendered"); | ||
|
||
return ResponseEntity.ok(content); | ||
} | ||
|
||
@GetMapping("/posts") | ||
public ResponseEntity<String> searchPosts(@RequestParam(value = "search", required = false) String search) { | ||
String url = WORDPRESS_API_BASE_URL + "posts?search=" + search; | ||
String response = restTemplate.getForObject(url, String.class); | ||
JSONArray postsArray = new JSONArray(response); | ||
StringBuilder renderedContent = new StringBuilder(); | ||
|
||
for (int i = 0; i < postsArray.length(); i++) { | ||
JSONObject post = postsArray.getJSONObject(i); | ||
String content = post.getJSONObject("content").getString("rendered"); | ||
renderedContent.append(content).append("\n"); | ||
} | ||
|
||
return ResponseEntity.ok(renderedContent.toString()); | ||
} | ||
|
||
@GetMapping("/posts-by-title") | ||
public ResponseEntity<String> searchPostsByTitle(@RequestParam String title) { | ||
String url = WORDPRESS_API_BASE_URL + "posts?search=" + title + "&searchFields=title"; | ||
String response = restTemplate.getForObject(url, String.class); | ||
JSONArray postsArray = new JSONArray(response); | ||
StringBuilder renderedContent = new StringBuilder(); | ||
|
||
for (int i = 0; i < postsArray.length(); i++) { | ||
JSONObject post = postsArray.getJSONObject(i); | ||
String content = post.getJSONObject("content").getString("rendered"); | ||
renderedContent.append(content).append("\n"); | ||
} | ||
|
||
return ResponseEntity.ok(renderedContent.toString()); | ||
} | ||
|
||
// @GetMapping("/posts/drafts") | ||
// public ResponseEntity<String> getDraftPosts() { | ||
// String url = WORDPRESS_API_BASE_URL + "posts?status=draft"; | ||
// String response = restTemplate.getForObject(url, String.class); | ||
// return ResponseEntity.ok(response); | ||
// } | ||
|
||
@GetMapping | ||
public ResponseEntity<List<Post>> getAllPosts() { | ||
String url = WORDPRESS_API_BASE_URL + "posts"; | ||
ResponseEntity<List<Map<String, Object>>> response = restTemplate.exchange( | ||
url, | ||
HttpMethod.GET, | ||
null, | ||
new ParameterizedTypeReference<>() { | ||
} | ||
); | ||
|
||
List<Map<String, Object>> postMaps = response.getBody(); | ||
List<Post> posts = new ArrayList<>(); | ||
|
||
if (postMaps != null) { | ||
for (Map<String, Object> postMap : postMaps) { | ||
Post post = new Post(); | ||
post.setId(((Number) postMap.get("id")).longValue()); | ||
|
||
Map<String, Object> titleMap = (Map<String, Object>) postMap.get("title"); | ||
Post.Title title = new Post.Title(); | ||
title.setRendered((String) titleMap.get("rendered")); | ||
post.setTitle(title); | ||
|
||
post.setUrl(WORDPRESS_API_BASE_URL + "posts/" + post.getId()); | ||
post.setStatus((String) postMap.get("status")); | ||
|
||
posts.add(post); | ||
} | ||
} | ||
|
||
return ResponseEntity.ok(posts); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
blogs-analyzer/src/main/java/com/nashtech/blogs/analyzer/model/Post.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.nashtech.blogs.analyzer.model; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Post { | ||
private Long id; | ||
private Title title; | ||
private String url; | ||
private String status; | ||
|
||
@Data | ||
public static class Title { | ||
private String rendered; | ||
} | ||
} |