Compare commits
1 Commits
78ac3d8610
...
7092483809
Author | SHA1 | Date | |
---|---|---|---|
7092483809 |
@ -3,13 +3,10 @@ Authorization: Bearer {{auth_token}}
|
|||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
{
|
{
|
||||||
"firstName": "Jan1",
|
"firstName": "Jan",
|
||||||
"lastName": "Klattenhoff1",
|
"lastName": "Klattenhoff",
|
||||||
"street": "Pirolweg 171",
|
"street": "Pirolweg 17",
|
||||||
"postcode": "27777",
|
"postcode": "27777",
|
||||||
"city": "Gandakersee1",
|
"city": "Gandakersee",
|
||||||
"phone": "0176849848161",
|
"phone": "017684984816"
|
||||||
"skillSet": [
|
|
||||||
"1"
|
|
||||||
]
|
|
||||||
}
|
}
|
@ -1,2 +1,2 @@
|
|||||||
GET https://employee.szut.dev/employees/315
|
GET https://employee.szut.dev/employees/312
|
||||||
Authorization: Bearer {{auth_token}}
|
Authorization: Bearer {{auth_token}}
|
@ -1,3 +0,0 @@
|
|||||||
GET http://localhost:8080/employees/315/projects
|
|
||||||
Authorization: Bearer {{auth_token}}
|
|
||||||
Content-Type: application/json
|
|
@ -6,7 +6,7 @@ Content-Type: application/json
|
|||||||
{
|
{
|
||||||
"name": "name",
|
"name": "name",
|
||||||
"leading_employee": 1,
|
"leading_employee": 1,
|
||||||
"employees": [315, 312],
|
"employees": [2, 3],
|
||||||
"contractor": 4,
|
"contractor": 4,
|
||||||
"contractor_name": "Peter File",
|
"contractor_name": "Peter File",
|
||||||
"comment": "goal of project",
|
"comment": "goal of project",
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
GET http://localhost:8080/projects/2/employees
|
|
||||||
Authorization: Bearer {{auth_token}}
|
|
@ -1,11 +1,5 @@
|
|||||||
package de.szut.lf8_starter.employee;
|
package de.szut.lf8_starter.employee;
|
||||||
|
|
||||||
import de.szut.lf8_starter.exceptionHandling.ResourceNotFoundException;
|
|
||||||
import de.szut.lf8_starter.project.ProjectEntity;
|
|
||||||
import de.szut.lf8_starter.project.ProjectRepository;
|
|
||||||
import de.szut.lf8_starter.project.dto.employee.GetEmployeeDto;
|
|
||||||
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
|
|
||||||
import de.szut.lf8_starter.project.mapper.ProjectMapper;
|
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpEntity;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
@ -13,19 +7,12 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.web.client.HttpClientErrorException;
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class EmployeeService {
|
public class EmployeeService {
|
||||||
private final RestTemplate restTemplate;
|
private final RestTemplate restTemplate;
|
||||||
private final ProjectRepository projectRepository;
|
|
||||||
private final ProjectMapper projectMapper;
|
|
||||||
|
|
||||||
public EmployeeService(RestTemplate restTemplate, ProjectRepository projectRepository, ProjectMapper projectMapper) {
|
public EmployeeService(RestTemplate restTemplate) {
|
||||||
this.restTemplate = restTemplate;
|
this.restTemplate = restTemplate;
|
||||||
this.projectRepository = projectRepository;
|
|
||||||
this.projectMapper = projectMapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean employeeExists(String accessToken, Long employeeId) {
|
public boolean employeeExists(String accessToken, Long employeeId) {
|
||||||
@ -44,30 +31,4 @@ public class EmployeeService {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GetEmployeeDto getEmployee(String accessToken, Long employeeId) {
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.setBearerAuth(accessToken.replace("Bearer ", ""));
|
|
||||||
|
|
||||||
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
|
|
||||||
|
|
||||||
String url = "https://employee.szut.dev/employees/" + employeeId;
|
|
||||||
|
|
||||||
try {
|
|
||||||
return restTemplate.exchange(url, HttpMethod.GET, requestEntity, GetEmployeeDto.class).getBody();
|
|
||||||
} catch (HttpClientErrorException.NotFound e) {
|
|
||||||
throw new ResourceNotFoundException("Employee not found");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<GetProjectDto> getProjects(Long employeeId) {
|
|
||||||
ArrayList<ProjectEntity> projects = this.projectRepository.findAllByEmployeeId(employeeId);
|
|
||||||
ArrayList<GetProjectDto> projectDtos = new ArrayList<>();
|
|
||||||
|
|
||||||
for (ProjectEntity project : projects) {
|
|
||||||
projectDtos.add(this.projectMapper.mapToGetDto(project));
|
|
||||||
}
|
|
||||||
|
|
||||||
return projectDtos;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
package de.szut.lf8_starter.project.mapper;
|
package de.szut.lf8_starter.project;
|
||||||
|
|
||||||
import de.szut.lf8_starter.project.ProjectEntity;
|
import de.szut.lf8_starter.project.dto.CreateProjectDto;
|
||||||
import de.szut.lf8_starter.project.dto.project.CreateProjectDto;
|
import de.szut.lf8_starter.project.dto.GetProjectDto;
|
||||||
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
|
import de.szut.lf8_starter.project.dto.UpdateProjectDto;
|
||||||
import de.szut.lf8_starter.project.dto.project.UpdateProjectDto;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
@ -1,14 +1,8 @@
|
|||||||
package de.szut.lf8_starter.project;
|
package de.szut.lf8_starter.project;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
|
||||||
import org.springframework.data.repository.query.Param;
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface ProjectRepository extends JpaRepository<ProjectEntity, Long> {
|
public interface ProjectRepository extends JpaRepository<ProjectEntity, Long> {
|
||||||
@Query("SELECT p FROM ProjectEntity p WHERE :employeeId MEMBER OF p.employees")
|
|
||||||
ArrayList<ProjectEntity> findAllByEmployeeId(@Param("employeeId") Long employeeId);
|
|
||||||
}
|
}
|
||||||
|
@ -2,17 +2,14 @@ package de.szut.lf8_starter.project.action;
|
|||||||
|
|
||||||
import de.szut.lf8_starter.project.ProjectEntity;
|
import de.szut.lf8_starter.project.ProjectEntity;
|
||||||
import de.szut.lf8_starter.project.ProjectService;
|
import de.szut.lf8_starter.project.ProjectService;
|
||||||
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
|
import de.szut.lf8_starter.project.dto.GetProjectDto;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
import io.swagger.v3.oas.annotations.media.Content;
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
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.DeleteMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
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,10 +1,10 @@
|
|||||||
package de.szut.lf8_starter.project.action.crud;
|
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.ProjectService;
|
import de.szut.lf8_starter.project.ProjectService;
|
||||||
import de.szut.lf8_starter.project.dto.project.CreateProjectDto;
|
import de.szut.lf8_starter.project.dto.CreateProjectDto;
|
||||||
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
|
import de.szut.lf8_starter.project.dto.GetProjectDto;
|
||||||
import de.szut.lf8_starter.project.mapper.ProjectMapper;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
import io.swagger.v3.oas.annotations.media.Content;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package de.szut.lf8_starter.project.action.crud;
|
package de.szut.lf8_starter.project.action.crud;
|
||||||
|
|
||||||
|
import de.szut.lf8_starter.project.ProjectMapper;
|
||||||
import de.szut.lf8_starter.project.ProjectService;
|
import de.szut.lf8_starter.project.ProjectService;
|
||||||
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
|
import de.szut.lf8_starter.project.dto.GetProjectDto;
|
||||||
import de.szut.lf8_starter.project.mapper.ProjectMapper;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
import io.swagger.v3.oas.annotations.media.Content;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package de.szut.lf8_starter.project.action.crud;
|
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.ProjectService;
|
import de.szut.lf8_starter.project.ProjectService;
|
||||||
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
|
import de.szut.lf8_starter.project.dto.GetProjectDto;
|
||||||
import de.szut.lf8_starter.project.mapper.ProjectMapper;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
import io.swagger.v3.oas.annotations.media.Content;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package de.szut.lf8_starter.project.action.crud;
|
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.ProjectService;
|
import de.szut.lf8_starter.project.ProjectService;
|
||||||
import de.szut.lf8_starter.project.dto.project.GetProjectDto;
|
import de.szut.lf8_starter.project.dto.GetProjectDto;
|
||||||
import de.szut.lf8_starter.project.dto.project.UpdateProjectDto;
|
import de.szut.lf8_starter.project.dto.UpdateProjectDto;
|
||||||
import de.szut.lf8_starter.project.mapper.ProjectMapper;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
import io.swagger.v3.oas.annotations.media.Content;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
@ -1,51 +0,0 @@
|
|||||||
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.project.dto.employee.GetEmployeesFromProjectDto;
|
|
||||||
import de.szut.lf8_starter.project.mapper.EmployeeFromProjectMapper;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
|
||||||
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.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestHeader;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
public class GetEmployeeFromProjectAction {
|
|
||||||
private final ProjectService projectService;
|
|
||||||
private final EmployeeFromProjectMapper employeeFromProjectMapper;
|
|
||||||
|
|
||||||
public GetEmployeeFromProjectAction(ProjectService projectService, EmployeeFromProjectMapper employeeFromProjectMapper) {
|
|
||||||
this.projectService = projectService;
|
|
||||||
this.employeeFromProjectMapper = employeeFromProjectMapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Operation(summary = "Get all employees from project")
|
|
||||||
@ApiResponses(value = {
|
|
||||||
@ApiResponse(responseCode = "200", description = "Outputs projects with employees"),
|
|
||||||
@ApiResponse(responseCode = "404", description = "Project not found", content = @Content)
|
|
||||||
})
|
|
||||||
@GetMapping("/projects/{projectId}/employees")
|
|
||||||
public ResponseEntity<Object> create(
|
|
||||||
@PathVariable Long projectId,
|
|
||||||
@RequestHeader("Authorization") String accessToken
|
|
||||||
) {
|
|
||||||
Optional<ProjectEntity> project = projectService.findById(projectId);
|
|
||||||
|
|
||||||
if (project.isEmpty()) {
|
|
||||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
GetEmployeesFromProjectDto getEmployeesFromProjectDto = this.employeeFromProjectMapper
|
|
||||||
.mapToGetEmployeesFromProjectDto(project.get(), accessToken);
|
|
||||||
|
|
||||||
return new ResponseEntity<>(getEmployeesFromProjectDto, HttpStatus.OK);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
package de.szut.lf8_starter.project.action.employee;
|
|
||||||
|
|
||||||
import de.szut.lf8_starter.employee.EmployeeService;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
|
||||||
import io.swagger.v3.oas.annotations.media.Content;
|
|
||||||
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.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestHeader;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
public class GetProjectsFromEmployee {
|
|
||||||
private final EmployeeService employeeService;
|
|
||||||
|
|
||||||
public GetProjectsFromEmployee(EmployeeService employeeService) {
|
|
||||||
this.employeeService = employeeService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Operation(summary = "Get all projects from employee")
|
|
||||||
@ApiResponses(value = {
|
|
||||||
@ApiResponse(responseCode = "200", description = "Get all projects from employee", content = @Content),
|
|
||||||
@ApiResponse(responseCode = "404", description = "Employee not found", content = @Content)
|
|
||||||
})
|
|
||||||
@GetMapping("/employees/{employeeId}/projects")
|
|
||||||
public ResponseEntity<Object> create(
|
|
||||||
@PathVariable Long employeeId,
|
|
||||||
@RequestHeader("Authorization") String accessToken
|
|
||||||
) {
|
|
||||||
if (!this.employeeService.employeeExists(accessToken, employeeId)) {
|
|
||||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ResponseEntity<>(this.employeeService.getProjects(employeeId), HttpStatus.OK);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package de.szut.lf8_starter.project.dto.project;
|
package de.szut.lf8_starter.project.dto;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
@ -1,4 +1,4 @@
|
|||||||
package de.szut.lf8_starter.project.dto.employee;
|
package de.szut.lf8_starter.project.dto;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
@ -1,4 +1,4 @@
|
|||||||
package de.szut.lf8_starter.project.dto.project;
|
package de.szut.lf8_starter.project.dto;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
@ -1,4 +1,4 @@
|
|||||||
package de.szut.lf8_starter.project.dto.project;
|
package de.szut.lf8_starter.project.dto;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
@ -1,11 +0,0 @@
|
|||||||
package de.szut.lf8_starter.project.dto.employee;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
public class GetEmployeeDto {
|
|
||||||
private long id;
|
|
||||||
private GetSkillDto[] skillSet;
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
package de.szut.lf8_starter.project.dto.employee;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
|
||||||
public class GetEmployeesFromProjectDto {
|
|
||||||
private long projectId;
|
|
||||||
private String projectDescription;
|
|
||||||
private ArrayList<GetEmployeeDto> employees;
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package de.szut.lf8_starter.project.dto.employee;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
|
||||||
public class GetSkillDto {
|
|
||||||
private String skill;
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
package de.szut.lf8_starter.project.mapper;
|
|
||||||
|
|
||||||
import de.szut.lf8_starter.employee.EmployeeService;
|
|
||||||
import de.szut.lf8_starter.project.ProjectEntity;
|
|
||||||
import de.szut.lf8_starter.project.dto.employee.GetEmployeeDto;
|
|
||||||
import de.szut.lf8_starter.project.dto.employee.GetEmployeesFromProjectDto;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class EmployeeFromProjectMapper {
|
|
||||||
private final EmployeeService employeeService;
|
|
||||||
|
|
||||||
public EmployeeFromProjectMapper(EmployeeService employeeService) {
|
|
||||||
this.employeeService = employeeService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GetEmployeesFromProjectDto mapToGetEmployeesFromProjectDto(ProjectEntity projectEntity, String accessToken) {
|
|
||||||
GetEmployeesFromProjectDto getEmployeesFromProjectDto = new GetEmployeesFromProjectDto();
|
|
||||||
getEmployeesFromProjectDto.setProjectId(projectEntity.getId());
|
|
||||||
getEmployeesFromProjectDto.setProjectDescription(projectEntity.getComment());
|
|
||||||
|
|
||||||
ArrayList<GetEmployeeDto> employeeDtos = new ArrayList<>();
|
|
||||||
for (long id : projectEntity.getEmployees()) {
|
|
||||||
GetEmployeeDto getEmployeeDto = this.employeeService.getEmployee(accessToken, id);
|
|
||||||
|
|
||||||
employeeDtos.add(getEmployeeDto);
|
|
||||||
}
|
|
||||||
|
|
||||||
getEmployeesFromProjectDto.setEmployees(employeeDtos);
|
|
||||||
|
|
||||||
return getEmployeesFromProjectDto;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,82 +0,0 @@
|
|||||||
package de.szut.lf8_starter.integration.project;
|
|
||||||
|
|
||||||
import de.szut.lf8_starter.project.ProjectEntity;
|
|
||||||
import de.szut.lf8_starter.project.ProjectRepository;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.http.*;
|
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
@AutoConfigureMockMvc(addFilters = false)
|
|
||||||
class GetEmployeesFromProjectTest {
|
|
||||||
@Autowired
|
|
||||||
private MockMvc mockMvc;
|
|
||||||
@Autowired
|
|
||||||
private ProjectRepository projectRepository;
|
|
||||||
@Autowired
|
|
||||||
private RestTemplate restTemplate;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getEmployeesFromProjectTest() throws Exception {
|
|
||||||
ProjectEntity project = new ProjectEntity();
|
|
||||||
project.setId(1);
|
|
||||||
project.setComment("comment");
|
|
||||||
project.setContractor(1);
|
|
||||||
project.setContractorName("contractorName");
|
|
||||||
project.setEndDate(LocalDate.of(2024, 1, 1));
|
|
||||||
project.setLeadingEmployee(1);
|
|
||||||
project.setName("name");
|
|
||||||
project.setStartDate(LocalDate.of(2021, 1, 1));
|
|
||||||
project.setEmployees(List.of(315L));
|
|
||||||
this.projectRepository.save(project);
|
|
||||||
|
|
||||||
this.mockMvc.perform(get("/projects/1/employees")
|
|
||||||
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
|
|
||||||
).andExpect(status().isOk())
|
|
||||||
.andExpect(jsonPath("project_id").value(1))
|
|
||||||
.andExpect(jsonPath("project_description").value("comment"))
|
|
||||||
.andExpect(jsonPath("employees[0].id").value(315))
|
|
||||||
.andExpect(jsonPath("employees[0].skillSet[0].skill").value("Product Owner"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getEmployeesFromProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception {
|
|
||||||
this.mockMvc.perform(get("/projects/1111/employees")
|
|
||||||
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
|
|
||||||
).andExpect(status().isNotFound());
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getBearerToken() {
|
|
||||||
String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token";
|
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
||||||
|
|
||||||
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
|
||||||
map.add("grant_type", "password");
|
|
||||||
map.add("client_id", "employee-management-service");
|
|
||||||
map.add("username", "user");
|
|
||||||
map.add("password", "test");
|
|
||||||
|
|
||||||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
|
|
||||||
|
|
||||||
ResponseEntity<Map> response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class);
|
|
||||||
|
|
||||||
return Objects.requireNonNull(response.getBody()).get("access_token").toString();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,86 +0,0 @@
|
|||||||
package de.szut.lf8_starter.integration.project;
|
|
||||||
|
|
||||||
import de.szut.lf8_starter.project.ProjectEntity;
|
|
||||||
import de.szut.lf8_starter.project.ProjectRepository;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.http.*;
|
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
@AutoConfigureMockMvc(addFilters = false)
|
|
||||||
class GetProjectsFromEmployeeTest {
|
|
||||||
@Autowired
|
|
||||||
private MockMvc mockMvc;
|
|
||||||
@Autowired
|
|
||||||
private ProjectRepository projectRepository;
|
|
||||||
@Autowired
|
|
||||||
private RestTemplate restTemplate;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getProjectsFromEmployeeTest() throws Exception {
|
|
||||||
ProjectEntity project = new ProjectEntity();
|
|
||||||
project.setId(1);
|
|
||||||
project.setComment("comment");
|
|
||||||
project.setContractor(1);
|
|
||||||
project.setContractorName("contractorName");
|
|
||||||
project.setEndDate(LocalDate.of(2024, 1, 1));
|
|
||||||
project.setLeadingEmployee(1);
|
|
||||||
project.setName("name");
|
|
||||||
project.setStartDate(LocalDate.of(2021, 1, 1));
|
|
||||||
project.setEmployees(List.of(315L));
|
|
||||||
this.projectRepository.save(project);
|
|
||||||
|
|
||||||
this.mockMvc.perform(get("/employees/315/projects")
|
|
||||||
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
|
|
||||||
).andExpect(status().isOk())
|
|
||||||
.andExpect(jsonPath("$[0].id").value(1))
|
|
||||||
.andExpect(jsonPath("$[0].name").value("name"))
|
|
||||||
.andExpect(jsonPath("$[0].leading_employee").value(1))
|
|
||||||
.andExpect(jsonPath("$[0].employees[0]").value(315))
|
|
||||||
.andExpect(jsonPath("$[0].contractor").value(1))
|
|
||||||
.andExpect(jsonPath("$[0].contractor_name").value("contractorName"))
|
|
||||||
.andExpect(jsonPath("$[0].comment").value("comment"))
|
|
||||||
.andExpect(jsonPath("$[0].start_date").value("01.01.2021"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void GetProjectsFromEmployeeShouldReturnNotFoundResponseWhenEmployeeIsNotFound() throws Exception {
|
|
||||||
this.mockMvc.perform(get("/employees/1111/projects")
|
|
||||||
.header(HttpHeaders.AUTHORIZATION, getBearerToken())
|
|
||||||
).andExpect(status().isNotFound());
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getBearerToken() {
|
|
||||||
String url = "https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token";
|
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
||||||
|
|
||||||
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
|
||||||
map.add("grant_type", "password");
|
|
||||||
map.add("client_id", "employee-management-service");
|
|
||||||
map.add("username", "user");
|
|
||||||
map.add("password", "test");
|
|
||||||
|
|
||||||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
|
|
||||||
|
|
||||||
ResponseEntity<Map> response = this.restTemplate.exchange(url, HttpMethod.POST, request, Map.class);
|
|
||||||
|
|
||||||
return Objects.requireNonNull(response.getBody()).get("access_token").toString();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user