PMT-16: Projekt Anlegen #12

Merged
SZUT-Ole merged 10 commits from story/PMT-16-projekt-anlegen into trunk 2024-10-15 08:15:23 +00:00
Showing only changes of commit 5b601d20d7 - Show all commits

View file

@ -1,29 +1,35 @@
package de.hmmh.pmt;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.hmmh.pmt.employee.ApiClientFactory;
import de.hmmh.pmt.db.Project;
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.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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import java.util.Optional;
@Controller
@RequestMapping("${openapi.projectManagement.base-path:/api/v1}")
public class ApiController implements DefaultApi {
@Autowired
private Mapper mapper;
@Autowired
private ApiClientFactory apiClientFactory;
@Autowired
private ProjectRepository projectRepository;
// apiClientFactory.getEmployeeApi().findAll1()
@Override
public Optional<ObjectMapper> getObjectMapper() {
@ -49,7 +55,7 @@ public class ApiController implements DefaultApi {
public ResponseEntity<GetAllProjectsDTO> getAllProjects() {
GetAllProjectsDTO response = new GetAllProjectsDTO();
for (Project project : this.projectRepository.findAll()){
for (Project project : this.projectRepository.findAll()) {
ProjectInfo projectInfo = new ProjectInfo();
projectInfo.setId(project.getId());
projectInfo.setName(project.getName());
@ -58,4 +64,32 @@ public class ApiController implements DefaultApi {
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);
}
}