From bfdad0369c51f7115e7e672d76892f70478ca165 Mon Sep 17 00:00:00 2001 From: Janek1010 Date: Thu, 15 Feb 2024 16:30:52 +0100 Subject: [PATCH] REFACTORING: Changing lombok to java records --- .../controller/GithubController.java | 10 ++++--- .../githubservice/model/dtos/BranchDTO.java | 19 +++---------- .../githubservice/model/dtos/CommitDTO.java | 19 ++++--------- .../githubservice/model/dtos/OwnerDTO.java | 18 ++++--------- .../model/dtos/RepositoryDTO.java | 27 +++++++------------ .../dtos/UserNotFoundErrorResponseDTO.java | 14 +++------- .../service/impl/GithubServiceImpl.java | 9 +++---- 7 files changed, 37 insertions(+), 79 deletions(-) diff --git a/src/main/java/org/example/githubservice/controller/GithubController.java b/src/main/java/org/example/githubservice/controller/GithubController.java index 3df664b..12675c5 100644 --- a/src/main/java/org/example/githubservice/controller/GithubController.java +++ b/src/main/java/org/example/githubservice/controller/GithubController.java @@ -6,7 +6,10 @@ import org.example.githubservice.service.api.GithubService; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; import org.springframework.web.server.ResponseStatusException; import reactor.core.publisher.Flux; @@ -23,8 +26,7 @@ public Flux listAllRepositoriesOfUser(@PathVariable String userna @ExceptionHandler(ResponseStatusException.class) public ResponseEntity handleResponseStatusException(ResponseStatusException ex) { - return ResponseEntity.status(ex.getStatusCode()).body(UserNotFoundErrorResponseDTO - .builder().message(ex.getReason()).status(ex.getStatusCode()) - .build()); + return ResponseEntity.status(ex.getStatusCode()) + .body(new UserNotFoundErrorResponseDTO(ex.getStatusCode(), ex.getReason())); } } diff --git a/src/main/java/org/example/githubservice/model/dtos/BranchDTO.java b/src/main/java/org/example/githubservice/model/dtos/BranchDTO.java index 44a0c3a..68a69d8 100644 --- a/src/main/java/org/example/githubservice/model/dtos/BranchDTO.java +++ b/src/main/java/org/example/githubservice/model/dtos/BranchDTO.java @@ -1,17 +1,6 @@ package org.example.githubservice.model.dtos; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@Builder -@AllArgsConstructor -@NoArgsConstructor -public class BranchDTO { - - private String name; - private CommitDTO commit; - -} +public record BranchDTO ( + String name, + CommitDTO commit +){} diff --git a/src/main/java/org/example/githubservice/model/dtos/CommitDTO.java b/src/main/java/org/example/githubservice/model/dtos/CommitDTO.java index 1648516..84d2b36 100644 --- a/src/main/java/org/example/githubservice/model/dtos/CommitDTO.java +++ b/src/main/java/org/example/githubservice/model/dtos/CommitDTO.java @@ -2,19 +2,10 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonValue; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; -@Data -@Builder -@AllArgsConstructor -@NoArgsConstructor -public class CommitDTO { - @JsonValue - @JsonProperty("sha") - private String sha; - -} +public record CommitDTO( + @JsonValue + @JsonProperty("sha") + String sha +) {} diff --git a/src/main/java/org/example/githubservice/model/dtos/OwnerDTO.java b/src/main/java/org/example/githubservice/model/dtos/OwnerDTO.java index e9ef993..5a30a6c 100644 --- a/src/main/java/org/example/githubservice/model/dtos/OwnerDTO.java +++ b/src/main/java/org/example/githubservice/model/dtos/OwnerDTO.java @@ -2,17 +2,9 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonValue; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; -@Data -@Builder -@AllArgsConstructor -@NoArgsConstructor -public class OwnerDTO { - @JsonValue - @JsonProperty("login") - private String login; -} +public record OwnerDTO ( + @JsonValue + @JsonProperty("login") + String login +) {} diff --git a/src/main/java/org/example/githubservice/model/dtos/RepositoryDTO.java b/src/main/java/org/example/githubservice/model/dtos/RepositoryDTO.java index 6b5a705..1e842ee 100644 --- a/src/main/java/org/example/githubservice/model/dtos/RepositoryDTO.java +++ b/src/main/java/org/example/githubservice/model/dtos/RepositoryDTO.java @@ -1,24 +1,17 @@ package org.example.githubservice.model.dtos; import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Builder; -import lombok.Data; import java.util.List; -@Data -@Builder -public class RepositoryDTO { - private String name; - @JsonProperty("ownerLogin") - private OwnerDTO owner; - - @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) - private boolean fork; - - @JsonProperty(value = "branches_url", access = JsonProperty.Access.WRITE_ONLY) - private String branchesUrl; - - private List branches; -} +public record RepositoryDTO( + String name, + //@JsonProperty("ownerLogin") + OwnerDTO owner, + @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) + boolean fork, + @JsonProperty(value = "branches_url", access = JsonProperty.Access.WRITE_ONLY) + String branchesUrl, + List branches +) {} diff --git a/src/main/java/org/example/githubservice/model/dtos/UserNotFoundErrorResponseDTO.java b/src/main/java/org/example/githubservice/model/dtos/UserNotFoundErrorResponseDTO.java index 6ed8aac..cb6e84b 100644 --- a/src/main/java/org/example/githubservice/model/dtos/UserNotFoundErrorResponseDTO.java +++ b/src/main/java/org/example/githubservice/model/dtos/UserNotFoundErrorResponseDTO.java @@ -1,14 +1,8 @@ package org.example.githubservice.model.dtos; -import lombok.Builder; -import lombok.Data; import org.springframework.http.HttpStatusCode; -@Data -@Builder -public class UserNotFoundErrorResponseDTO { - - private HttpStatusCode status; - - private String message; -} +public record UserNotFoundErrorResponseDTO ( + HttpStatusCode status, + String message +) {} diff --git a/src/main/java/org/example/githubservice/service/impl/GithubServiceImpl.java b/src/main/java/org/example/githubservice/service/impl/GithubServiceImpl.java index 5b40e16..b7993fd 100644 --- a/src/main/java/org/example/githubservice/service/impl/GithubServiceImpl.java +++ b/src/main/java/org/example/githubservice/service/impl/GithubServiceImpl.java @@ -32,23 +32,20 @@ public Flux listAllRepositoriesOfUser(String username) { .onStatus(status -> status.value() == HttpStatus.NOT_FOUND.value(), clientResponse -> Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"))) .bodyToFlux(RepositoryDTO.class) - .filter(repositoryDTO -> !repositoryDTO.isFork()) + .filter(repositoryDTO -> !repositoryDTO.fork()) .flatMap(this::listAllBranches); } @Override public Mono listAllBranches(RepositoryDTO repositoryDTO) { - String uri = repositoryDTO.getBranchesUrl().replace("{/branch}", ""); + String uri = repositoryDTO.branchesUrl().replace("{/branch}", ""); return webClient.get() .uri(uri) .accept(MediaType.APPLICATION_JSON) .retrieve() .bodyToFlux(BranchDTO.class) .collectList() - .map(branches -> { - repositoryDTO.setBranches(branches); - return repositoryDTO; - }); + .map(branches -> new RepositoryDTO(repositoryDTO.name(), repositoryDTO.owner(), repositoryDTO.fork(), repositoryDTO.branchesUrl(), branches)); } }