2323import static org .springframework .web .reactive .function .BodyInserters .fromValue ;
2424import static org .springframework .web .reactive .function .server .ServerResponse .*;
2525
26+ /**
27+ * This class is responsible for handling HTTP requests related to the Person entity.
28+ * It includes methods for CRUD operations and finding a person by name.
29+ */
2630@ Component
2731@ RequiredArgsConstructor
2832@ Slf4j
2933public class PersonHandler {
3034 private final PersonRepository personRepository ;
3135 private final Validator validator ;
3236
37+ /**
38+ * Handles a request to get all persons.
39+ * @param serverRequest The incoming server request.
40+ * @return A ServerResponse with the list of all persons.
41+ */
3342 public Mono <ServerResponse > handleFindAll (ServerRequest serverRequest ) {
3443 return ok ()
3544 .body (this .personRepository .findAll (), Person .class );
3645 }
3746
47+ /**
48+ * Handles a request to get a person by id.
49+ * @param serverRequest The incoming server request.
50+ * @return A ServerResponse with the person found or a 404 status if not found.
51+ */
3852 public Mono <ServerResponse > handleFindById (ServerRequest serverRequest ) {
3953 var id = Long .parseLong (serverRequest .pathVariable ("id" ));
4054 return this .personRepository .findById (id )
@@ -43,6 +57,11 @@ public Mono<ServerResponse> handleFindById(ServerRequest serverRequest) {
4357 .switchIfEmpty (notFound ().build ());
4458 }
4559
60+ /**
61+ * Handles a request to get the first person found by name.
62+ * @param serverRequest The incoming server request.
63+ * @return A ServerResponse with the person found or a 404 status if not found.
64+ */
4665 public Mono <ServerResponse > handleFindFirstByName (ServerRequest serverRequest ) {
4766 log .info ("Handle request {} {}" , serverRequest .method (), serverRequest .path ());
4867 var name = serverRequest .pathVariable ("name" );
@@ -53,6 +72,11 @@ public Mono<ServerResponse> handleFindFirstByName(ServerRequest serverRequest) {
5372 .switchIfEmpty (notFound );
5473 }
5574
75+ /**
76+ * Handles a request to delete a person by id.
77+ * @param serverRequest The incoming server request.
78+ * @return A ServerResponse with a success message or a 404 status if not found.
79+ */
5680 public Mono <ServerResponse > handleDeleteById (ServerRequest serverRequest ) {
5781 var id = Long .parseLong (serverRequest .pathVariable ("id" ));
5882 return this .personRepository .findById (id )
@@ -64,6 +88,11 @@ public Mono<ServerResponse> handleDeleteById(ServerRequest serverRequest) {
6488 .bodyValue (msg ));
6589 }
6690
91+ /**
92+ * Handles a request to create a new person.
93+ * @param serverRequest The incoming server request.
94+ * @return A ServerResponse with the created person or a 400 status if the request body is invalid.
95+ */
6796 public Mono <ServerResponse > handleCreate (ServerRequest serverRequest ) {
6897 return serverRequest .bodyToMono (Person .class )
6998 .switchIfEmpty (Mono .error (new ServerWebInputException ("person must not be null" )))
@@ -74,6 +103,11 @@ public Mono<ServerResponse> handleCreate(ServerRequest serverRequest) {
74103 .bodyValue (person ));
75104 }
76105
106+ /**
107+ * Handles a request to update a person by id.
108+ * @param serverRequest The incoming server request.
109+ * @return A ServerResponse with the updated person or a 404 status if not found.
110+ */
77111 public Mono <ServerResponse > handleUpdate (ServerRequest serverRequest ) {
78112 var id = Long .parseLong (serverRequest .pathVariable ("id" ));
79113 final Mono <Person > update = serverRequest .bodyToMono (Person .class )
@@ -90,6 +124,11 @@ public Mono<ServerResponse> handleUpdate(ServerRequest serverRequest) {
90124 .switchIfEmpty (notFound ().build ());
91125 }
92126
127+ /**
128+ * Validates a person using the Validator.
129+ * @param person The person to validate.
130+ * @throws ServerWebInputException If the person is not valid.
131+ */
93132 private void validate (Person person ) {
94133 Set <ConstraintViolation <Person >> violations = this .validator .validate (person );
95134 if (!violations .isEmpty ()) {
@@ -101,9 +140,14 @@ private void validate(Person person) {
101140 }
102141 }
103142
143+ /**
144+ * Formats a validation error.
145+ * @param personConstraintViolation The validation error.
146+ * @return A string representation of the validation error.
147+ */
104148 private String formatError (ConstraintViolation <Person > personConstraintViolation ) {
105149 String field = StringUtils .capitalize (personConstraintViolation .getPropertyPath ().toString ());
106150 String error = personConstraintViolation .getMessage ();
107151 return String .format ("%s - %s" , field , error );
108152 }
109- }
153+ }
0 commit comments