Skip to content

Commit

Permalink
REFACTORING: Changing lombok to java records
Browse files Browse the repository at this point in the history
  • Loading branch information
Janek1010 committed Feb 15, 2024
1 parent dad2ce9 commit bfdad03
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,8 +26,7 @@ public Flux<RepositoryDTO> listAllRepositoriesOfUser(@PathVariable String userna

@ExceptionHandler(ResponseStatusException.class)
public ResponseEntity<UserNotFoundErrorResponseDTO> 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()));
}
}
19 changes: 4 additions & 15 deletions src/main/java/org/example/githubservice/model/dtos/BranchDTO.java
Original file line number Diff line number Diff line change
@@ -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
){}
19 changes: 5 additions & 14 deletions src/main/java/org/example/githubservice/model/dtos/CommitDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {}
18 changes: 5 additions & 13 deletions src/main/java/org/example/githubservice/model/dtos/OwnerDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {}
Original file line number Diff line number Diff line change
@@ -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<BranchDTO> 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<BranchDTO> branches
) {}
Original file line number Diff line number Diff line change
@@ -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
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,20 @@ public Flux<RepositoryDTO> 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<RepositoryDTO> 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));
}

}

0 comments on commit bfdad03

Please sign in to comment.