diff --git a/src/main/java/de/szut/lf8_starter/project/action/employee/RemoveEmployeeFromProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/employee/RemoveEmployeeFromProjectAction.java new file mode 100644 index 0000000..4fec8e7 --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/action/employee/RemoveEmployeeFromProjectAction.java @@ -0,0 +1,52 @@ +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.*; + +import java.util.Optional; + +@RestController +public class RemoveEmployeeFromProjectAction { + private final ProjectService projectService; + private final EmployeeService employeeService; + + public RemoveEmployeeFromProjectAction(ProjectService projectService, EmployeeService employeeService) { + this.projectService = projectService; + this.employeeService = employeeService; + } + + @Operation(summary = "Remove an employee from a project") + @ApiResponses(value = { + @ApiResponse(responseCode = "204", description = "Employee removed from project"), + @ApiResponse(responseCode = "404", description = "Project or employee not found", content = @Content) + }) + @DeleteMapping("/projects/{projectId}/employees/{employeeId}") + public ResponseEntity remove( + @PathVariable Long projectId, + @PathVariable Long employeeId, + @RequestHeader("Authorization") String accessToken + ) { + Optional 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().remove(employeeId); + this.projectService.update(project.get()); + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } +}