From fe75c845dbf1a0f1de70ab29c631f286fc7f1883 Mon Sep 17 00:00:00 2001
From: Phan Huy Tran
Date: Wed, 23 Oct 2024 12:45:12 +0200
Subject: [PATCH] Implement
---
requests/employee/getProjects.http | 3 +
.../lf8_starter/employee/EmployeeService.java | 23 ++++-
.../project/ProjectRepository.java | 6 ++
.../employee/GetProjectsFromEmployee.java | 39 +++++++++
.../project/GetProjectsFromEmployeeTest.java | 86 +++++++++++++++++++
5 files changed, 156 insertions(+), 1 deletion(-)
create mode 100644 requests/employee/getProjects.http
create mode 100644 src/main/java/de/szut/lf8_starter/project/action/employee/GetProjectsFromEmployee.java
create mode 100644 src/test/java/de/szut/lf8_starter/integration/project/GetProjectsFromEmployeeTest.java
diff --git a/requests/employee/getProjects.http b/requests/employee/getProjects.http
new file mode 100644
index 0000000..8689798
--- /dev/null
+++ b/requests/employee/getProjects.http
@@ -0,0 +1,3 @@
+GET http://localhost:8080/employees/315/projects
+Authorization: Bearer {{auth_token}}
+Content-Type: application/json
\ 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 907de80..5008cd9 100644
--- a/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java
+++ b/src/main/java/de/szut/lf8_starter/employee/EmployeeService.java
@@ -1,7 +1,11 @@
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.HttpHeaders;
import org.springframework.http.HttpMethod;
@@ -9,12 +13,18 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
+import java.util.ArrayList;
+
@Service
public class EmployeeService {
private final RestTemplate restTemplate;
+ private final ProjectRepository projectRepository;
+ private final ProjectMapper projectMapper;
- public EmployeeService(RestTemplate restTemplate) {
+ public EmployeeService(RestTemplate restTemplate, ProjectRepository projectRepository, ProjectMapper projectMapper) {
this.restTemplate = restTemplate;
+ this.projectRepository = projectRepository;
+ this.projectMapper = projectMapper;
}
public boolean employeeExists(String accessToken, Long employeeId) {
@@ -48,4 +58,15 @@ public class EmployeeService {
throw new ResourceNotFoundException("Employee not found");
}
}
+
+ public ArrayList getProjects(Long employeeId) {
+ ArrayList projects = this.projectRepository.findAllByEmployeeId(employeeId);
+ ArrayList projectDtos = new ArrayList<>();
+
+ for (ProjectEntity project : projects) {
+ projectDtos.add(this.projectMapper.mapToGetDto(project));
+ }
+
+ return projectDtos;
+ }
}
diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectRepository.java b/src/main/java/de/szut/lf8_starter/project/ProjectRepository.java
index 6f7e33f..3b9dfb0 100644
--- a/src/main/java/de/szut/lf8_starter/project/ProjectRepository.java
+++ b/src/main/java/de/szut/lf8_starter/project/ProjectRepository.java
@@ -1,8 +1,14 @@
package de.szut.lf8_starter.project;
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 java.util.ArrayList;
+
@Repository
public interface ProjectRepository extends JpaRepository {
+ @Query("SELECT p FROM ProjectEntity p WHERE :employeeId MEMBER OF p.employees")
+ ArrayList findAllByEmployeeId(@Param("employeeId") Long employeeId);
}
diff --git a/src/main/java/de/szut/lf8_starter/project/action/employee/GetProjectsFromEmployee.java b/src/main/java/de/szut/lf8_starter/project/action/employee/GetProjectsFromEmployee.java
new file mode 100644
index 0000000..baa4dea
--- /dev/null
+++ b/src/main/java/de/szut/lf8_starter/project/action/employee/GetProjectsFromEmployee.java
@@ -0,0 +1,39 @@
+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