diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/CompleteTaskWebController.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/CompleteTaskWebController.java new file mode 100644 index 0000000..e160c2b --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/CompleteTaskWebController.java @@ -0,0 +1,41 @@ +package ch.unisg.tapastasks.tasks.adapter.in.web; + +import ch.unisg.tapastasks.tasks.application.port.in.CompleteTaskCommand; +import ch.unisg.tapastasks.tasks.application.port.in.CompleteTaskUseCase; +import ch.unisg.tapastasks.tasks.domain.Task; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +import javax.validation.ConstraintViolationException; + +@RestController +public class CompleteTaskWebController { + private final CompleteTaskUseCase completeTaskUseCase; + + public CompleteTaskWebController(CompleteTaskUseCase completeTaskUseCase){ + this.completeTaskUseCase = completeTaskUseCase; + } + + @PostMapping(path = "/tasks/completeTask", consumes = {TaskMediaType.TASK_MEDIA_TYPE}) + public ResponseEntity completeTask (@RequestBody Task task){ + try { + CompleteTaskCommand command = new CompleteTaskCommand( + task.getTaskId(), task.getTaskResult() + ); + + Task updateATask = completeTaskUseCase.completeTask(command); + + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskMediaType.TASK_MEDIA_TYPE); + + return new ResponseEntity<>(TaskMediaType.serialize(updateATask), responseHeaders, HttpStatus.ACCEPTED); + } catch(ConstraintViolationException e){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); + } + } +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/DeleteTaskWebController.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/DeleteTaskWebController.java new file mode 100644 index 0000000..af721d1 --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/DeleteTaskWebController.java @@ -0,0 +1,49 @@ +package ch.unisg.tapastasks.tasks.adapter.in.web; + + +import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskCommand; +import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskUseCase; +import ch.unisg.tapastasks.tasks.domain.Task; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +import javax.validation.ConstraintViolationException; +import java.util.Optional; + +@RestController +public class DeleteTaskWebController { + private final DeleteTaskUseCase deleteClassUseCase; + + public DeleteTaskWebController(DeleteTaskUseCase deleteClassUseCase){ + this.deleteClassUseCase = deleteClassUseCase; + } + + @PostMapping(path="/tasks/deleteTask", consumes = {TaskMediaType.TASK_MEDIA_TYPE}) + public ResponseEntity deleteTask (@RequestBody Task task){ + try { + DeleteTaskCommand command = new DeleteTaskCommand(task.getTaskId()); + + Optional deleteATask = deleteClassUseCase.deleteTask(command); + + // Check if the task with the given identifier exists + if (deleteATask.isEmpty()) { + + // If not, through a 404 Not Found status code + throw new ResponseStatusException(HttpStatus.NOT_FOUND); + } + + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskMediaType.TASK_MEDIA_TYPE); + + + return new ResponseEntity<>(TaskMediaType.serialize(deleteATask.get()), responseHeaders, HttpStatus.ACCEPTED); + } catch(ConstraintViolationException e){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); + } + } +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskAssignedWebController.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskAssignedWebController.java new file mode 100644 index 0000000..9dfa6a2 --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskAssignedWebController.java @@ -0,0 +1,42 @@ +package ch.unisg.tapastasks.tasks.adapter.in.web; + +import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedCommand; +import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedUseCase; +import ch.unisg.tapastasks.tasks.domain.Task; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +import javax.validation.ConstraintViolationException; + +@RestController +public class TaskAssignedWebController { + private final TaskAssignedUseCase taskAssignedUseCase; + + public TaskAssignedWebController(TaskAssignedUseCase taskAssignedUseCase){ + this.taskAssignedUseCase = taskAssignedUseCase; + } + + @PostMapping(path="/tasks/assignTask", consumes= {TaskMediaType.TASK_MEDIA_TYPE}) + public ResponseEntity assignTask(@RequestBody Task task){ + try{ + TaskAssignedCommand command = new TaskAssignedCommand( + task.getTaskId() + ); + + Task updateATask = taskAssignedUseCase.assignTask(command); + + HttpHeaders responseHeaders = new HttpHeaders(); + responseHeaders.add(HttpHeaders.CONTENT_TYPE, TaskMediaType.TASK_MEDIA_TYPE); + + return new ResponseEntity<>(TaskMediaType.serialize(updateATask), responseHeaders, HttpStatus.ACCEPTED); + } catch (ConstraintViolationException e){ + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); + } + } + +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskMediaType.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskMediaType.java index 3c555e5..d9a0a46 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskMediaType.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/in/web/TaskMediaType.java @@ -15,7 +15,7 @@ final public class TaskMediaType { payload.put("taskType", task.getTaskType().getValue()); payload.put("taskState", task.getTaskState().getValue()); payload.put("taskListName", TaskList.getTapasTaskList().getTaskListName().getValue()); - + payload.put("taskResult", task.getTaskResult().getValue()); return payload.toString(); } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/PublishNewTaskAddedEventWebAdapter.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/PublishNewTaskAddedEventWebAdapter.java index db02f2a..7ae8e90 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/PublishNewTaskAddedEventWebAdapter.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/adapter/out/web/PublishNewTaskAddedEventWebAdapter.java @@ -29,6 +29,8 @@ public class PublishNewTaskAddedEventWebAdapter implements NewTaskAddedEventPort var values = new HashMap() {{ put("taskname",event.taskName); put("tasklist",event.taskListName); + put("taskId", event.taskId); + put("taskType", event.taskType); }}; var objectMapper = new ObjectMapper(); diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/CompleteTaskCommand.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/CompleteTaskCommand.java new file mode 100644 index 0000000..0634165 --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/CompleteTaskCommand.java @@ -0,0 +1,23 @@ +package ch.unisg.tapastasks.tasks.application.port.in; + +import ch.unisg.tapastasks.common.SelfValidating; +import ch.unisg.tapastasks.tasks.domain.Task.TaskId; +import ch.unisg.tapastasks.tasks.domain.Task.TaskResult; +import lombok.Value; + +import javax.validation.constraints.NotNull; + +@Value +public class CompleteTaskCommand extends SelfValidating { + @NotNull + private final TaskId taskId; + + @NotNull + private final TaskResult taskResult; + + public CompleteTaskCommand(TaskId taskId, TaskResult taskResult){ + this.taskId = taskId; + this.taskResult = taskResult; + this.validateSelf(); + } +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/CompleteTaskUseCase.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/CompleteTaskUseCase.java new file mode 100644 index 0000000..6fba7e6 --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/CompleteTaskUseCase.java @@ -0,0 +1,7 @@ +package ch.unisg.tapastasks.tasks.application.port.in; + +import ch.unisg.tapastasks.tasks.domain.Task; + +public interface CompleteTaskUseCase { + Task completeTask(CompleteTaskCommand command); +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/DeleteTaskCommand.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/DeleteTaskCommand.java new file mode 100644 index 0000000..24acbb8 --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/DeleteTaskCommand.java @@ -0,0 +1,18 @@ +package ch.unisg.tapastasks.tasks.application.port.in; + +import ch.unisg.tapastasks.common.SelfValidating; +import ch.unisg.tapastasks.tasks.domain.Task.TaskId; +import lombok.Value; + +import javax.validation.constraints.NotNull; + +@Value +public class DeleteTaskCommand extends SelfValidating { + @NotNull + private final TaskId taskId; + + public DeleteTaskCommand(TaskId taskId){ + this.taskId=taskId; + this.validateSelf(); + } +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/DeleteTaskUseCase.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/DeleteTaskUseCase.java new file mode 100644 index 0000000..8ba206f --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/DeleteTaskUseCase.java @@ -0,0 +1,9 @@ +package ch.unisg.tapastasks.tasks.application.port.in; + +import ch.unisg.tapastasks.tasks.domain.Task; + +import java.util.Optional; + +public interface DeleteTaskUseCase { + Optional deleteTask(DeleteTaskCommand command); +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/TaskAssignedCommand.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/TaskAssignedCommand.java new file mode 100644 index 0000000..7a5e383 --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/TaskAssignedCommand.java @@ -0,0 +1,18 @@ +package ch.unisg.tapastasks.tasks.application.port.in; + +import ch.unisg.tapastasks.common.SelfValidating; +import ch.unisg.tapastasks.tasks.domain.Task.TaskId; +import lombok.Value; + +import javax.validation.constraints.NotNull; + +@Value +public class TaskAssignedCommand extends SelfValidating { + @NotNull + private final TaskId taskId; + + public TaskAssignedCommand(TaskId taskId){ + this.taskId=taskId; + this.validateSelf(); + } +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/TaskAssignedUseCase.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/TaskAssignedUseCase.java new file mode 100644 index 0000000..3a84587 --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/port/in/TaskAssignedUseCase.java @@ -0,0 +1,7 @@ +package ch.unisg.tapastasks.tasks.application.port.in; + +import ch.unisg.tapastasks.tasks.domain.Task; + +public interface TaskAssignedUseCase { + Task assignTask(TaskAssignedCommand command); +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/AddNewTaskToTaskListService.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/AddNewTaskToTaskListService.java index 48c75a6..f1a6a8c 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/AddNewTaskToTaskListService.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/AddNewTaskToTaskListService.java @@ -30,7 +30,7 @@ public class AddNewTaskToTaskListService implements AddNewTaskToTaskListUseCase //the core and then the integration event in the application layer. if (newTask != null) { NewTaskAddedEvent newTaskAdded = new NewTaskAddedEvent(newTask.getTaskName().getValue(), - taskList.getTaskListName().getValue()); + taskList.getTaskListName().getValue(), newTask.getTaskId().getValue(), newTask.getTaskType().getValue()); newTaskAddedEventPort.publishNewTaskAddedEvent(newTaskAdded); } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/CompleteTaskService.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/CompleteTaskService.java new file mode 100644 index 0000000..bade832 --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/CompleteTaskService.java @@ -0,0 +1,35 @@ +package ch.unisg.tapastasks.tasks.application.service; + +import ch.unisg.tapastasks.tasks.application.port.in.CompleteTaskCommand; +import ch.unisg.tapastasks.tasks.application.port.in.CompleteTaskUseCase; +import ch.unisg.tapastasks.tasks.domain.Task; +import ch.unisg.tapastasks.tasks.domain.Task.*; +import ch.unisg.tapastasks.tasks.domain.TaskList; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import javax.transaction.Transactional; +import java.util.Optional; + +@RequiredArgsConstructor +@Component +@Transactional +public class CompleteTaskService implements CompleteTaskUseCase { + + @Override + public Task completeTask(CompleteTaskCommand command){ + // TODO Retrieve the task based on ID + TaskList taskList = TaskList.getTapasTaskList(); + Optional updatedTask = taskList.retrieveTaskById(command.getTaskId()); + + // TODO Update the status and result (and save?) + Task newTask = updatedTask.get(); + newTask.taskResult = new TaskResult(command.getTaskResult().getValue()); + newTask.taskState = new TaskState(Task.State.EXECUTED); + + + // TODO return the updated task + return newTask; + } +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/DeleteTaskService.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/DeleteTaskService.java new file mode 100644 index 0000000..05d1da5 --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/DeleteTaskService.java @@ -0,0 +1,25 @@ +package ch.unisg.tapastasks.tasks.application.service; + + +import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskCommand; +import ch.unisg.tapastasks.tasks.application.port.in.DeleteTaskUseCase; +import ch.unisg.tapastasks.tasks.domain.Task; +import ch.unisg.tapastasks.tasks.domain.TaskList; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import javax.transaction.Transactional; +import java.util.Optional; + +@RequiredArgsConstructor +@Component +@Transactional +public class DeleteTaskService implements DeleteTaskUseCase { + + @Override + public Optional deleteTask(DeleteTaskCommand command){ + TaskList taskList = TaskList.getTapasTaskList(); + return taskList.deleteTaskById(command.getTaskId()); + + } +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/TaskAssignedService.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/TaskAssignedService.java new file mode 100644 index 0000000..baa6059 --- /dev/null +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/application/service/TaskAssignedService.java @@ -0,0 +1,31 @@ +package ch.unisg.tapastasks.tasks.application.service; + +import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedCommand; +import ch.unisg.tapastasks.tasks.application.port.in.TaskAssignedUseCase; +import ch.unisg.tapastasks.tasks.domain.Task; +import ch.unisg.tapastasks.tasks.domain.Task.*; +import ch.unisg.tapastasks.tasks.domain.TaskList; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import javax.transaction.Transactional; +import java.util.Optional; + +@RequiredArgsConstructor +@Component +@Transactional +public class TaskAssignedService implements TaskAssignedUseCase { + + @Override + public Task assignTask(TaskAssignedCommand command) { + // retrieve the task based on ID + TaskList taskList = TaskList.getTapasTaskList(); + Optional task = taskList.retrieveTaskById(command.getTaskId()); + + // update the status to assigned + Task updatedTask = task.get(); + updatedTask.taskState = new TaskState(State.ASSIGNED); + + return updatedTask; + } +} diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/NewTaskAddedEvent.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/NewTaskAddedEvent.java index 32f5966..a4703f2 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/NewTaskAddedEvent.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/NewTaskAddedEvent.java @@ -4,9 +4,14 @@ package ch.unisg.tapastasks.tasks.domain; public class NewTaskAddedEvent { public String taskName; public String taskListName; + public String taskId; + public String taskType; - public NewTaskAddedEvent(String taskName, String taskListName) { + public NewTaskAddedEvent(String taskName, String taskListName, String taskId, String taskType) { this.taskName = taskName; this.taskListName = taskListName; + this.taskId = taskId; + this.taskType = taskType; } + } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/Task.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/Task.java index 0dcafc3..3decd1f 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/Task.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/Task.java @@ -22,13 +22,18 @@ public class Task { private final TaskType taskType; @Getter - private TaskState taskState; + public TaskState taskState; // had to make public for CompleteTaskService + + @Getter + public TaskResult taskResult; // same as above + public Task(TaskName taskName, TaskType taskType) { this.taskName = taskName; this.taskType = taskType; this.taskState = new TaskState(State.OPEN); this.taskId = new TaskId(UUID.randomUUID().toString()); + this.taskResult = new TaskResult(""); } protected static Task createTaskWithNameAndType(TaskName name, TaskType type) { @@ -56,4 +61,9 @@ public class Task { public static class TaskType { private String value; } + + @Value + public static class TaskResult{ + private String value; + } } diff --git a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/TaskList.java b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/TaskList.java index 2b90da5..ccdc59a 100644 --- a/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/TaskList.java +++ b/tapas-tasks/src/main/java/ch/unisg/tapastasks/tasks/domain/TaskList.java @@ -20,8 +20,7 @@ public class TaskList { //Note: We do not care about the management of task lists, there is only one within this service //--> using the Singleton pattern here to make lives easy; we will later load it from a repo - //TODO change "tutors" to your group name ("groupx") - private static final TaskList taskList = new TaskList(new TaskListName("tapas-tasks-tutors")); + private static final TaskList taskList = new TaskList(new TaskListName("tapas-tasks-group1")); private TaskList(TaskListName taskListName) { this.taskListName = taskListName; @@ -55,6 +54,17 @@ public class TaskList { return Optional.empty(); } + public Optional deleteTaskById(Task.TaskId id) { + for (Task task: listOfTasks.value){ + if(task.getTaskId().getValue().equalsIgnoreCase(id.getValue())){ + listOfTasks.value.remove(task); + return Optional.of(task); + } + } + + return Optional.empty(); + } + @Value public static class TaskListName { private String value;