From da7905a3a810e4180e929bca7b8b79d64245e0c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Tue, 1 Oct 2024 20:44:45 +0200 Subject: [PATCH] PMT-32: NEEDS SPLITTING AND CURRENT PR #6 --- api/pmt.yml | 32 ++++++------- src/main/java/de/hmmh/pmt/ApiController.java | 27 +++++++---- src/main/java/de/hmmh/pmt/db/Project.java | 47 +++++++++++++++++++ .../de/hmmh/pmt/db/ProjectRepository.java | 6 +++ .../java/de/hmmh/pmt/project/GetAllTest.java | 32 +++++++++++++ 5 files changed, 118 insertions(+), 26 deletions(-) create mode 100644 src/main/java/de/hmmh/pmt/db/Project.java create mode 100644 src/main/java/de/hmmh/pmt/db/ProjectRepository.java create mode 100644 src/test/java/de/hmmh/pmt/project/GetAllTest.java diff --git a/api/pmt.yml b/api/pmt.yml index 06e99b1..b498fc0 100644 --- a/api/pmt.yml +++ b/api/pmt.yml @@ -14,20 +14,20 @@ components: type: http scheme: bearer bearerFormat: JWT - schemas: - HelloOut: - description: "A Test Schema" + schemas: + ProjectInfo: type: object - properties: - msg: + properties: + id: + type: integer + format: int64 + name: type: string + GetAllProjectsDTO: + type: array + items: + $ref: "#/components/schemas/ProjectInfo" responses: - OK: - description: "OK" - content: - text/plain: - schema: - type: string InternalError: description: "Internal Server Error" content: @@ -35,16 +35,16 @@ components: schema: type: string paths: - /hello: + /project: get: - operationId: "GetHello" - description: "A Simple Hello World Endpoint" + operationId: "getAllProjects" + description: "Get a List of all Projects" responses: 200: - description: "A Hello Response" + description: "" content: application/json: schema: - $ref: "#/components/schemas/HelloOut" + $ref: "#/components/schemas/GetAllProjectsDTO" 500: $ref: "#/components/responses/InternalError" \ No newline at end of file diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index 8b56898..5031841 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -2,9 +2,11 @@ package de.hmmh.pmt; import com.fasterxml.jackson.databind.ObjectMapper; import de.hmmh.pmt.employee.ApiClientFactory; -import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO; +import de.hmmh.pmt.db.Project; +import de.hmmh.pmt.db.ProjectRepository; import de.hmmh.pmt.oas.DefaultApi; -import de.hmmh.pmt.dtos.HelloOut; +import de.hmmh.pmt.dtos.GetAllProjectsDTO; +import de.hmmh.pmt.dtos.ProjectInfo; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -18,6 +20,10 @@ import java.util.Optional; public class ApiController implements DefaultApi { @Autowired private ApiClientFactory apiClientFactory; + @Autowired + private ProjectRepository projectRepository; + + // apiClientFactory.getEmployeeApi().findAll1() @Override public Optional getObjectMapper() { @@ -30,15 +36,16 @@ public class ApiController implements DefaultApi { } @Override - public ResponseEntity getHello() { + public ResponseEntity getAllProjects() { + GetAllProjectsDTO response = new GetAllProjectsDTO(); - StringBuilder employees = new StringBuilder(); - for (EmployeeResponseDTO employeeResponseDTO : apiClientFactory.getEmployeeApi().findAll1()) { - employees.append(employeeResponseDTO.toString()); + for (Project project : this.projectRepository.findAll()){ + ProjectInfo projectInfo = new ProjectInfo(); + projectInfo.setId(project.getId()); + projectInfo.setName(project.getName()); + response.add(projectInfo); } - - HelloOut hello = new HelloOut(); - hello.setMsg(employees.toString()); - return ResponseEntity.ok(hello); + + return ResponseEntity.ok(response); } } diff --git a/src/main/java/de/hmmh/pmt/db/Project.java b/src/main/java/de/hmmh/pmt/db/Project.java new file mode 100644 index 0000000..9c9891f --- /dev/null +++ b/src/main/java/de/hmmh/pmt/db/Project.java @@ -0,0 +1,47 @@ +package de.hmmh.pmt.db; + +import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.time.LocalDateTime; + +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +@Entity +@Table(name = "project") +public class Project { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @NotBlank + @Size(min = 3, max = 64) + private String name; + + @NotBlank + @Size(min = 10) + private String goal; + + @NotNull + private Long customerId; + + @NotNull + private Long administratorId; // Is an Employee + + @NotNull + private LocalDateTime start; + + @NotNull + private LocalDateTime plannedEnd; + + private LocalDateTime realEnd; // Cant be named just "end" because it's and SQL Keyword +} + diff --git a/src/main/java/de/hmmh/pmt/db/ProjectRepository.java b/src/main/java/de/hmmh/pmt/db/ProjectRepository.java new file mode 100644 index 0000000..ef501dc --- /dev/null +++ b/src/main/java/de/hmmh/pmt/db/ProjectRepository.java @@ -0,0 +1,6 @@ +package de.hmmh.pmt.db; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ProjectRepository extends JpaRepository { +} diff --git a/src/test/java/de/hmmh/pmt/project/GetAllTest.java b/src/test/java/de/hmmh/pmt/project/GetAllTest.java new file mode 100644 index 0000000..baa813b --- /dev/null +++ b/src/test/java/de/hmmh/pmt/project/GetAllTest.java @@ -0,0 +1,32 @@ +package de.hmmh.pmt.project; + +import de.hmmh.pmt.IntegrationTest; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasSize; +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; + +public class GetAllTest extends IntegrationTest { + @Test + void noProjects() throws Exception { + mvc + .perform(get(baseUri + "/project")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$", empty())); + } + + @Test + void multipleProjects() throws Exception { + createTestData(); + + mvc + .perform(get(baseUri + "/project")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$", hasSize(3))); + } + +}