From 0e5229520e95bf5d9d0b70e2753fa370aa535efa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Mon, 7 Oct 2024 15:58:38 +0200 Subject: [PATCH] PMT-16: Implement New Endpoint Logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rajbir Singh Signed-off-by: Dominik Säume --- src/main/java/de/hmmh/pmt/ApiController.java | 44 +++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index bc05059..51799b5 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -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 getObjectMapper() { @@ -49,7 +55,7 @@ public class ApiController implements DefaultApi { public ResponseEntity 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 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); + } }