From c3517d0517fe89cf03cfb0433a91c9160b46d24f Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 23 Oct 2024 11:58:29 +0200 Subject: [PATCH] Implement --- requests/employee/createEmployee.http | 13 +++-- requests/employee/getEmployee.http | 2 +- requests/project/createProject.http | 2 +- requests/project/getEmployeesFromProject.http | 2 + .../lf8_starter/employee/EmployeeService.java | 17 +++++++ .../project/action/RemoveProjectAction.java | 7 ++- .../action/crud/CreateProjectAction.java | 6 +-- .../action/crud/GetAllProjectsAction.java | 4 +- .../project/action/crud/GetProjectAction.java | 4 +- .../action/crud/UpdateProjectAction.java | 6 +-- .../GetEmployeeFromProjectAction.java | 51 +++++++++++++++++++ .../dto/{ => employee}/EmployeeDto.java | 2 +- .../project/dto/employee/GetEmployeeDto.java | 11 ++++ .../employee/GetEmployeesFromProjectDto.java | 17 +++++++ .../project/dto/employee/GetSkillDto.java | 13 +++++ .../dto/{ => project}/CreateProjectDto.java | 2 +- .../dto/{ => project}/GetProjectDto.java | 2 +- .../dto/{ => project}/UpdateProjectDto.java | 2 +- .../mapper/EmployeeFromProjectMapper.java | 35 +++++++++++++ .../project/{ => mapper}/ProjectMapper.java | 9 ++-- 20 files changed, 180 insertions(+), 27 deletions(-) create mode 100644 requests/project/getEmployeesFromProject.http create mode 100644 src/main/java/de/szut/lf8_starter/project/action/employee/GetEmployeeFromProjectAction.java rename src/main/java/de/szut/lf8_starter/project/dto/{ => employee}/EmployeeDto.java (91%) create mode 100644 src/main/java/de/szut/lf8_starter/project/dto/employee/GetEmployeeDto.java create mode 100644 src/main/java/de/szut/lf8_starter/project/dto/employee/GetEmployeesFromProjectDto.java create mode 100644 src/main/java/de/szut/lf8_starter/project/dto/employee/GetSkillDto.java rename src/main/java/de/szut/lf8_starter/project/dto/{ => project}/CreateProjectDto.java (95%) rename src/main/java/de/szut/lf8_starter/project/dto/{ => project}/GetProjectDto.java (94%) rename src/main/java/de/szut/lf8_starter/project/dto/{ => project}/UpdateProjectDto.java (90%) create mode 100644 src/main/java/de/szut/lf8_starter/project/mapper/EmployeeFromProjectMapper.java rename src/main/java/de/szut/lf8_starter/project/{ => mapper}/ProjectMapper.java (90%) diff --git a/requests/employee/createEmployee.http b/requests/employee/createEmployee.http index a170fe2..b3f4591 100644 --- a/requests/employee/createEmployee.http +++ b/requests/employee/createEmployee.http @@ -3,10 +3,13 @@ Authorization: Bearer {{auth_token}} Content-Type: application/json { - "firstName": "Jan", - "lastName": "Klattenhoff", - "street": "Pirolweg 17", + "firstName": "Jan1", + "lastName": "Klattenhoff1", + "street": "Pirolweg 171", "postcode": "27777", - "city": "Gandakersee", - "phone": "017684984816" + "city": "Gandakersee1", + "phone": "0176849848161", + "skillSet": [ + "1" + ] } \ No newline at end of file diff --git a/requests/employee/getEmployee.http b/requests/employee/getEmployee.http index eddf4d9..350a7fc 100644 --- a/requests/employee/getEmployee.http +++ b/requests/employee/getEmployee.http @@ -1,2 +1,2 @@ -GET https://employee.szut.dev/employees/312 +GET https://employee.szut.dev/employees/315 Authorization: Bearer {{auth_token}} \ No newline at end of file diff --git a/requests/project/createProject.http b/requests/project/createProject.http index 7b08e28..3338a2c 100644 --- a/requests/project/createProject.http +++ b/requests/project/createProject.http @@ -6,7 +6,7 @@ Content-Type: application/json { "name": "name", "leading_employee": 1, - "employees": [2, 3], + "employees": [315, 312], "contractor": 4, "contractor_name": "Peter File", "comment": "goal of project", diff --git a/requests/project/getEmployeesFromProject.http b/requests/project/getEmployeesFromProject.http new file mode 100644 index 0000000..d12dd96 --- /dev/null +++ b/requests/project/getEmployeesFromProject.http @@ -0,0 +1,2 @@ +GET http://localhost:8080/projects/2/employees +Authorization: Bearer {{auth_token}} \ No newline at end of file diff --git a/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java b/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java index a78f76b..907de80 100644 --- a/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java +++ b/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java @@ -1,5 +1,7 @@ package de.szut.lf8_starter.employee; +import de.szut.lf8_starter.exceptionHandling.ResourceNotFoundException; +import de.szut.lf8_starter.project.dto.employee.GetEmployeeDto; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -31,4 +33,19 @@ public class EmployeeService { return true; } + + public GetEmployeeDto getEmployee(String accessToken, Long employeeId) { + HttpHeaders headers = new HttpHeaders(); + headers.setBearerAuth(accessToken.replace("Bearer ", "")); + + HttpEntity 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"); + } + } } diff --git a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java index d14dbee..efbd48d 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java @@ -2,14 +2,17 @@ package de.szut.lf8_starter.project.action; import de.szut.lf8_starter.project.ProjectEntity; import de.szut.lf8_starter.project.ProjectService; -import de.szut.lf8_starter.project.dto.GetProjectDto; +import de.szut.lf8_starter.project.dto.project.GetProjectDto; 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.*; +import org.springframework.web.bind.annotation.DeleteMapping; +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/crud/CreateProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java index f21646d..1725ba8 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java @@ -1,10 +1,10 @@ package de.szut.lf8_starter.project.action.crud; 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.dto.CreateProjectDto; -import de.szut.lf8_starter.project.dto.GetProjectDto; +import de.szut.lf8_starter.project.dto.project.CreateProjectDto; +import de.szut.lf8_starter.project.dto.project.GetProjectDto; +import de.szut.lf8_starter.project.mapper.ProjectMapper; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; diff --git a/src/main/java/de/szut/lf8_starter/project/action/crud/GetAllProjectsAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/GetAllProjectsAction.java index 5fb346a..97f5b9a 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/crud/GetAllProjectsAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/GetAllProjectsAction.java @@ -1,8 +1,8 @@ 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.dto.GetProjectDto; +import de.szut.lf8_starter.project.dto.project.GetProjectDto; +import de.szut.lf8_starter.project.mapper.ProjectMapper; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; diff --git a/src/main/java/de/szut/lf8_starter/project/action/crud/GetProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/GetProjectAction.java index eba31c1..041a26d 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/crud/GetProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/GetProjectAction.java @@ -1,9 +1,9 @@ package de.szut.lf8_starter.project.action.crud; 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.dto.GetProjectDto; +import de.szut.lf8_starter.project.dto.project.GetProjectDto; +import de.szut.lf8_starter.project.mapper.ProjectMapper; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; diff --git a/src/main/java/de/szut/lf8_starter/project/action/crud/UpdateProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/UpdateProjectAction.java index 29a4ad5..3a169cf 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/crud/UpdateProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/crud/UpdateProjectAction.java @@ -1,10 +1,10 @@ package de.szut.lf8_starter.project.action.crud; 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.dto.GetProjectDto; -import de.szut.lf8_starter.project.dto.UpdateProjectDto; +import de.szut.lf8_starter.project.dto.project.GetProjectDto; +import de.szut.lf8_starter.project.dto.project.UpdateProjectDto; +import de.szut.lf8_starter.project.mapper.ProjectMapper; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; diff --git a/src/main/java/de/szut/lf8_starter/project/action/employee/GetEmployeeFromProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/employee/GetEmployeeFromProjectAction.java new file mode 100644 index 0000000..f9d339c --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/action/employee/GetEmployeeFromProjectAction.java @@ -0,0 +1,51 @@ +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 create( + @PathVariable Long projectId, + @RequestHeader("Authorization") String accessToken + ) { + Optional 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); + } +} diff --git a/src/main/java/de/szut/lf8_starter/project/dto/EmployeeDto.java b/src/main/java/de/szut/lf8_starter/project/dto/employee/EmployeeDto.java similarity index 91% rename from src/main/java/de/szut/lf8_starter/project/dto/EmployeeDto.java rename to src/main/java/de/szut/lf8_starter/project/dto/employee/EmployeeDto.java index 05909e1..cc4e872 100644 --- a/src/main/java/de/szut/lf8_starter/project/dto/EmployeeDto.java +++ b/src/main/java/de/szut/lf8_starter/project/dto/employee/EmployeeDto.java @@ -1,4 +1,4 @@ -package de.szut.lf8_starter.project.dto; +package de.szut.lf8_starter.project.dto.employee; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.annotation.JsonNaming; diff --git a/src/main/java/de/szut/lf8_starter/project/dto/employee/GetEmployeeDto.java b/src/main/java/de/szut/lf8_starter/project/dto/employee/GetEmployeeDto.java new file mode 100644 index 0000000..fdb13a1 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/dto/employee/GetEmployeeDto.java @@ -0,0 +1,11 @@ +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; +} diff --git a/src/main/java/de/szut/lf8_starter/project/dto/employee/GetEmployeesFromProjectDto.java b/src/main/java/de/szut/lf8_starter/project/dto/employee/GetEmployeesFromProjectDto.java new file mode 100644 index 0000000..df24a76 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/dto/employee/GetEmployeesFromProjectDto.java @@ -0,0 +1,17 @@ +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 employees; +} diff --git a/src/main/java/de/szut/lf8_starter/project/dto/employee/GetSkillDto.java b/src/main/java/de/szut/lf8_starter/project/dto/employee/GetSkillDto.java new file mode 100644 index 0000000..ba91c03 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/dto/employee/GetSkillDto.java @@ -0,0 +1,13 @@ +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; +} diff --git a/src/main/java/de/szut/lf8_starter/project/dto/CreateProjectDto.java b/src/main/java/de/szut/lf8_starter/project/dto/project/CreateProjectDto.java similarity index 95% rename from src/main/java/de/szut/lf8_starter/project/dto/CreateProjectDto.java rename to src/main/java/de/szut/lf8_starter/project/dto/project/CreateProjectDto.java index ebf4393..8e2a2aa 100644 --- a/src/main/java/de/szut/lf8_starter/project/dto/CreateProjectDto.java +++ b/src/main/java/de/szut/lf8_starter/project/dto/project/CreateProjectDto.java @@ -1,4 +1,4 @@ -package de.szut.lf8_starter.project.dto; +package de.szut.lf8_starter.project.dto.project; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.PropertyNamingStrategies; diff --git a/src/main/java/de/szut/lf8_starter/project/dto/GetProjectDto.java b/src/main/java/de/szut/lf8_starter/project/dto/project/GetProjectDto.java similarity index 94% rename from src/main/java/de/szut/lf8_starter/project/dto/GetProjectDto.java rename to src/main/java/de/szut/lf8_starter/project/dto/project/GetProjectDto.java index 90ebe5e..89f043b 100644 --- a/src/main/java/de/szut/lf8_starter/project/dto/GetProjectDto.java +++ b/src/main/java/de/szut/lf8_starter/project/dto/project/GetProjectDto.java @@ -1,4 +1,4 @@ -package de.szut.lf8_starter.project.dto; +package de.szut.lf8_starter.project.dto.project; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.PropertyNamingStrategies; diff --git a/src/main/java/de/szut/lf8_starter/project/dto/UpdateProjectDto.java b/src/main/java/de/szut/lf8_starter/project/dto/project/UpdateProjectDto.java similarity index 90% rename from src/main/java/de/szut/lf8_starter/project/dto/UpdateProjectDto.java rename to src/main/java/de/szut/lf8_starter/project/dto/project/UpdateProjectDto.java index 6d94232..943a62b 100644 --- a/src/main/java/de/szut/lf8_starter/project/dto/UpdateProjectDto.java +++ b/src/main/java/de/szut/lf8_starter/project/dto/project/UpdateProjectDto.java @@ -1,4 +1,4 @@ -package de.szut.lf8_starter.project.dto; +package de.szut.lf8_starter.project.dto.project; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.PropertyNamingStrategies; diff --git a/src/main/java/de/szut/lf8_starter/project/mapper/EmployeeFromProjectMapper.java b/src/main/java/de/szut/lf8_starter/project/mapper/EmployeeFromProjectMapper.java new file mode 100644 index 0000000..039f23c --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/mapper/EmployeeFromProjectMapper.java @@ -0,0 +1,35 @@ +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 employeeDtos = new ArrayList<>(); + for (long id : projectEntity.getEmployees()) { + GetEmployeeDto getEmployeeDto = this.employeeService.getEmployee(accessToken, id); + + employeeDtos.add(getEmployeeDto); + } + + getEmployeesFromProjectDto.setEmployees(employeeDtos); + + return getEmployeesFromProjectDto; + } +} diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectMapper.java b/src/main/java/de/szut/lf8_starter/project/mapper/ProjectMapper.java similarity index 90% rename from src/main/java/de/szut/lf8_starter/project/ProjectMapper.java rename to src/main/java/de/szut/lf8_starter/project/mapper/ProjectMapper.java index cb43b84..0d3b0af 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectMapper.java +++ b/src/main/java/de/szut/lf8_starter/project/mapper/ProjectMapper.java @@ -1,8 +1,9 @@ -package de.szut.lf8_starter.project; +package de.szut.lf8_starter.project.mapper; -import de.szut.lf8_starter.project.dto.CreateProjectDto; -import de.szut.lf8_starter.project.dto.GetProjectDto; -import de.szut.lf8_starter.project.dto.UpdateProjectDto; +import de.szut.lf8_starter.project.ProjectEntity; +import de.szut.lf8_starter.project.dto.project.CreateProjectDto; +import de.szut.lf8_starter.project.dto.project.GetProjectDto; +import de.szut.lf8_starter.project.dto.project.UpdateProjectDto; import org.springframework.stereotype.Service; @Service