From 63f7fe04dc874e2da58152b536a6a48632d25e77 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 5031841..07b815d 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() { @@ -39,7 +45,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()); @@ -48,4 +54,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); + } }