From dcaeef600d4382cfee0d0cc5713642057f14a150 Mon Sep 17 00:00:00 2001 From: Phan Huy Tran Date: Wed, 2 Oct 2024 10:00:59 +0200 Subject: [PATCH] Implement update project route --- requests/getProject.http | 2 +- requests/updateProject.http | 15 +++++++ .../lf8_starter/project/ProjectMapper.java | 21 ++++++++++ .../lf8_starter/project/ProjectService.java | 13 +++---- .../project/action/GetProjectAction.java | 10 ++++- .../project/action/UpdateProjectAction.java | 39 +++++++++++++++++++ .../project/dto/UpdateProjectDto.java | 38 ++++++++++++++++++ 7 files changed, 128 insertions(+), 10 deletions(-) create mode 100644 requests/updateProject.http create mode 100644 src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java create mode 100644 src/main/java/de/szut/lf8_starter/project/dto/UpdateProjectDto.java diff --git a/requests/getProject.http b/requests/getProject.http index 5758af5..0bf862f 100644 --- a/requests/getProject.http +++ b/requests/getProject.http @@ -1,3 +1,3 @@ ### GET request to example server -GET http://localhost:8080/projects/2 +GET http://localhost:8080/projects/1 Authorization: Bearer {{auth_token}} diff --git a/requests/updateProject.http b/requests/updateProject.http new file mode 100644 index 0000000..dfa5f0b --- /dev/null +++ b/requests/updateProject.http @@ -0,0 +1,15 @@ +### GET request to example server +PUT http://localhost:8080/projects/1 +Authorization: Bearer {{auth_token}} +Content-Type: application/json + +{ + "name": "newName", + "leading_employee": 2, + "employees": [], + "contractor": 9, + "contractor_name": "New Contractor name", + "comment": "new goal of project", + "start_date": "01.01.2010", + "planned_end_date": "01.01.2021" +} \ 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 index fa60491..608233c 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectMapper.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectMapper.java @@ -2,6 +2,8 @@ package de.szut.lf8_starter.project; 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 jakarta.validation.Valid; import org.springframework.stereotype.Service; @Service @@ -38,4 +40,23 @@ public class ProjectMapper { return getProjectDto; } + + public ProjectEntity mapUpdateDtoToEntity(UpdateProjectDto updateProjectDto, ProjectEntity projectEntity) { + projectEntity.setName(updateProjectDto.getName() != null ? updateProjectDto.getName() : projectEntity.getName()); + projectEntity.setLeadingEmployee(updateProjectDto.getLeadingEmployee() != null ? updateProjectDto.getLeadingEmployee() : projectEntity.getLeadingEmployee()); + projectEntity.setContractor(updateProjectDto.getContractor() != null ? updateProjectDto.getContractor() : projectEntity.getContractor()); + projectEntity.setContractorName(updateProjectDto.getContractorName() != null ? updateProjectDto.getContractorName() : projectEntity.getContractorName()); + projectEntity.setComment(updateProjectDto.getComment() != null ? updateProjectDto.getComment() : projectEntity.getComment()); + projectEntity.setStartDate(updateProjectDto.getStartDate() != null ? updateProjectDto.getStartDate() : projectEntity.getStartDate()); + projectEntity.setPlannedEndDate(updateProjectDto.getPlannedEndDate() != null ? updateProjectDto.getPlannedEndDate() : projectEntity.getPlannedEndDate()); + projectEntity.setEndDate(updateProjectDto.getEndDate() != null ? updateProjectDto.getEndDate() : projectEntity.getEndDate()); + + if (updateProjectDto.getEmployees() != null) { + projectEntity.getEmployees().clear(); + + projectEntity.setEmployees(updateProjectDto.getEmployees()); + } + + return projectEntity; + } } 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 a815350..ef19d44 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectService.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectService.java @@ -1,6 +1,5 @@ package de.szut.lf8_starter.project; -import de.szut.lf8_starter.exceptionHandling.ResourceNotFoundException; import org.springframework.stereotype.Service; import java.util.List; @@ -22,13 +21,13 @@ public class ProjectService { return this.projectRepository.findAll(); } - public ProjectEntity findById(Long id) { - Optional articleEntity = projectRepository.findById(id); + public Optional findById(Long id) { + return projectRepository.findById(id); + } - if (articleEntity.isEmpty()) { - throw new ResourceNotFoundException("Project with id " + id + " not found"); - } + public ProjectEntity update(ProjectEntity project) { + this.projectRepository.save(project); - return articleEntity.get(); + return project; } } 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 index d04ac20..b435bdd 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/GetProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/GetProjectAction.java @@ -13,6 +13,8 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.Optional; + @RestController @RequestMapping(value = "projects") public class GetProjectAction { @@ -33,8 +35,12 @@ public class GetProjectAction { }) @GetMapping("/{id}") public ResponseEntity findArticleById(@PathVariable Long id) { - ProjectEntity project = this.projectService.findById(id); + Optional project = this.projectService.findById(id); - return new ResponseEntity<>(this.projectMapper.mapToGetDto(project), HttpStatus.OK); + if (project.isEmpty()) { + return new ResponseEntity<>(null, HttpStatus.NOT_FOUND); + } + + return new ResponseEntity<>(this.projectMapper.mapToGetDto(project.get()), HttpStatus.OK); } } diff --git a/src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java new file mode 100644 index 0000000..1e94754 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/action/UpdateProjectAction.java @@ -0,0 +1,39 @@ +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 de.szut.lf8_starter.project.dto.UpdateProjectDto; +import jakarta.validation.Valid; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.Optional; + +@RestController +@RequestMapping(value = "/projects") +public class UpdateProjectAction { + private final ProjectService projectService; + private final ProjectMapper projectMapper; + + public UpdateProjectAction(ProjectService projectService, ProjectMapper mappingService) { + this.projectService = projectService; + this.projectMapper = mappingService; + } + + @PutMapping("/{id}") + public ResponseEntity updateSupplier(@PathVariable Long id, @Valid @RequestBody UpdateProjectDto updateProjectDto) { + Optional project = this.projectService.findById(id); + + if (project.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + ProjectEntity updatedProject = this.projectMapper.mapUpdateDtoToEntity(updateProjectDto, project.get()); + this.projectService.update(updatedProject); + + return new ResponseEntity<>(this.projectMapper.mapToGetDto(updatedProject), HttpStatus.OK); + } +} diff --git a/src/main/java/de/szut/lf8_starter/project/dto/UpdateProjectDto.java b/src/main/java/de/szut/lf8_starter/project/dto/UpdateProjectDto.java new file mode 100644 index 0000000..0dc7f69 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/dto/UpdateProjectDto.java @@ -0,0 +1,38 @@ +package de.szut.lf8_starter.project.dto; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDate; +import java.util.List; + +@Getter +@Setter +@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) +public class UpdateProjectDto { + private String name; + + private Long leadingEmployee; + + private List employees; + + private Long contractor; + + private String contractorName; + + private String comment; + + @JsonFormat(pattern = "dd.MM.yyyy") + private LocalDate startDate; + + @JsonFormat(pattern = "dd.MM.yyyy") + private LocalDate plannedEndDate; + + @JsonFormat(pattern = "dd.MM.yyyy") + private LocalDate endDate; +}