WIP
Some checks failed
Quality Check / Tests (pull_request) Successful in 57s
Quality Check / Checkstyle Main (pull_request) Successful in 46s
Build PR / Build and analyze (pull_request) Successful in 1m56s
gitea-sonarqube-bot ERROR

This commit is contained in:
Phan Huy Tran 2024-10-02 12:31:59 +02:00
parent 1e38efaa63
commit c5eeabbdff
16 changed files with 128 additions and 5 deletions

@ -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"
}

@ -0,0 +1,2 @@
DELETE https://employee.szut.dev/employees/310
Authorization: Bearer {{auth_token}}

@ -0,0 +1,2 @@
GET https://employee.szut.dev/employees
Authorization: Bearer {{auth_token}}

@ -0,0 +1,2 @@
GET https://employee.szut.dev/employees/312
Authorization: Bearer {{auth_token}}

@ -0,0 +1,2 @@
POST http://localhost:8080/projects/1/employees/1
Authorization: Bearer {{auth_token}}

@ -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.ProjectEntity;
import de.szut.lf8_starter.project.ProjectMapper; import de.szut.lf8_starter.project.ProjectMapper;

@ -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.ProjectMapper;
import de.szut.lf8_starter.project.ProjectService; import de.szut.lf8_starter.project.ProjectService;

@ -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.ProjectEntity;
import de.szut.lf8_starter.project.ProjectMapper; 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 io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; 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; import java.util.Optional;

@ -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.ProjectEntity;
import de.szut.lf8_starter.project.ProjectMapper; import de.szut.lf8_starter.project.ProjectMapper;

@ -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<Object> create(@PathVariable Long projectId, @PathVariable Long employeeId) {
Optional<ProjectEntity> 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<String> requestEntity = new HttpEntity<>(headers);
String url = "https://employee.szut.dev/employees/" + employeeId;
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
System.out.println("Employee Data: " + response.getBody());
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}

@ -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;
}

@ -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<String> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity<BearerTokenResponseDto> response = this.restTemplate.exchange(
url, HttpMethod.POST, requestEntity, new ParameterizedTypeReference<>() {
}
);
return Objects.requireNonNull(response.getBody()).getAccessToken();
}
}