feat: Implement add employee to project route (SCRUM-2) #32

Merged
jank merged 8 commits from feature/add-employee-to-project into main 2024-10-23 08:39:26 +00:00
15 changed files with 92 additions and 5 deletions
Showing only changes of commit 790c2f4bc4 - Show all commits

@ -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/312
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.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.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.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;

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

@ -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 org.springframework.http.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
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 RestTemplate restTemplate;
public AddEmployeeToProjectAction(ProjectService projectService, RestTemplate restTemplate) {
this.projectService = projectService;
this.restTemplate = restTemplate;
}
@PostMapping("/projects/{projectId}/employees/{employeeId}")
public ResponseEntity<Object> create(
@PathVariable Long projectId,
@PathVariable Long employeeId,
@RequestHeader("Authorization") String accessToken
) {
Optional<ProjectEntity> project = this.projectService.findById(projectId);
if (project.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(accessToken.replace("Bearer ", ""));
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;
}