Implemented the remove task workflow. Still got one todo for the workflow that I want to discuss

This commit is contained in:
reynisson 2021-12-14 23:46:07 +01:00
parent 45edd76c8b
commit 3e141e5318
9 changed files with 37 additions and 38 deletions

View File

@ -3,6 +3,7 @@ package ch.unisg.roster.roster.adapter.in.web;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -22,10 +23,10 @@ public class DeleteTaskController {
* Controller to delete a task * Controller to delete a task
* @return 200 OK, 409 Conflict * @return 200 OK, 409 Conflict
**/ **/
@DeleteMapping(path = "/task", consumes = {"application/task+json"}) @DeleteMapping(path = "/task/{taskId}")
public ResponseEntity<Void> applyForTask(@RequestBody Task task) { public ResponseEntity<Void> deleteTask(@PathVariable("taskId") String taskId) {
DeleteTaskCommand command = new DeleteTaskCommand(task.getTaskID(), task.getTaskType()); DeleteTaskCommand command = new DeleteTaskCommand(taskId);
if (deleteTaskUseCase.deleteTask(command)) { if (deleteTaskUseCase.deleteTask(command)) {
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);

View File

@ -13,12 +13,8 @@ public class DeleteTaskCommand extends SelfValidating<ApplyForTaskCommand> {
@NotNull @NotNull
private final String taskId; private final String taskId;
@NotNull public DeleteTaskCommand(String taskId) {
private final ExecutorType taskType;
public DeleteTaskCommand(String taskId, ExecutorType taskType) {
this.taskId = taskId; this.taskId = taskId;
this.taskType = taskType;
this.validateSelf(); this.validateSelf();
} }
} }

View File

@ -21,7 +21,7 @@ public class DeleteTaskService implements DeleteTaskUseCase {
@Override @Override
public boolean deleteTask(DeleteTaskCommand command) { public boolean deleteTask(DeleteTaskCommand command) {
Roster roster = Roster.getInstance(); Roster roster = Roster.getInstance();
return roster.deleteTask(command.getTaskId(), command.getTaskType()); return roster.deleteTask(command.getTaskId());
} }
} }

View File

@ -76,9 +76,14 @@ public class Roster {
* Deletes a task if it is still in the queue. * Deletes a task if it is still in the queue.
* @return Whether the task got deleted or not * @return Whether the task got deleted or not
**/ **/
public boolean deleteTask(String taskID, ExecutorType taskType) { public boolean deleteTask(String taskID) {
logger.log(Level.INFO, "Try to delete task with id {0}", taskID); logger.log(Level.INFO, "Try to delete task with id {0}", taskID);
return queues.get(taskType.getValue()).removeIf(task -> task.getTaskID().equalsIgnoreCase(taskID)); for(var listOfTasks : queues.entrySet()){
if(listOfTasks.getValue().removeIf(task -> task.getTaskID().equals(taskID))){
return true;
}
}
return false;
} }
public void initialiseRoster(List<RosterItem> rosterItemList){ public void initialiseRoster(List<RosterItem> rosterItemList){

View File

@ -31,7 +31,7 @@ public class RosterTest {
assertThat(task.getTaskType().getValue().toString()).isEqualTo("TEST-TYPE"); assertThat(task.getTaskType().getValue().toString()).isEqualTo("TEST-TYPE");
assertThat(task.getTaskID()).isEqualTo("TEST-ID"); assertThat(task.getTaskID()).isEqualTo("TEST-ID");
boolean empty_queue = roster.deleteTask("TEST-ID", new ExecutorType("TEST-TYPE")); boolean empty_queue = roster.deleteTask("TEST-ID");
// TODO test that the task was removed from the Queue similar to below --> I don't know if it actually gets deleted or not // TODO test that the task was removed from the Queue similar to below --> I don't know if it actually gets deleted or not
//assertThat(empty_queue).isEqualTo(true); //assertThat(empty_queue).isEqualTo(true);
//assertThat(queues.size()).isEqualTo(0); //assertThat(queues.size()).isEqualTo(0);
@ -44,8 +44,8 @@ public class RosterTest {
queues.clear(); queues.clear();
roster.addTaskToQueue(new Task("TEST-ID", "TEST-TYPE")); roster.addTaskToQueue(new Task("TEST-ID", "TEST-TYPE"));
boolean test = roster.deleteTask("TEST-ID", new ExecutorType("TEST-TYPE")); boolean test = roster.deleteTask("TEST-ID");
// TODO Fix assert for queue
assertThat(test).isEqualTo(true); assertThat(test).isEqualTo(true);
assertThat(queues.size()).isEqualTo(1); assertThat(queues.size()).isEqualTo(1);
} }

View File

@ -25,6 +25,7 @@ public class DeleteTaskWebController {
this.deleteClassUseCase = deleteClassUseCase; this.deleteClassUseCase = deleteClassUseCase;
} }
// TODO change to DELETE and why are we using task URI here?
@PostMapping(path="/tasks/deleteTask", consumes = {TaskJsonRepresentation.MEDIA_TYPE}) @PostMapping(path="/tasks/deleteTask", consumes = {TaskJsonRepresentation.MEDIA_TYPE})
public ResponseEntity<String> deleteTask (@RequestBody Task task){ public ResponseEntity<String> deleteTask (@RequestBody Task task){
try { try {
@ -35,6 +36,7 @@ public class DeleteTaskWebController {
// Check if the task with the given identifier exists // Check if the task with the given identifier exists
if (deleteATask.isEmpty()) { if (deleteATask.isEmpty()) {
// If not, through a 404 Not Found status code // If not, through a 404 Not Found status code
// TODO is 404 the best here?
throw new ResponseStatusException(HttpStatus.NOT_FOUND); throw new ResponseStatusException(HttpStatus.NOT_FOUND);
} }

View File

@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
@ -25,36 +26,24 @@ public class CanTaskBeDeletedWebAdapter implements CanTaskBeDeletedPort {
String server; String server;
@Override @Override
public void canTaskBeDeletedEvent(DeleteTaskEvent event){ public boolean canTaskBeDeletedEvent(DeleteTaskEvent event){
var values = new HashMap<> () {{
put("taskId", event.taskId);
put("taskUri", event.taskUri);
}};
var objectMapper = new ObjectMapper();
String requestBody = null;
try {
requestBody = objectMapper.writeValueAsString(values);
} catch (JsonProcessingException e){
e.printStackTrace();
}
//Todo: Question: How do we include the URI from the DeleteTaskEvent? Do we even need it? //Todo: Question: How do we include the URI from the DeleteTaskEvent? Do we even need it?
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(server+"task")) .uri(URI.create(server+"$/task/" + event.taskId))
.header("Content-Type", "application/task+json") .header("Content-Type", "application/task+json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody)) .DELETE()
.build(); .build();
//Todo: The following parameters probably need to be changed to get the right error code
try { try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.statusCode() == HttpStatus.OK.value();
} catch (IOException e){ } catch (IOException e){
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
return false;
} }
} }

View File

@ -3,5 +3,5 @@ package ch.unisg.tapastasks.tasks.application.port.out;
import ch.unisg.tapastasks.tasks.domain.DeleteTaskEvent; import ch.unisg.tapastasks.tasks.domain.DeleteTaskEvent;
public interface CanTaskBeDeletedPort { public interface CanTaskBeDeletedPort {
void canTaskBeDeletedEvent(DeleteTaskEvent event); boolean canTaskBeDeletedEvent(DeleteTaskEvent event);
} }

View File

@ -3,6 +3,8 @@ package ch.unisg.tapastasks.tasks.application.service;
import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskCommand; import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskCommand;
import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskUseCase; import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskUseCase;
import ch.unisg.tapastasks.tasks.application.port.out.CanTaskBeDeletedPort;
import ch.unisg.tapastasks.tasks.domain.DeleteTaskEvent;
import ch.unisg.tapastasks.tasks.domain.Task; import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList; import ch.unisg.tapastasks.tasks.domain.TaskList;
import jdk.jshell.spi.ExecutionControl; import jdk.jshell.spi.ExecutionControl;
@ -17,17 +19,21 @@ import java.util.Optional;
@Transactional @Transactional
public class DeleteTaskService implements DeleteTaskUseCase { public class DeleteTaskService implements DeleteTaskUseCase {
private final CanTaskBeDeletedPort canTaskBeDeletedPort;
@Override @Override
public Optional<Task> deleteTask(DeleteTaskCommand command){ public Optional<Task> deleteTask(DeleteTaskCommand command){
TaskList taskList = TaskList.getTapasTaskList(); TaskList taskList = TaskList.getTapasTaskList();
Optional<Task> updatedTask = taskList.retrieveTaskById(command.getTaskId()); Optional<Task> updatedTask = taskList.retrieveTaskById(command.getTaskId());
Task newTask = updatedTask.get();
// TODO: Fill in the right condition into the if-statement and the else-statement if(updatedTask.isPresent() && updatedTask.get().getTaskStatus().getValue().equals(Task.Status.OPEN)){
if (true){ // If task exists, and it is open then we try deleting it from the roster
if(canTaskBeDeletedPort.canTaskBeDeletedEvent(new DeleteTaskEvent(command.getTaskId().getValue(), command.getTaskUri().getValue()))){
return taskList.deleteTaskById(command.getTaskId()); return taskList.deleteTaskById(command.getTaskId());
} }
// TODO Handle with a return message }
return Optional.empty(); return Optional.empty();
} }
} }