feat: Implement add employee to project route (SCRUM-2) (!32)
Co-authored-by: Phan Huy Tran <p.tran@neusta.de> Reviewed-on: #32 Reviewed-by: Jan Gleytenhoover <krisellp9@gmail.com> Co-authored-by: Phan Huy Tran <ptran@noreply.localhost> Co-committed-by: Phan Huy Tran <ptran@noreply.localhost>
This commit is contained in:
parent
1e38efaa63
commit
1008278fde
24 changed files with 246 additions and 34 deletions
|
@ -0,0 +1,34 @@
|
|||
package de.szut.lf8_starter.employee;
|
||||
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Service
|
||||
public class EmployeeService {
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
public EmployeeService(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
public boolean employeeExists(String accessToken, Long employeeId) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setBearerAuth(accessToken.replace("Bearer ", ""));
|
||||
|
||||
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
|
||||
|
||||
String url = "https://employee.szut.dev/employees/" + employeeId;
|
||||
|
||||
try {
|
||||
restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
|
||||
} catch (HttpClientErrorException.NotFound e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -25,7 +25,7 @@ public class ProjectEntity {
|
|||
|
||||
private long leadingEmployee;
|
||||
|
||||
@ElementCollection
|
||||
@ElementCollection(fetch = FetchType.EAGER)
|
||||
private List<Long> employees;
|
||||
|
||||
private long contractor;
|
||||
|
|
|
@ -25,10 +25,8 @@ public class ProjectService {
|
|||
return projectRepository.findById(id);
|
||||
}
|
||||
|
||||
public ProjectEntity update(ProjectEntity project) {
|
||||
public void update(ProjectEntity project) {
|
||||
this.projectRepository.save(project);
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
public void delete(Long id) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package de.szut.lf8_starter.project.action;
|
||||
package de.szut.lf8_starter.project.action.crud;
|
||||
|
||||
import de.szut.lf8_starter.project.ProjectEntity;
|
||||
import de.szut.lf8_starter.project.ProjectMapper;
|
|
@ -1,4 +1,4 @@
|
|||
package de.szut.lf8_starter.project.action;
|
||||
package de.szut.lf8_starter.project.action.crud;
|
||||
|
||||
import de.szut.lf8_starter.project.ProjectMapper;
|
||||
import de.szut.lf8_starter.project.ProjectService;
|
|
@ -1,4 +1,4 @@
|
|||
package de.szut.lf8_starter.project.action;
|
||||
package de.szut.lf8_starter.project.action.crud;
|
||||
|
||||
import de.szut.lf8_starter.project.ProjectEntity;
|
||||
import de.szut.lf8_starter.project.ProjectMapper;
|
||||
|
@ -11,7 +11,10 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Optional;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package de.szut.lf8_starter.project.action;
|
||||
package de.szut.lf8_starter.project.action.crud;
|
||||
|
||||
import de.szut.lf8_starter.project.ProjectEntity;
|
||||
import de.szut.lf8_starter.project.ProjectMapper;
|
|
@ -0,0 +1,55 @@
|
|||
package de.szut.lf8_starter.project.action.employee;
|
||||
|
||||
import de.szut.lf8_starter.employee.EmployeeService;
|
||||
import de.szut.lf8_starter.project.ProjectEntity;
|
||||
import de.szut.lf8_starter.project.ProjectService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
public class AddEmployeeToProjectAction {
|
||||
private final ProjectService projectService;
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
public AddEmployeeToProjectAction(ProjectService projectService, EmployeeService employeeService) {
|
||||
this.projectService = projectService;
|
||||
this.employeeService = employeeService;
|
||||
}
|
||||
|
||||
@Operation(summary = "Add an employee to a project")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "204", description = "Employee added to project"),
|
||||
@ApiResponse(responseCode = "404", description = "Project or employee not found", content = @Content)
|
||||
})
|
||||
@PostMapping("/projects/{projectId}/employees/{employeeId}")
|
||||
public ResponseEntity<Object> create(
|
||||
@PathVariable Long projectId,
|
||||
@PathVariable Long employeeId,
|
||||
@RequestHeader("Authorization") String accessToken
|
||||
) {
|
||||
Optional<ProjectEntity> project = this.projectService.findById(projectId);
|
||||
|
||||
if (project.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
if (!this.employeeService.employeeExists(accessToken, employeeId)) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
project.get().getEmployees().add(employeeId);
|
||||
this.projectService.update(project.get());
|
||||
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package de.szut.lf8_starter.project.dto;
|
||||
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
||||
public class EmployeeDto {
|
||||
private long id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String street;
|
||||
private String postcode;
|
||||
private String city;
|
||||
private String phone;
|
||||
private List<String> skillSet;
|
||||
}
|
Reference in a new issue