Skip to content

Commit

Permalink
Added controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuldeep-knoldus committed Jun 14, 2024
1 parent 74bb6d8 commit d4acb0f
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
6 changes: 6 additions & 0 deletions blogs-analyzer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
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();
}
}
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);
}
}
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;
}
}

0 comments on commit d4acb0f

Please sign in to comment.