From 25ed7e8759b586ac80334171f2510ad098449829 Mon Sep 17 00:00:00 2001
From: Phan Huy Tran
Date: Wed, 2 Oct 2024 08:47:36 +0200
Subject: [PATCH] implement get project by id
---
requests/GetBearerToken.http | 10 +++----
.../lf8_starter/project/ProjectService.java | 12 ++++++++
.../{ => action}/CreateProjectAction.java | 5 +++-
.../GetAllProjectsAction.java} | 22 +++++++++------
.../project/action/GetProjectAction.java | 28 +++++++++++++++++++
5 files changed, 62 insertions(+), 15 deletions(-)
rename src/main/java/de/szut/lf8_starter/project/{ => action}/CreateProjectAction.java (89%)
rename src/main/java/de/szut/lf8_starter/project/{ProjectController.java => action/GetAllProjectsAction.java} (60%)
create mode 100644 src/main/java/de/szut/lf8_starter/project/action/GetProjectAction.java
diff --git a/requests/GetBearerToken.http b/requests/GetBearerToken.http
index f5209ca..abf7e94 100644
--- a/requests/GetBearerToken.http
+++ b/requests/GetBearerToken.http
@@ -1,6 +1,6 @@
-POST https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token
-Content-Type: application/x-www-form-urlencoded
-
-grant_type=password&client_id=employee-management-service&username=user&password=test
-
+POST https://keycloak.szut.dev/auth/realms/szut/protocol/openid-connect/token
+Content-Type: application/x-www-form-urlencoded
+
+grant_type=password&client_id=employee-management-service&username=user&password=test
+
> {% client.global.set("auth_token", response.body.access_token); %}
\ No newline at end of file
diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectService.java b/src/main/java/de/szut/lf8_starter/project/ProjectService.java
index 4dc8688..a815350 100644
--- a/src/main/java/de/szut/lf8_starter/project/ProjectService.java
+++ b/src/main/java/de/szut/lf8_starter/project/ProjectService.java
@@ -1,8 +1,10 @@
package de.szut.lf8_starter.project;
+import de.szut.lf8_starter.exceptionHandling.ResourceNotFoundException;
import org.springframework.stereotype.Service;
import java.util.List;
+import java.util.Optional;
@Service
public class ProjectService {
@@ -19,4 +21,14 @@ public class ProjectService {
public List readAll() {
return this.projectRepository.findAll();
}
+
+ public ProjectEntity findById(Long id) {
+ Optional articleEntity = projectRepository.findById(id);
+
+ if (articleEntity.isEmpty()) {
+ throw new ResourceNotFoundException("Project with id " + id + " not found");
+ }
+
+ return articleEntity.get();
+ }
}
diff --git a/src/main/java/de/szut/lf8_starter/project/CreateProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/CreateProjectAction.java
similarity index 89%
rename from src/main/java/de/szut/lf8_starter/project/CreateProjectAction.java
rename to src/main/java/de/szut/lf8_starter/project/action/CreateProjectAction.java
index d54318b..3e636b5 100644
--- a/src/main/java/de/szut/lf8_starter/project/CreateProjectAction.java
+++ b/src/main/java/de/szut/lf8_starter/project/action/CreateProjectAction.java
@@ -1,5 +1,8 @@
-package de.szut.lf8_starter.project;
+package de.szut.lf8_starter.project.action;
+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 io.swagger.v3.oas.annotations.Operation;
diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectController.java b/src/main/java/de/szut/lf8_starter/project/action/GetAllProjectsAction.java
similarity index 60%
rename from src/main/java/de/szut/lf8_starter/project/ProjectController.java
rename to src/main/java/de/szut/lf8_starter/project/action/GetAllProjectsAction.java
index 61ee843..039a104 100644
--- a/src/main/java/de/szut/lf8_starter/project/ProjectController.java
+++ b/src/main/java/de/szut/lf8_starter/project/action/GetAllProjectsAction.java
@@ -1,32 +1,36 @@
-package de.szut.lf8_starter.project;
+package de.szut.lf8_starter.project.action;
+import de.szut.lf8_starter.project.ProjectMapper;
+import de.szut.lf8_starter.project.ProjectService;
import de.szut.lf8_starter.project.dto.GetProjectDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "projects")
-public class ProjectController {
+public class GetAllProjectsAction {
private final ProjectService service;
private final ProjectMapper projectMapper;
- public ProjectController(ProjectService service, ProjectMapper projectMapper) {
+
+ public GetAllProjectsAction(ProjectService service, ProjectMapper projectMapper) {
this.service = service;
this.projectMapper = projectMapper;
}
@Operation(summary = "returns all projects")
@ApiResponses(value = {
- @ApiResponse(responseCode = "200", description = "all projects",
- content = {@Content(mediaType = "application/json",
- schema = @Schema(implementation = GetProjectDto.class))}),
- @ApiResponse(responseCode = "401", description = "not authorized",
- content = @Content)})
+ @ApiResponse(responseCode = "200", description = "all projects", content = {
+ @Content(mediaType = "application/json", schema = @Schema(implementation = GetProjectDto.class))
+ }),
+ @ApiResponse(responseCode = "401", description = "not authorized", content = @Content)})
@GetMapping
public List findAll() {
return this.service
diff --git a/src/main/java/de/szut/lf8_starter/project/action/GetProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/GetProjectAction.java
new file mode 100644
index 0000000..b581197
--- /dev/null
+++ b/src/main/java/de/szut/lf8_starter/project/action/GetProjectAction.java
@@ -0,0 +1,28 @@
+package de.szut.lf8_starter.project.action;
+
+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 org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+@RestController
+@RequestMapping(value = "projects")
+public class GetProjectAction {
+ private final ProjectService projectService;
+ private final ProjectMapper projectMapper;
+
+ public GetProjectAction(ProjectService projectService, ProjectMapper projectMapper) {
+ this.projectService = projectService;
+ this.projectMapper = projectMapper;
+ }
+
+ @GetMapping("/{id}")
+ public ResponseEntity findArticleById(@PathVariable Long id) {
+ ProjectEntity project = this.projectService.findById(id);
+
+ return new ResponseEntity<>(this.projectMapper.mapToGetDto(project), HttpStatus.OK);
+ }
+}