@@ -148,8 +148,40 @@ public class TaskController {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(TaskResponse.fromEntity(createdTask));
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public String deleteTasks() {
|
||||
return "Delete Tasks";
|
||||
/**
|
||||
* Delete a task given its ID.
|
||||
*
|
||||
* @param id Task identification.
|
||||
* @throws TaskNotFoundException when task not found.
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
@Operation(
|
||||
summary = "Delete a task",
|
||||
description = "Delete a task given its ID.",
|
||||
responses = {
|
||||
@ApiResponse(
|
||||
responseCode = "204",
|
||||
description = "Task successfully deleted",
|
||||
content = @Content(schema = @Schema(implementation = Void.class))),
|
||||
@ApiResponse(
|
||||
responseCode = "403",
|
||||
description = "Forbidden. Access Denied",
|
||||
content = @Content(schema = @Schema(implementation = Void.class))),
|
||||
@ApiResponse(
|
||||
responseCode = "404",
|
||||
description = "Task not found",
|
||||
content = @Content(schema = @Schema(implementation = Void.class)))
|
||||
})
|
||||
public ResponseEntity<Void> deleteTasks(
|
||||
@Parameter(
|
||||
name = "id",
|
||||
in = ParameterIn.PATH,
|
||||
description = "Task id to be patched.",
|
||||
required = true,
|
||||
schema = @Schema(type = "integer", format = "int64"))
|
||||
@PathVariable
|
||||
Long id) {
|
||||
taskService.deleteTask(id);
|
||||
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,5 @@ public interface TaskService {
|
||||
|
||||
public TaskResponse patchTask(Long taskId, TaskPatchRequest taskRequest);
|
||||
|
||||
public void updateTaskDone(Long taskId);
|
||||
|
||||
public void deleteTask(Long taskId);
|
||||
}
|
||||
|
||||
+22
-9
@@ -69,8 +69,8 @@ class TaskServiceImpl implements TaskService {
|
||||
return created;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
@Transactional
|
||||
public TaskResponse patchTask(Long taskId, TaskPatchRequest patch) {
|
||||
UserEntity user = getCurrentUser();
|
||||
|
||||
@@ -114,15 +114,28 @@ class TaskServiceImpl implements TaskService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTaskDone(Long taskId) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'updateTaskDone'");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteTask(Long taskId) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'deleteTask'");
|
||||
UserEntity user = getCurrentUser();
|
||||
|
||||
log.info("Deleting task {} to user {}", taskId, user.getId());
|
||||
|
||||
Optional<TaskEntity> task = taskRepository.findById(taskId);
|
||||
if (task.isEmpty()) {
|
||||
throw new TaskNotFoundException();
|
||||
}
|
||||
|
||||
List<TaskUrlEntity> urls = task.get().getUrls();
|
||||
if (!urls.isEmpty()) {
|
||||
taskUrlRepository.deleteAll(urls);
|
||||
log.info("Deleted {} urls from task {}", urls.size(), taskId);
|
||||
} else {
|
||||
log.info("No urls to delete for task {}", taskId);
|
||||
}
|
||||
|
||||
taskRepository.delete(task.get());
|
||||
|
||||
log.info("Task deleted! Id {}", taskId);
|
||||
}
|
||||
|
||||
private UserEntity getCurrentUser() {
|
||||
|
||||
@@ -7,16 +7,21 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/** This class contains utils methods to handle authentication. */
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AuthUtil {
|
||||
|
||||
/**
|
||||
* Get the current user email, from the security context.
|
||||
*
|
||||
* @return Optional instance containing user email, if found.
|
||||
*/
|
||||
public Optional<String> getCurrentUserEmail() {
|
||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (!Objects.isNull(auth) && auth.isAuthenticated()) {
|
||||
Object principal = auth.getPrincipal();
|
||||
if (principal instanceof String userEmail) {
|
||||
log.info("Auth email: {}", userEmail);
|
||||
return Optional.of(userEmail);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user