Move most code from controller to service

This commit is contained in:
Mateusz Słodkowicz 2024-03-11 15:01:21 +01:00
parent d1c233068c
commit 94d617ba04
Signed by: materus
GPG Key ID: 28D140BCA60B4FD1
2 changed files with 47 additions and 32 deletions

View File

@ -7,7 +7,6 @@ import pl.materus.ghrepo.service.GitHubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ -44,41 +43,13 @@ public class GitHubController {
@ExceptionHandler(WebClientResponseException.class)
public ResponseEntity<ResponseErrorModel> handleResponseException(WebClientResponseException ex) {
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
String message;
Integer statusCode = ex.getStatusCode().value();
switch (statusCode) {
case 404:
message = "User not found";
status = HttpStatus.NOT_FOUND;
break;
case 403:
message = "Github API limit";
status = HttpStatus.FORBIDDEN;
break;
default:
message = "Unknown github webclient error";
break;
}
ResponseErrorModel responseErrorModel = new ResponseErrorModel(statusCode,message);
return ResponseEntity.status(status).headers(httpHeaders).body(responseErrorModel);
return gitHubService.handleWebClientException(ex);
}
@ExceptionHandler(WrongHeaderException.class)
public ResponseEntity<ResponseErrorModel> handleResponseException(WrongHeaderException ex) {
ResponseErrorModel responseErrorModel = new ResponseErrorModel(HttpStatus.BAD_REQUEST.value(),ex.getMessage());
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return ResponseEntity.badRequest().headers(httpHeaders).body(responseErrorModel);
return gitHubService.handleWrongHeaderException(ex);
}
}

View File

@ -1,12 +1,19 @@
package pl.materus.ghrepo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import pl.materus.ghrepo.exception.WrongHeaderException;
import pl.materus.ghrepo.model.GitHubBranchModel;
import pl.materus.ghrepo.model.GitHubRepositoryModel;
import pl.materus.ghrepo.model.ResponseBranchModel;
import pl.materus.ghrepo.model.ResponseErrorModel;
import pl.materus.ghrepo.model.ResponseRepositoryModel;
import reactor.core.publisher.Flux;
@ -39,6 +46,43 @@ public class GitHubService {
}
public ResponseEntity<ResponseErrorModel> handleWebClientException(WebClientResponseException ex) {
HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
String message;
Integer statusCode = ex.getStatusCode().value();
switch (statusCode) {
case 404:
message = "User not found";
status = HttpStatus.NOT_FOUND;
break;
case 403:
message = "Github API limit";
status = HttpStatus.FORBIDDEN;
break;
default:
message = "Unknown github webclient error";
break;
}
ResponseErrorModel responseErrorModel = new ResponseErrorModel(statusCode, message);
return ResponseEntity.status(status).headers(httpHeaders).body(responseErrorModel);
}
public ResponseEntity<ResponseErrorModel> handleWrongHeaderException(WrongHeaderException ex) {
ResponseErrorModel responseErrorModel = new ResponseErrorModel(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return ResponseEntity.badRequest().headers(httpHeaders).body(responseErrorModel);
}
private Flux<ResponseBranchModel> getRepoBranches(String owner, String repo) {
return githubWebClient.get()
.uri("/repos/{owner}/{repo}/branches", owner, repo)
@ -49,7 +93,7 @@ public class GitHubService {
}
private Mono<String[]> createResponse(GitHubRepositoryModel repository) {
String[] ret = { repository.owner().login(),repository.name() };
String[] ret = { repository.owner().login(), repository.name() };
return Mono.just(ret);
}