Skip to content

Commit dec30f7

Browse files
committed
Updated method UpdateTaskStatus
1 parent b4296c3 commit dec30f7

File tree

9 files changed

+56
-29
lines changed

9 files changed

+56
-29
lines changed

src/main/java/com/example/rest/Services/ProjectService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ public interface ProjectService {
2424
// para obtener todas las tareas de un proyecto por su estado
2525
Map<String, Object> getAllProjectTasks(Long projectId);
2626

27+
// para obtener todas las tareas por fecha de vencimiento
28+
List<Task> getDueTasksByProjectId(Long projectId);
29+
2730
}

src/main/java/com/example/rest/Services/ProjectServiceImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.beans.factory.annotation.Autowired;
1111
import org.springframework.stereotype.Service;
1212

13+
import java.time.LocalDate;
1314
import java.time.LocalDateTime;
1415
import java.util.HashMap;
1516
import java.util.List;
@@ -100,5 +101,13 @@ public Map<String, Object> getAllProjectTasks(Long projectId) {
100101
);
101102
}
102103

104+
@Override
105+
public List<Task> getDueTasksByProjectId(Long projectId) {
106+
LocalDate currentDate = LocalDate.now();
107+
return taskRepository.findByProjectIdAndDueDateBefore(projectId, currentDate);
108+
}
109+
110+
111+
103112
}
104113

