From c5eeabbdff58ca43d1ae10a6fdb463a53698af7c Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 2 Oct 2024 12:31:59 +0200 Subject: [PATCH] WIP --- requests/employee/createEmployee.http | 12 +++++ requests/employee/deleteEmployee.http | 2 + requests/employee/getAllEmployees.http | 2 + requests/employee/getEmployee.http | 2 + requests/project/addEmployeeToProject.http | 2 + requests/{ => project}/createProject.http | 0 requests/{ => project}/getAllProjects.http | 0 requests/{ => project}/getProject.http | 0 requests/{ => project}/updateProject.http | 0 .../{ => crud}/CreateProjectAction.java | 2 +- .../{ => crud}/GetAllProjectsAction.java | 2 +- .../action/{ => crud}/GetProjectAction.java | 7 ++- .../{ => crud}/UpdateProjectAction.java | 2 +- .../employee/AddEmployeeToProjectAction.java | 49 +++++++++++++++++++ .../project/dto/BearerTokenResponseDto.java | 15 ++++++ .../security/AccessTokenService.java | 36 ++++++++++++++ 16 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 requests/employee/createEmployee.http create mode 100644 requests/employee/deleteEmployee.http create mode 100644 requests/employee/getAllEmployees.http create mode 100644 requests/employee/getEmployee.http create mode 100644 requests/project/addEmployeeToProject.http rename requests/{ => project}/createProject.http (100%) rename requests/{ => project}/getAllProjects.http (100%) rename requests/{ => project}/getProject.http (100%) rename requests/{ => project}/updateProject.http (100%) rename src/main/java/de/szut/lf8_starter/project/action/{ => crud}/CreateProjectAction.java (97%) rename src/main/java/de/szut/lf8_starter/project/action/{ => crud}/GetAllProjectsAction.java (97%) rename src/main/java/de/szut/lf8_starter/project/action/{ => crud}/GetProjectAction.java (83%) rename src/main/java/de/szut/lf8_starter/project/action/{ => crud}/UpdateProjectAction.java (95%) create mode 100644 src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java create mode 100644 src/main/java/de/szut/lf8_starter/project/dto/BearerTokenResponseDto.java create mode 100644 src/main/java/de/szut/lf8_starter/security/AccessTokenService.java diff --git a/requests/employee/createEmployee.http b/requests/employee/createEmployee.http new file mode 100644 index 0000000..c9f51b4 --- /dev/null +++ b/requests/employee/createEmployee.http @@ -0,0 +1,12 @@ +POST https://employee.szut.dev/employees +Authorization: Bearer {{auth_token}} +Content-Type: application/json + +{ + "firstName": "yomom", + "lastName": "yomom", + "street": "yomom", + "postcode": "yomom", + "city": "yomom", + "phone": "yomom" +} \ No newline at end of file diff --git a/requests/employee/deleteEmployee.http b/requests/employee/deleteEmployee.http new file mode 100644 index 0000000..cf1d827 --- /dev/null +++ b/requests/employee/deleteEmployee.http @@ -0,0 +1,2 @@ +DELETE https://employee.szut.dev/employees/310 +Authorization: Bearer {{auth_token}} \ No newline at end of file diff --git a/requests/employee/getAllEmployees.http b/requests/employee/getAllEmployees.http new file mode 100644 index 0000000..c3911be --- /dev/null +++ b/requests/employee/getAllEmployees.http @@ -0,0 +1,2 @@ +GET https://employee.szut.dev/employees +Authorization: Bearer {{auth_token}} \ No newline at end of file diff --git a/requests/employee/getEmployee.http b/requests/employee/getEmployee.http new file mode 100644 index 0000000..eddf4d9 --- /dev/null +++ b/requests/employee/getEmployee.http @@ -0,0 +1,2 @@ +GET https://employee.szut.dev/employees/312 +Authorization: Bearer {{auth_token}} \ No newline at end of file diff --git a/requests/project/addEmployeeToProject.http b/requests/project/addEmployeeToProject.http new file mode 100644 index 0000000..96500fc --- /dev/null +++ b/requests/project/addEmployeeToProject.http @@ -0,0 +1,2 @@ +POST http://localhost:8080/projects/1/employees/1 +Authorization: Bearer {{auth_token}} \ No newline at end of file diff --git a/requests/createProject.http b/requests/project/createProject.http similarity index 100% rename from requests/createProject.http rename to requests/project/createProject.http diff --git a/requests/getAllProjects.http b/requests/project/getAllProjects.http similarity index 100% rename from requests/getAllProjects.http rename to requests/project/getAllProjects.http diff --git a/requests/getProject.http b/requests/project/getProject.http similarity index 100% rename from requests/getProject.http rename to requests/project/getProject.http diff --git a/requests/updateProject.http b/requests/project/updateProject.http similarity index 100% rename from requests/updateProject.http rename to requests/project/updateProject.http diff --git a/src/main/java/de/szut/lf8_starter/project/action/CreateProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java similarity index 97% rename from src/main/java/de/szut/lf8_starter/project/action/CreateProjectAction.java rename to src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java index 3e636b5..f21646d 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/CreateProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java @@ -1,4 +1,4 @@ -package de.szut.lf8_starter.project.action; +package de.szut.lf8_starter.project.action.crud; import de.szut.lf8_starter.project.ProjectEntity; import de.szut.lf8_starter.project.ProjectMapper; diff --git a/src/main/java/de/szut/lf8_starter/project/action/GetAllProjectsAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/GetAllProjectsAction.java similarity index 97% rename from src/main/java/de/szut/lf8_starter/project/action/GetAllProjectsAction.java rename to src/main/java/de/szut/lf8_starter/project/action/crud/GetAllProjectsAction.java index 039a104..5fb346a 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/GetAllProjectsAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/GetAllProjectsAction.java @@ -1,4 +1,4 @@ -package de.szut.lf8_starter.project.action; +package de.szut.lf8_starter.project.action.crud; import de.szut.lf8_starter.project.ProjectMapper; import de.szut.lf8_starter.project.ProjectService; diff --git a/src/main/java/de/szut/lf8_starter/project/action/GetProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/GetProjectAction.java similarity index 83% rename from src/main/java/de/szut/lf8_starter/project/action/GetProjectAction.java rename to src/main/java/de/szut/lf8_starter/project/action/crud/GetProjectAction.java index d8e4207..eba31c1 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/GetProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/GetProjectAction.java @@ -1,4 +1,4 @@ -package de.szut.lf8_starter.project.action; +package de.szut.lf8_starter.project.action.crud; import de.szut.lf8_starter.project.ProjectEntity; import de.szut.lf8_starter.project.ProjectMapper; @@ -11,7 +11,10 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import java.util.Optional; diff --git a/src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/UpdateProjectAction.java similarity index 95% rename from src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java rename to src/main/java/de/szut/lf8_starter/project/action/crud/UpdateProjectAction.java index 9c174e3..29a4ad5 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/UpdateProjectAction.java @@ -1,4 +1,4 @@ -package de.szut.lf8_starter.project.action; +package de.szut.lf8_starter.project.action.crud; import de.szut.lf8_starter.project.ProjectEntity; import de.szut.lf8_starter.project.ProjectMapper; diff --git a/src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java new file mode 100644 index 0000000..c7344b4 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/action/employee/AddEmployeeToProjectAction.java @@ -0,0 +1,49 @@ +package de.szut.lf8_starter.project.action.employee; + +import de.szut.lf8_starter.project.ProjectEntity; +import de.szut.lf8_starter.project.ProjectService; +import de.szut.lf8_starter.security.AccessTokenService; +import org.springframework.http.*; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +import java.util.Optional; + +@RestController +public class AddEmployeeToProjectAction { + private final ProjectService projectService; + private final AccessTokenService accessTokenService; + private final RestTemplate restTemplate; + + public AddEmployeeToProjectAction(ProjectService projectService, AccessTokenService accessTokenService, RestTemplate restTemplate) { + this.projectService = projectService; + this.accessTokenService = accessTokenService; + this.restTemplate = restTemplate; + } + + + @PostMapping("/projects/{projectId}/employees/{employeeId}") + public ResponseEntity create(@PathVariable Long projectId, @PathVariable Long employeeId) { + Optional project = this.projectService.findById(projectId); + + if (project.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + String accessToken = this.accessTokenService.getAccessToken(); + + HttpHeaders headers = new HttpHeaders(); + headers.setBearerAuth(accessToken); + + HttpEntity requestEntity = new HttpEntity<>(headers); + + String url = "https://employee.szut.dev/employees/" + employeeId; + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); + + System.out.println("Employee Data: " + response.getBody()); + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } +} diff --git a/src/main/java/de/szut/lf8_starter/project/dto/BearerTokenResponseDto.java b/src/main/java/de/szut/lf8_starter/project/dto/BearerTokenResponseDto.java new file mode 100644 index 0000000..7bd40eb --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/dto/BearerTokenResponseDto.java @@ -0,0 +1,15 @@ +package de.szut.lf8_starter.project.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import lombok.Getter; +import lombok.Setter; + +@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) +@JsonIgnoreProperties(ignoreUnknown = true) +@Getter +@Setter +public class BearerTokenResponseDto { + private String accessToken; +} diff --git a/src/main/java/de/szut/lf8_starter/security/AccessTokenService.java b/src/main/java/de/szut/lf8_starter/security/AccessTokenService.java new file mode 100644 index 0000000..29cb0be --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/security/AccessTokenService.java @@ -0,0 +1,36 @@ +package de.szut.lf8_starter.security; + +import de.szut.lf8_starter.project.dto.BearerTokenResponseDto; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.util.Objects; + +@Service +public class AccessTokenService { + private final RestTemplate restTemplate; + + public AccessTokenService(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + public String getAccessToken() { + String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token"; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + + String body = "grant_type=password&client_id=employee-management-service&username=user&password=test"; + + HttpEntity requestEntity = new HttpEntity<>(body, headers); + + ResponseEntity response = this.restTemplate.exchange( + url, HttpMethod.POST, requestEntity, new ParameterizedTypeReference<>() { + } + ); + + return Objects.requireNonNull(response.getBody()).getAccessToken(); + } +}