Skip to content

Commit

Permalink
CHORE: Removing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Janek1010 committed Feb 15, 2024
1 parent 83df7ed commit 1ca4c4c
Show file tree
Hide file tree
Showing 8 changed files with 1 addition and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,19 @@
import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Flux;

/**
* Class that handles the requests to the Github API.
*/
@RestController
@RequiredArgsConstructor
public class GithubController {
private final String REPOS_OF_USER = "/api/v1/users/{username}/repos";
private final GithubService githubService;

/**
* Retrieves a list of all repositories owned by the specified GitHub user.
* This version of the API does not require authorization, allowing up to 60 requests per hour.
*
* @param username GitHub username.
* @param page Page number to be returned.
* @param perPage Number of records per page; GitHub handles a maximum of 100.
* @return A stream of repositories in JSON format, including owner login, repository name, and branches.
* Returns 404 if the user is not found, 200 OK if the user is found.
* @see <a href="https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repositories-for-a-user">documentation of endpoint</a>
*/
@GetMapping(value = REPOS_OF_USER, produces = MediaType.APPLICATION_JSON_VALUE)
public Flux<RepositoryDTO> listAllRepositoriesOfUser(@PathVariable String username,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "30") int perPage) {
return githubService.listAllRepositoriesOfUser(username, page, perPage);
}

/**
* The method is responsible for returning the error message in a different format.
*/
@ExceptionHandler(ResponseStatusException.class)
public ResponseEntity<UserNotFoundErrorResponseDTO> handleResponseStatusException(ResponseStatusException ex) {
return ResponseEntity.status(ex.getStatusCode()).body(UserNotFoundErrorResponseDTO
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/org/example/githubservice/model/dtos/BranchDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,13 @@
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Represents a branch of a repository.
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class BranchDTO {

/**
* Name of the branch.
*/
private String name;

/**
* Last commit associated with the branch.
*/
private CommitDTO commit;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Represents a commit in a repository.
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CommitDTO {

/**
* SHA of the LAST commit.
*/
@JsonValue
@JsonProperty("sha")
private String sha;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Represents the owner of a repository.
*/
@Data
@Builder
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,19 @@

import java.util.List;

/**
* Represents a repository on GitHub.
*/
@Data
@Builder
public class RepositoryDTO {
/**
* Name of the repository.
*/
private String name;

/**
* Owner of the repository. In the response there will be presented value 'login' from OwnerDTO as ownerLogin
*/
@JsonProperty("ownerLogin")
private OwnerDTO owner;

/**
* Indicates if the repository is a fork. Invisible in a response
*/
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private boolean fork;

/**
* URL to list branches of the repository. Invisible in a response
*/
@JsonProperty(value = "branches_url", access = JsonProperty.Access.WRITE_ONLY)
private String branchesUrl;

/**
* List of branches of the repository.
*/
private List<BranchDTO> branches;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,11 @@
import lombok.Data;
import org.springframework.http.HttpStatusCode;

/**
* Represents a customized error response for user not found.
*/
@Data
@Builder
public class UserNotFoundErrorResponseDTO {
/**
* HTTP status code of the error response.
*/

private HttpStatusCode status;

/**
* Error message for user not found.
*/
private String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,7 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
* Service interface for interacting with the GitHub API.
*/
public interface GithubService {
/**
* Retrieves a list of all repositories owned by the specified user.
*
* @param username GitHub username of the user.
* @param page Page number to be returned.
* @param perPage Number of records per page.
* @return A flux of RepositoryDTO representing the repositories.
*/
Flux<RepositoryDTO> listAllRepositoriesOfUser(String username, int page, int perPage);

/**
* Retrieves a list of all branches for the specified repository.
*
* @param repositoryDTO The RepositoryDTO for which to retrieve branches.
* @return A mono of RepositoryDTO representing the repository with branches.
*/
Mono<RepositoryDTO> listAllBranches(RepositoryDTO repositoryDTO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
* Implementation of the GithubService interface for interacting with the GitHub API.
*/
@Service
@Primary
public class GithubServiceImpl implements GithubService {
Expand All @@ -34,13 +31,10 @@ public Flux<RepositoryDTO> listAllRepositoriesOfUser(String username, int page,
.build(username))
.accept(MediaType.APPLICATION_JSON)
.retrieve()
// Handles response statuses, in this case 404 (NOT_FOUND).
.onStatus(status -> status.value() == HttpStatus.NOT_FOUND.value(),
clientResponse -> Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found")))
// Maps the response body to a Flux<RepositoryDTO> stream.
.bodyToFlux(RepositoryDTO.class)
.filter(repositoryDTO -> !repositoryDTO.isFork())
// For each repository, calls the listAllBranches method to retrieve a list of branches.
.flatMap(this::listAllBranches);
}

Expand All @@ -53,7 +47,6 @@ public Mono<RepositoryDTO> listAllBranches(RepositoryDTO repositoryDTO) {
.retrieve()
.bodyToFlux(BranchDTO.class)
.collectList()
// Collected branches are mapped to a list and assigned to the repository.
.map(branches -> {
repositoryDTO.setBranches(branches);
return repositoryDTO;
Expand Down

0 comments on commit 1ca4c4c

Please sign in to comment.