src/main/java/com/example/rest/Services/TaskService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public interface TaskService {
1818
Task getTaskById(Long task_id);
1919

2020
// para actualizar status
21-
Task updateTaskStatus(Long taskId, String newStatus) throws ApiRequestException;
21+
Task updateTaskStatus(Long taskId, TaskStatus newStatus) throws ApiRequestException;
2222

2323
// Consultar tareas por fecha de vencimiento
2424

src/main/java/com/example/rest/Services/TaskServiceImpl.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public Task createTask(long project_id, TaskDTO taskDTO) {
3535
Task newTask = new Task();
3636
newTask.setName(taskDTO.getName());
3737
newTask.setDescription(taskDTO.getDescription());
38-
newTask.setTaskStatus(TaskStatus.TODO.toString());
38+
newTask.setTaskStatus(TaskStatus.TODO);
3939
newTask.setType(taskDTO.getType());
4040
newTask.setStartDate(taskDTO.getStartDate());
4141
newTask.setDueDate(taskDTO.getDueDate());
@@ -66,31 +66,22 @@ public Task getTaskById(Long task_id) {
6666
}
6767
}
6868

69-
@Override
70-
public Task updateTaskStatus(Long taskId, String newStatus) throws ApiRequestException {
69+
public Task updateTaskStatus(Long taskId, TaskStatus newStatus) throws ApiRequestException {
7170
Task task = taskRepository.findById(taskId)
7271
.orElseThrow(() -> new ApiRequestException("Task not found with id: " + taskId));
7372

74-
TaskStatus currentStatus = TaskStatus.valueOf(task.getTaskStatus());
75-
TaskStatus updatedStatus;
76-
77-
try {
78-
updatedStatus = TaskStatus.valueOf(newStatus.toUpperCase());
79-
} catch (IllegalArgumentException e) {
80-
throw new ApiRequestException("el estado " + newStatus + " no es válido");
81-
}
73+
TaskStatus currentStatus = task.getTaskStatus();
8274

83-
if (!isValidStatusTransition(currentStatus, updatedStatus)) {
84-
throw new ApiRequestException("no es posible asignar al estado " + updatedStatus +
85-
" una tarea con estado " + currentStatus);
75+
if (!isValidStatusTransition(currentStatus, newStatus)) {
76+
throw new ApiRequestException("no es posible asignar al estado " + newStatus + " una tarea con estado " + currentStatus);
8677
}
8778

88-
task.setTaskStatus(String.valueOf(updatedStatus));
79+
task.setTaskStatus(newStatus);
8980
return taskRepository.save(task);
9081
}
9182

9283
private boolean isValidStatusTransition(TaskStatus currentStatus, TaskStatus newStatus) {
93-
// validar los estados (Preguntar)
84+
// validar los estados (tenemos dudas)
9485

9586
return (currentStatus == TaskStatus.TODO && newStatus == TaskStatus.IN_PROGRESS) ||
9687
(currentStatus == TaskStatus.IN_PROGRESS && (newStatus == TaskStatus.BLOCKED || newStatus == TaskStatus.DONE)) ||

src/main/java/com/example/rest/controllers/ProjectController.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import com.example.rest.DTO.ProjectDTO;
44
import com.example.rest.Services.ProjectServiceImpl;
55
import com.example.rest.entities.Project;
6+
import com.example.rest.entities.Task;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.http.ResponseEntity;
89
import org.springframework.web.bind.annotation.*;
910

1011
import java.net.URI;
12+
import java.util.List;
1113
import java.util.Map;
1214

1315

@@ -24,15 +26,15 @@ public ProjectController(ProjectServiceImpl projectService) {
2426
}
2527

2628

27-
//POST -> /v1/projects crear un Project
29+
//POST -> /v1/projects // crear un Project
2830
@PostMapping("/")
2931
public ResponseEntity<Project> createProject(@RequestBody ProjectDTO projectDTO) {
3032
Project projectoCreado = projectService.createProject(projectDTO);
3133
return ResponseEntity.created(URI.create("/v1/projects" + projectoCreado.getId()))
3234
.body(projectoCreado);
3335
}
3436

35-
// PUT -> /v1/projects/{id} editar un Project
37+
// PUT -> /v1/projects/{id} // editar un Project
3638
@PutMapping("/{id}")
3739
public ResponseEntity<Project> editProject(@PathVariable("id") Long id,
3840
@RequestBody Project project) {
@@ -41,25 +43,33 @@ public ResponseEntity<Project> editProject(@PathVariable("id") Long id,
4143
.body(proyectoEditado);
4244
}
4345

44-
// DELETE -> /v1/projects/{id} eliminar un Project
46+
// DELETE -> /v1/projects/{id} // eliminar un Project
4547
public ResponseEntity<Void> eliminarProjecto(@PathVariable("id") Long id) {
4648
projectService.deleteProject(id);
4749
return ResponseEntity.noContent()
4850
.build();
4951
}
5052

51-
// GET -> /v1/projects/{id} obtener un Project por id
53+
// GET -> /v1/projects/{id} // obtener un Project por id
5254
@GetMapping("/{id}")
5355
public ResponseEntity<Project> getProject(@PathVariable("id") Long id) {
5456
Project project = projectService.getProjectById(id);
5557
return ResponseEntity.ok(project);
5658
}
5759

58-
// GET -> /v1/projects/{id}/board Obtener todas las tareas de un proyecto
60+
// GET -> /v1/projects/{id}/board // Obtener todas las tareas de un proyecto
5961
@GetMapping("/{id}/board")
6062
public ResponseEntity<Map<String, Object>> getAllProjectTasks(@PathVariable("id") Long projectId) {
6163
Map<String, Object> response = projectService.getAllProjectTasks(projectId);
6264
return ResponseEntity.ok(response);
6365
}
6466

67+
// GET /v1/projects/{id}/due-task // Obtener todas las tareas de un proyecto por fecha de vencimiento
68+
69+
@GetMapping("/{id}/due-task")
70+
public ResponseEntity<List<Task>> getDueTasksByProjectId(@PathVariable("id") Long projectId) {
71+
List<Task> dueTasks = projectService.getDueTasksByProjectId(projectId);
72+
return ResponseEntity.ok(dueTasks);
73+
}
74+
6575
}

src/main/java/com/example/rest/controllers/TaskController.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.example.rest.Exceptions.ApiRequestException;
55
import com.example.rest.Services.TaskServiceImpl;
66
import com.example.rest.entities.Task;
7+
import com.example.rest.entities.TaskStatus;
78
import org.springframework.http.ResponseEntity;
89
import org.springframework.web.bind.annotation.*;
910

@@ -45,13 +46,20 @@ public ResponseEntity<Task> getTask(@PathVariable("id") Long task_id){
4546
// PATCH // Actualizar Status
4647
@PatchMapping("/{id}")
4748
public ResponseEntity<Object> updateTaskStatus(@PathVariable("id") Long taskId, @RequestBody Map<String, String> requestBody) {
48-
String newStatus = requestBody.get("status");
49+
String newStatusString = requestBody.get("status");
50+
51+
TaskStatus newStatus;
52+
try {
53+
newStatus = TaskStatus.valueOf(newStatusString.toUpperCase());
54+
} catch (IllegalArgumentException e) {
55+
return ResponseEntity.badRequest().body(Map.of("mensaje", "el estado " + newStatusString + " no es válido"));
56+
}
57+
4958
try {
5059
Task updatedTask = taskService.updateTaskStatus(taskId, newStatus);
5160
return ResponseEntity.ok(updatedTask);
5261
} catch (ApiRequestException e) {
5362
return ResponseEntity.badRequest().body(Map.of("mensaje", e.getMessage()));
5463
}
5564
}
56-
5765
}

src/main/java/com/example/rest/entities/Task.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public class Task {
2525
@Column(name = "descripcion")
2626
private String description;
2727

28-
@Column(name = "status")
28+
@Column(name = "taskStatus")
2929
@Enumerated(EnumType.STRING)
30-
private String taskStatus;
30+
private TaskStatus taskStatus;
3131

3232
@Column(name = "tipo")
3333
private String type;
@@ -72,11 +72,11 @@ public void setDescription(String description) {
7272
this.description = description;
7373
}
7474

75-
public String getTaskStatus() {
75+
public TaskStatus getTaskStatus() {
7676
return taskStatus;
7777
}
7878

79-
public void setTaskStatus(String taskStatus) {
79+
public void setTaskStatus(TaskStatus taskStatus) {
8080
this.taskStatus = taskStatus;
8181
}
8282

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.example.rest.entities;
22

33
public enum TaskStatus {
4-
TODO, IN_PROGRESS, BLOCKED, DONE
4+
TODO,
5+
IN_PROGRESS,
6+
BLOCKED,
7+
DONE
58
}

src/main/java/com/example/rest/repositories/TaskRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import org.springframework.stereotype.Repository;
66

7+
import java.time.LocalDate;
78
import java.util.List;
89

910
@Repository
@@ -12,4 +13,6 @@ public interface TaskRepository extends JpaRepository<Task, Long> {
1213
Task findTaskById(Long id);
1314
List<Task> findByProjectId(Long projectId);
1415

16+
List<Task> findByProjectIdAndDueDateBefore(Long projectId, LocalDate currentDate);
17+
1518
}

0 commit comments

Comments
 (0)