PMT-16: Implement New Endpoint Logic
Co-authored-by: Rajbir Singh <rajbir.singh@hmmh.de> Signed-off-by: Dominik Säume <Dominik.Saeume@hmmh.de>
This commit is contained in:
parent
ec9b930386
commit
21df04fbc6
1 changed files with 39 additions and 5 deletions
|
@ -1,30 +1,36 @@
|
||||||
package de.hmmh.pmt;
|
package de.hmmh.pmt;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import de.hmmh.pmt.employee.ApiClientFactory;
|
|
||||||
import de.hmmh.pmt.db.Project;
|
import de.hmmh.pmt.db.Project;
|
||||||
import de.hmmh.pmt.db.ProjectRepository;
|
import de.hmmh.pmt.db.ProjectRepository;
|
||||||
import de.hmmh.pmt.oas.DefaultApi;
|
import de.hmmh.pmt.dtos.CreateProjectDTO;
|
||||||
|
import de.hmmh.pmt.dtos.CreatedProjectDTO;
|
||||||
import de.hmmh.pmt.dtos.GetAllProjectsDTO;
|
import de.hmmh.pmt.dtos.GetAllProjectsDTO;
|
||||||
import de.hmmh.pmt.dtos.ProjectInfo;
|
import de.hmmh.pmt.dtos.ProjectInfo;
|
||||||
|
import de.hmmh.pmt.employee.ApiClientFactory;
|
||||||
|
import de.hmmh.pmt.oas.DefaultApi;
|
||||||
|
import de.hmmh.pmt.util.Mapper;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
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.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("${openapi.projectManagement.base-path:/api/v1}")
|
@RequestMapping("${openapi.projectManagement.base-path:/api/v1}")
|
||||||
public class ApiController implements DefaultApi {
|
public class ApiController implements DefaultApi {
|
||||||
|
@Autowired
|
||||||
|
private Mapper mapper;
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApiClientFactory apiClientFactory;
|
private ApiClientFactory apiClientFactory;
|
||||||
@Autowired
|
@Autowired
|
||||||
private ProjectRepository projectRepository;
|
private ProjectRepository projectRepository;
|
||||||
|
|
||||||
// apiClientFactory.getEmployeeApi().findAll1()
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<ObjectMapper> getObjectMapper() {
|
public Optional<ObjectMapper> getObjectMapper() {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
@ -39,7 +45,7 @@ public class ApiController implements DefaultApi {
|
||||||
public ResponseEntity<GetAllProjectsDTO> getAllProjects() {
|
public ResponseEntity<GetAllProjectsDTO> getAllProjects() {
|
||||||
GetAllProjectsDTO response = new GetAllProjectsDTO();
|
GetAllProjectsDTO response = new GetAllProjectsDTO();
|
||||||
|
|
||||||
for (Project project : this.projectRepository.findAll()){
|
for (Project project : this.projectRepository.findAll()) {
|
||||||
ProjectInfo projectInfo = new ProjectInfo();
|
ProjectInfo projectInfo = new ProjectInfo();
|
||||||
projectInfo.setId(project.getId());
|
projectInfo.setId(project.getId());
|
||||||
projectInfo.setName(project.getName());
|
projectInfo.setName(project.getName());
|
||||||
|
@ -48,4 +54,32 @@ public class ApiController implements DefaultApi {
|
||||||
|
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ResponseEntity<CreatedProjectDTO> createProject(CreateProjectDTO body) {
|
||||||
|
if (projectRepository.existsByName(body.getName())) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
apiClientFactory.getEmployeeApi().findById(body.getAdministratorId());
|
||||||
|
} catch (HttpClientErrorException exception) {
|
||||||
|
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);
|
||||||
|
if (!project.isValid()) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||||
|
}
|
||||||
|
projectRepository.save(project);
|
||||||
|
|
||||||
|
CreatedProjectDTO response = mapper.map(project);
|
||||||
|
return new ResponseEntity<>(response, HttpStatus.CREATED);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue