Skip to content

Commit ec72825

Browse files
📝 Add docstrings to reactive-programing
Docstrings generation was requested by @developer-shubham101. * #1 (comment) The following files were modified: * `reactive/src/main/java/com/example/reactive/BlogController.java` * `reactive/src/main/java/com/example/reactive/BlogRepository.java` * `reactive/src/main/java/com/example/reactive/BlogService.java` * `reactive/src/main/java/com/example/reactive/EpisodeController.java` * `reactive/src/main/java/com/example/reactive/ReactiveApplication.java` * `reactive/src/main/java/com/example/reactive/sample/CustomEpisode.java` * `reactive/src/main/java/com/example/reactive/sample/TracingConfig.java` * `reactive/src/main/java/com/example/reactive/sample/WebClientConfig.java`
1 parent 4b180b8 commit ec72825

8 files changed

Lines changed: 130 additions & 42 deletions

File tree

reactive/src/main/java/com/example/reactive/BlogController.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,76 @@
99
public class BlogController {
1010
private final BlogService blogService;
1111

12+
/****
13+
* Constructs a BlogController with the specified BlogService.
14+
*
15+
* @param blogService the service used to handle blog operations
16+
*/
1217
public BlogController(BlogService blogService) {
1318
this.blogService = blogService;
1419
}
1520

21+
/**
22+
* Creates a new blog entry.
23+
*
24+
* @param blog the blog data to create
25+
* @return a Mono emitting the created blog
26+
*/
1627
@PostMapping
1728
public Mono<Blog> createBlog(@RequestBody Blog blog) {
1829
return blogService.createBlog(blog);
1930
}
2031

32+
/**
33+
* Retrieves all blog entries as a reactive stream.
34+
*
35+
* @return a Flux emitting all Blog entities
36+
*/
2137
@GetMapping
2238
public Flux<Blog> getAllBlogs() {
2339
return blogService.getAllBlogs();
2440
}
2541

42+
/**
43+
* Retrieves a blog by its unique identifier.
44+
*
45+
* @param id the unique identifier of the blog to retrieve
46+
* @return a Mono emitting the blog with the specified ID, or empty if not found
47+
*/
2648
@GetMapping("/{id}")
2749
public Mono<Blog> getBlogById(@PathVariable String id) {
2850
return blogService.getBlogById(id);
2951
}
3052

53+
/**
54+
* Retrieves all blogs authored by the specified author as a reactive stream.
55+
*
56+
* @param author the name of the author whose blogs are to be retrieved
57+
* @return a Flux emitting blogs written by the given author
58+
*/
3159
@GetMapping("/author/{author}")
3260
public Flux<Blog> getBlogsByAuthor(@PathVariable String author) {
3361
return blogService.getBlogsByAuthor(author);
3462
}
3563

64+
/**
65+
* Updates an existing blog with the specified ID using the provided blog data.
66+
*
67+
* @param id the unique identifier of the blog to update
68+
* @param blog the new blog data to apply
69+
* @return a Mono emitting the updated blog
70+
*/
3671
@PutMapping("/{id}")
3772
public Mono<Blog> updateBlog(@PathVariable String id, @RequestBody Blog blog) {
3873
return blogService.updateBlog(id, blog);
3974
}
4075

76+
/**
77+
* Deletes a blog with the specified ID.
78+
*
79+
* @param id the unique identifier of the blog to delete
80+
* @return a Mono signaling completion when the blog is deleted
81+
*/
4182
@DeleteMapping("/{id}")
4283
public Mono<Void> deleteBlog(@PathVariable String id) {
4384
return blogService.deleteBlog(id);

reactive/src/main/java/com/example/reactive/BlogRepository.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
import reactor.core.publisher.Flux;
55

66
public interface BlogRepository extends ReactiveMongoRepository<Blog, String> {
7-
Flux<Blog> findByAuthor(String author);
7+
/****
8+
* Returns a reactive stream of blog entries authored by the specified user.
9+
*
10+
* @param author the author's name to filter blog entries by
11+
* @return a Flux emitting all Blog entities with the given author
12+
*/
13+
Flux<Blog> findByAuthor(String author);
814
}

reactive/src/main/java/com/example/reactive/BlogService.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,61 @@
88
public class BlogService {
99
private final BlogRepository blogRepository;
1010

11+
/****
12+
* Constructs a BlogService with the specified BlogRepository for data access.
13+
*
14+
* @param blogRepository the repository used for blog persistence operations
15+
*/
1116
public BlogService(BlogRepository blogRepository) {
1217
this.blogRepository = blogRepository;
1318
}
1419

20+
/**
21+
* Creates a new blog entry and saves it to the repository.
22+
*
23+
* @param blog the blog entity to be created
24+
* @return a Mono emitting the saved blog entity
25+
*/
1526
public Mono<Blog> createBlog(Blog blog) {
1627
return blogRepository.save(blog);
1728
}
1829

30+
/**
31+
* Retrieves all blog entries as a reactive stream.
32+
*
33+
* @return a Flux emitting all Blog entities
34+
*/
1935
public Flux<Blog> getAllBlogs() {
2036
return blogRepository.findAll();
2137
}
2238

39+
/**
40+
* Retrieves a blog entry by its unique identifier.
41+
*
42+
* @param id the unique identifier of the blog
43+
* @return a Mono emitting the blog with the specified ID, or empty if not found
44+
*/
2345
public Mono<Blog> getBlogById(String id) {
2446
return blogRepository.findById(id);
2547
}
2648

49+
/**
50+
* Retrieves all blogs authored by the specified author.
51+
*
52+
* @param author the name of the author whose blogs are to be retrieved
53+
* @return a Flux emitting all Blog entries written by the given author
54+
*/
2755
public Flux<Blog> getBlogsByAuthor(String author) {
2856
return blogRepository.findByAuthor(author);
2957
}
3058

59+
/**
60+
* Updates an existing blog with new title, content, and author information.
61+
*
62+
* @param id the unique identifier of the blog to update
63+
* @param blog the blog object containing updated fields
64+
* @return a Mono emitting the updated Blog, or empty if the blog does not exist
65+
*/
3166
public Mono<Blog> updateBlog(String id, Blog blog) {
3267
return blogRepository.findById(id)
3368
.flatMap(existingBlog -> {
@@ -38,6 +73,12 @@ public Mono<Blog> updateBlog(String id, Blog blog) {
3873
});
3974
}
4075

76+
/**
77+
* Deletes the blog entry with the specified ID.
78+
*
79+
* @param id the unique identifier of the blog to delete
80+
* @return a Mono signaling completion when the blog is deleted
81+
*/
4182
public Mono<Void> deleteBlog(String id) {
4283
return blogRepository.deleteById(id);
4384
}

reactive/src/main/java/com/example/reactive/EpisodeController.java

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,15 @@ public class EpisodeController {
2929
private final Tracer tracer; // from micrometer
3030
private final Logger logger = LoggerFactory.getLogger(EpisodeController.class);
3131

32-
/* @GetMapping
33-
public Mono<ResponseEntity<List<CustomEpisode>>> getEpisodes() {
34-
// Manually create and start a span
35-
Span newSpan = tracer.nextSpan().name("fetch-episodes").start();
36-
try (Tracer.SpanInScope ws = tracer.withSpan(newSpan)) {
37-
38-
return webClient.get()
39-
.uri("https://apissa.sampleapis.com/futurama/episodexxs") // Intentionally faulty for error testing
40-
.retrieve()
41-
.bodyToFlux(EpisodeResponse.class)
42-
.map(ep -> new CustomEpisode(ep.title, ep.writers, ep.originalAirDate, ep.desc, ep.id))
43-
.collectList()
44-
.map(ResponseEntity::ok)
45-
.doOnSuccess(res -> {
46-
newSpan.tag("episodes.success", "true");
47-
newSpan.event("Successfully fetched episodes");
48-
newSpan.end();
49-
})
50-
.doOnError(e -> {
51-
newSpan.tag("episodes.success", "false");
52-
newSpan.error(e);
53-
newSpan.end();
54-
})
55-
.onErrorResume(e -> {
56-
String traceId = newSpan.context().traceId();
57-
58-
// logger.error("Error fetching episodes. Trace ID: {}", traceId, e);
59-
60-
CustomEpisode errorEpisode = new CustomEpisode(
61-
"Error occurred", "", "", "Trace ID: " + traceId + ", error: " + e.getMessage(), -1
62-
);
63-
return Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
64-
.body(Collections.singletonList(errorEpisode)));
65-
});
66-
67-
} catch (Exception e) {
68-
newSpan.error(e);
69-
newSpan.end();
70-
throw e;
71-
}
72-
}*/
32+
/**
33+
* Handles HTTP GET requests to retrieve a list of Futurama episodes from an external API, propagating the current trace ID for distributed tracing.
34+
*
35+
* If the external API call succeeds, returns a list of episodes wrapped in a 200 OK response. If an error occurs, returns a 500 response containing a single episode entry describing the error and including the trace ID for troubleshooting.
36+
*
37+
* The trace ID is propagated via Reactor's context and included in error responses and logs for observability.
38+
*
39+
* @return a reactive Mono emitting a ResponseEntity containing either the list of episodes or an error description with trace information
40+
*/
7341

7442
@GetMapping
7543
public Mono<ResponseEntity<List<CustomEpisode>>> getEpisodes() {
@@ -105,6 +73,14 @@ public Mono<ResponseEntity<List<CustomEpisode>>> getEpisodes() {
10573
}
10674

10775

76+
/**
77+
* Performs a GET request to an external API and returns a response containing the current trace ID.
78+
*
79+
* The trace ID from the current span is propagated through Reactor's context and included in the response.
80+
* On error, returns a 500 response with the error message and trace ID.
81+
*
82+
* @return a Mono emitting a ResponseEntity with a success or error message including the trace ID
83+
*/
10884
public Mono<ResponseEntity<String>> callApiWithTrace(WebClient webClient, Tracer tracer) {
10985
String traceId = tracer.currentSpan() != null
11086
? tracer.currentSpan().context().traceId()

reactive/src/main/java/com/example/reactive/ReactiveApplication.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
@SpringBootApplication
77
public class ReactiveApplication {
88

9+
/**
10+
* Launches the Spring Boot application.
11+
*
12+
* @param args command-line arguments passed to the application
13+
*/
914
public static void main(String[] args) {
1015
SpringApplication.run(ReactiveApplication.class, args);
1116
}

reactive/src/main/java/com/example/reactive/sample/CustomEpisode.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ public class CustomEpisode {
77
public String description;
88
public int id;
99

10+
/**
11+
* Constructs a CustomEpisode with the specified title, writers, publish date, description, and ID.
12+
*
13+
* @param title the episode's title
14+
* @param writers the episode's writers
15+
* @param publishDate the episode's publish date
16+
* @param description a brief description of the episode
17+
* @param id the unique identifier for the episode
18+
*/
1019
public CustomEpisode(String title, String writers, String publishDate, String description, int id) {
1120
this.title = title;
1221
this.writers = writers;

reactive/src/main/java/com/example/reactive/sample/TracingConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
@Configuration
1414
public class TracingConfig {
1515

16+
/**
17+
* Creates and configures a Tracer bean for distributed tracing using OpenTelemetry with span data exported to logs.
18+
*
19+
* @return a Tracer instance set up with a logging span exporter and current trace context management
20+
*/
1621
@Bean
1722
public Tracer tracer() {
1823
SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()

reactive/src/main/java/com/example/reactive/sample/WebClientConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
@Configuration
88
public class WebClientConfig {
99

10+
/**
11+
* Provides a configured {@link WebClient} bean for use in the application.
12+
*
13+
* @return a built {@link WebClient} instance
14+
*/
1015
@Bean
1116
public WebClient webClient(WebClient.Builder builder) {
1217
return builder.build();

0 commit comments

Comments
 (0)