PMT-43: Separate Repeated API Logic into ApiTools
This commit is contained in:
parent
21040c69b6
commit
95ff26473a
2 changed files with 44 additions and 23 deletions
|
@ -9,6 +9,7 @@ import de.hmmh.pmt.dtos.*;
|
||||||
import de.hmmh.pmt.employee.ApiClientFactory;
|
import de.hmmh.pmt.employee.ApiClientFactory;
|
||||||
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
|
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
|
||||||
import de.hmmh.pmt.oas.DefaultApi;
|
import de.hmmh.pmt.oas.DefaultApi;
|
||||||
|
import de.hmmh.pmt.util.ApiTools;
|
||||||
import de.hmmh.pmt.util.Mapper;
|
import de.hmmh.pmt.util.Mapper;
|
||||||
import de.hmmh.pmt.util.Validator;
|
import de.hmmh.pmt.util.Validator;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
@ -17,11 +18,7 @@ import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.client.HttpClientErrorException;
|
|
||||||
import org.springframework.web.client.RestClientException;
|
|
||||||
|
|
||||||
import java.time.ZoneOffset;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
|
@ -80,16 +77,9 @@ public class ApiController implements DefaultApi {
|
||||||
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
HttpStatus status = apiTools.checkEmployeeExists(body.getAdministratorId()).status();
|
||||||
apiClientFactory.getEmployeeApi().findById(body.getAdministratorId());
|
if (status != HttpStatus.OK) {
|
||||||
} catch (HttpClientErrorException exception) {
|
return new ResponseEntity<>(status);
|
||||||
return new ResponseEntity<>(
|
|
||||||
exception.getStatusCode().equals(HttpStatus.NOT_FOUND)
|
|
||||||
? HttpStatus.NOT_FOUND
|
|
||||||
: HttpStatus.SERVICE_UNAVAILABLE
|
|
||||||
);
|
|
||||||
} catch (RestClientException exception) {
|
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Project project = mapper.map(body);
|
Project project = mapper.map(body);
|
||||||
|
@ -110,16 +100,13 @@ public class ApiController implements DefaultApi {
|
||||||
}
|
}
|
||||||
Project project = optionalProject.get();
|
Project project = optionalProject.get();
|
||||||
|
|
||||||
EmployeeResponseDTO employee;
|
|
||||||
try {
|
ApiTools.CheckEmployeeRecord employeeRecord = apiTools.checkEmployeeExists(body.getEmployeeId());
|
||||||
employee = apiClientFactory.getEmployeeApi().findById(body.getEmployeeId());
|
if (employeeRecord.status() != HttpStatus.OK) {
|
||||||
} catch (HttpClientErrorException exception) {
|
return new ResponseEntity<>(employeeRecord.status());
|
||||||
return new ResponseEntity<>(exception.getStatusCode().equals(HttpStatus.NOT_FOUND)
|
|
||||||
? HttpStatus.NOT_FOUND
|
|
||||||
: HttpStatus.SERVICE_UNAVAILABLE);
|
|
||||||
} catch (RestClientException exception) {
|
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
}
|
||||||
|
EmployeeResponseDTO employee = employeeRecord.employee();
|
||||||
|
|
||||||
|
|
||||||
if (employee.getSkillSet()
|
if (employee.getSkillSet()
|
||||||
.stream()
|
.stream()
|
||||||
|
|
34
src/main/java/de/hmmh/pmt/util/ApiTools.java
Normal file
34
src/main/java/de/hmmh/pmt/util/ApiTools.java
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package de.hmmh.pmt.util;
|
||||||
|
|
||||||
|
import de.hmmh.pmt.employee.ApiClientFactory;
|
||||||
|
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
|
import org.springframework.web.client.RestClientException;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ApiTools {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApiClientFactory apiClientFactory;
|
||||||
|
|
||||||
|
public record CheckEmployeeRecord(EmployeeResponseDTO employee, HttpStatus status) {}
|
||||||
|
public CheckEmployeeRecord checkEmployeeExists(long id) {
|
||||||
|
EmployeeResponseDTO employee;
|
||||||
|
try {
|
||||||
|
employee =apiClientFactory.getEmployeeApi().findById(id);
|
||||||
|
} catch (HttpClientErrorException exception) {
|
||||||
|
return new CheckEmployeeRecord(
|
||||||
|
null,
|
||||||
|
exception.getStatusCode().equals(HttpStatus.NOT_FOUND)
|
||||||
|
? HttpStatus.NOT_FOUND
|
||||||
|
: HttpStatus.SERVICE_UNAVAILABLE
|
||||||
|
);
|
||||||
|
} catch (RestClientException exception) {
|
||||||
|
return new CheckEmployeeRecord(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
return new CheckEmployeeRecord(employee, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue