From ee90ff7a5ece7c2f0fa24de518a42161237f5c69 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 25 Sep 2024 12:05:12 +0200 Subject: [PATCH] feat(project): add ProjectController, Service, Mapper, and DTO --- .../project/ProjectController.java | 38 +++++++++++++++++++ .../lf8_starter/project/ProjectMapper.java | 21 ++++++++++ .../lf8_starter/project/ProjectService.java | 21 ++++++++++ .../project/dto/ProjectGetDto.java | 29 ++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 src/main/java/de/szut/lf8_starter/project/ProjectController.java create mode 100644 src/main/java/de/szut/lf8_starter/project/ProjectMapper.java create mode 100644 src/main/java/de/szut/lf8_starter/project/ProjectService.java create mode 100644 src/main/java/de/szut/lf8_starter/project/dto/ProjectGetDto.java diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectController.java b/src/main/java/de/szut/lf8_starter/project/ProjectController.java new file mode 100644 index 0000000..259efff --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/ProjectController.java @@ -0,0 +1,38 @@ +package de.szut.lf8_starter.project; + +import de.szut.lf8_starter.project.dto.ProjectGetDto; +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 java.util.List; + +@RestController +@RequestMapping(value = "projects") +public class ProjectController { + private final ProjectService service; + private final ProjectMapper projectMapper; + public ProjectController(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 = ProjectGetDto.class))}), + @ApiResponse(responseCode = "401", description = "not authorized", + content = @Content)}) + @GetMapping + public List findAll() { + return this.service + .readAll() + .stream() + .map(this.projectMapper::mapToGetDto) + .toList(); + } +} \ No newline at end of file diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectMapper.java b/src/main/java/de/szut/lf8_starter/project/ProjectMapper.java new file mode 100644 index 0000000..1846038 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/ProjectMapper.java @@ -0,0 +1,21 @@ +package de.szut.lf8_starter.project; + +import de.szut.lf8_starter.project.dto.ProjectGetDto; +import org.springframework.stereotype.Service; + +@Service +public class ProjectMapper { + public ProjectGetDto mapToGetDto(ProjectEntity entity) { + return new ProjectGetDto( + entity.getId(), + entity.getName(), + entity.getLeadingEmployee(), + entity.getEmployees(), + entity.getContractor(), + entity.getContractorName(), + entity.getComment(), + entity.getStartDate(), + entity.getEndDate() + ); + } +} diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectService.java b/src/main/java/de/szut/lf8_starter/project/ProjectService.java new file mode 100644 index 0000000..8aeedfa --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/ProjectService.java @@ -0,0 +1,21 @@ +package de.szut.lf8_starter.project; + +import de.szut.lf8_starter.hello.HelloEntity; +import de.szut.lf8_starter.hello.HelloRepository; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +public class ProjectService { + private final ProjectRepository repository; + + public ProjectService(ProjectRepository repository) { + this.repository = repository; + } + + public List readAll() { + return this.repository.findAll(); + } +} diff --git a/src/main/java/de/szut/lf8_starter/project/dto/ProjectGetDto.java b/src/main/java/de/szut/lf8_starter/project/dto/ProjectGetDto.java new file mode 100644 index 0000000..4d53212 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/dto/ProjectGetDto.java @@ -0,0 +1,29 @@ +package de.szut.lf8_starter.project.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.time.LocalDate; +import java.util.List; + +@Data +@AllArgsConstructor +public class ProjectGetDto { + private long id; + + private String name; + + private long leadingEmployee; + + private List employees; + + private long contractor; + + private String contractorName; + + private String comment; + + private LocalDate startDate; + + private LocalDate endDate; +}