feat: Implement Update Project Route (SCRUM-11) #28

Merged
jank merged 8 commits from feature/update-project into main 2024-10-02 08:52:19 +00:00
7 changed files with 128 additions and 10 deletions
Showing only changes of commit dcaeef600d - Show all commits

@ -1,3 +1,3 @@
### GET request to example server ### GET request to example server
GET http://localhost:8080/projects/2 GET http://localhost:8080/projects/1
Authorization: Bearer {{auth_token}} Authorization: Bearer {{auth_token}}

@ -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"
}

@ -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.CreateProjectDto;
import de.szut.lf8_starter.project.dto.GetProjectDto; 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; import org.springframework.stereotype.Service;
@Service @Service
@ -38,4 +40,23 @@ public class ProjectMapper {
return getProjectDto; 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;
}
} }

@ -1,6 +1,5 @@
package de.szut.lf8_starter.project; package de.szut.lf8_starter.project;
import de.szut.lf8_starter.exceptionHandling.ResourceNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@ -22,13 +21,13 @@ public class ProjectService {
return this.projectRepository.findAll(); return this.projectRepository.findAll();
} }
public ProjectEntity findById(Long id) { public Optional<ProjectEntity> findById(Long id) {
Optional<ProjectEntity> articleEntity = projectRepository.findById(id); return projectRepository.findById(id);
if (articleEntity.isEmpty()) {
throw new ResourceNotFoundException("Project with id " + id + " not found");
} }
return articleEntity.get(); public ProjectEntity update(ProjectEntity project) {
this.projectRepository.save(project);
return project;
} }
} }

@ -13,6 +13,8 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController @RestController
@RequestMapping(value = "projects") @RequestMapping(value = "projects")
public class GetProjectAction { public class GetProjectAction {
@ -33,8 +35,12 @@ public class GetProjectAction {
}) })
@GetMapping("/{id}") @GetMapping("/{id}")
public ResponseEntity<GetProjectDto> findArticleById(@PathVariable Long id) { public ResponseEntity<GetProjectDto> findArticleById(@PathVariable Long id) {
ProjectEntity project = this.projectService.findById(id); Optional<ProjectEntity> 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);
} }
} }

@ -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<GetProjectDto> updateSupplier(@PathVariable Long id, @Valid @RequestBody UpdateProjectDto updateProjectDto) {
Optional<ProjectEntity> 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);
}
}

@ -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<Long> 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;
}