Skip to content

Commit b4296c3

Browse files
committed
Personalized methods
1 parent 6064d54 commit b4296c3

File tree

9 files changed

+112
-5
lines changed

9 files changed

+112
-5
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import com.example.rest.DTO.ProjectDTO;
44
import com.example.rest.entities.Project;
5+
import com.example.rest.entities.Task;
6+
7+
import java.util.List;
8+
import java.util.Map;
59

610
public interface ProjectService {
711

@@ -16,4 +20,8 @@ public interface ProjectService {
1620

1721
// para obtener projecto por id
1822
Project getProjectById(Long id);
23+
24+
// para obtener todas las tareas de un proyecto por su estado
25+
Map<String, Object> getAllProjectTasks(Long projectId);
26+
1927
}

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,30 @@
33
import com.example.rest.DTO.ProjectDTO;
44
import com.example.rest.Exceptions.ApiRequestException;
55
import com.example.rest.entities.Project;
6+
import com.example.rest.entities.Task;
67
import com.example.rest.entities.projectStatus;
78
import com.example.rest.repositories.ProjectRepository;
9+
import com.example.rest.repositories.TaskRepository;
810
import org.springframework.beans.factory.annotation.Autowired;
911
import org.springframework.stereotype.Service;
1012

1113
import java.time.LocalDateTime;
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
1217
import java.util.Optional;
18+
import java.util.stream.Collectors;
1319

1420
@Service
1521
public class ProjectServiceImpl implements ProjectService {
1622

1723
private final ProjectRepository projectRepository;
24+
private final TaskRepository taskRepository;
1825

1926
@Autowired
20-
public ProjectServiceImpl(ProjectRepository projectRepository) {
27+
public ProjectServiceImpl(ProjectRepository projectRepository, TaskRepository taskRepository) {
2128
this.projectRepository = projectRepository;
29+
this.taskRepository = taskRepository;
2230
}
2331

2432
@Override
@@ -71,5 +79,26 @@ public Project getProjectById(Long id) {
7179
}
7280
}
7381

82+
83+
public Map<String, Object> getAllProjectTasks(Long projectId) {
84+
Project project = projectRepository.findById(projectId)
85+
.orElseThrow(() -> new ApiRequestException("Project not found with id: " + projectId));
86+
87+
List<Task> tasks = taskRepository.findByProjectId(projectId);
88+
89+
Map<String, List<Task>> board = tasks.stream()
90+
.collect(Collectors.groupingBy(task -> task.getTaskStatus().toString()));
91+
92+
return Map.of(
93+
"project", Map.of(
94+
"id", project.getId(),
95+
"name", project.getName()
96+
),
97+
"board", board.entrySet().stream()
98+
.map(entry -> Map.of("status", entry.getKey(), "tasks", entry.getValue()))
99+
.collect(Collectors.toList())
100+
);
101+
}
102+
74103
}
75104

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
package com.example.rest.Services;
22

33
import com.example.rest.DTO.TaskDTO;
4+
import com.example.rest.Exceptions.ApiRequestException;
45
import com.example.rest.entities.Task;
6+
import com.example.rest.entities.TaskStatus;
57

68
public interface TaskService {
79

810

911
// para crea tareas
10-
1112
Task createTask(long project_id, TaskDTO taskDTO);
1213

1314
// para eliminar tareas
14-
1515
void deleteTask(Long task_id);
1616

1717
// para obtener tareas
18-
1918
Task getTaskById(Long task_id);
2019

20+
// para actualizar status
21+
Task updateTaskStatus(Long taskId, String newStatus) throws ApiRequestException;
22+
23+
// Consultar tareas por fecha de vencimiento
24+
25+
// Task getTaskByDueDate (dueDate ) {
26+
//
27+
// }
28+
29+
30+
2131
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,37 @@ public Task getTaskById(Long task_id) {
6565
throw new ApiRequestException("La tarea no existe");
6666
}
6767
}
68+
69+
@Override
70+
public Task updateTaskStatus(Long taskId, String newStatus) throws ApiRequestException {
71+
Task task = taskRepository.findById(taskId)
72+
.orElseThrow(() -> new ApiRequestException("Task not found with id: " + taskId));
73+
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+
}
82+
83+
if (!isValidStatusTransition(currentStatus, updatedStatus)) {
84+
throw new ApiRequestException("no es posible asignar al estado " + updatedStatus +
85+
" una tarea con estado " + currentStatus);
86+
}
87+
88+
task.setTaskStatus(String.valueOf(updatedStatus));
89+
return taskRepository.save(task);
90+
}
91+
92+
private boolean isValidStatusTransition(TaskStatus currentStatus, TaskStatus newStatus) {
93+
// validar los estados (Preguntar)
94+
95+
return (currentStatus == TaskStatus.TODO && newStatus == TaskStatus.IN_PROGRESS) ||
96+
(currentStatus == TaskStatus.IN_PROGRESS && (newStatus == TaskStatus.BLOCKED || newStatus == TaskStatus.DONE)) ||
97+
(currentStatus == TaskStatus.BLOCKED && (newStatus == TaskStatus.IN_PROGRESS || newStatus == TaskStatus.DONE));
98+
}
99+
100+
68101
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.web.bind.annotation.*;
99

1010
import java.net.URI;
11+
import java.util.Map;
1112

1213

1314
@RestController
@@ -54,4 +55,11 @@ public ResponseEntity<Project> getProject(@PathVariable("id") Long id) {
5455
return ResponseEntity.ok(project);
5556
}
5657

58+
// GET -> /v1/projects/{id}/board Obtener todas las tareas de un proyecto
59+
@GetMapping("/{id}/board")
60+
public ResponseEntity<Map<String, Object>> getAllProjectTasks(@PathVariable("id") Long projectId) {
61+
Map<String, Object> response = projectService.getAllProjectTasks(projectId);
62+
return ResponseEntity.ok(response);
63+
}
64+
5765
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.example.rest.controllers;
22

33
import com.example.rest.DTO.TaskDTO;
4+
import com.example.rest.Exceptions.ApiRequestException;
45
import com.example.rest.Services.TaskServiceImpl;
56
import com.example.rest.entities.Task;
67
import org.springframework.http.ResponseEntity;
78
import org.springframework.web.bind.annotation.*;
89

910
import java.net.URI;
11+
import java.util.Map;
1012

1113
@RestController
1214
@RequestMapping("/v1/task/")
@@ -40,4 +42,16 @@ public ResponseEntity<Task> getTask(@PathVariable("id") Long task_id){
4042
return ResponseEntity.ok(task);
4143
}
4244

45+
// PATCH // Actualizar Status
46+
@PatchMapping("/{id}")
47+
public ResponseEntity<Object> updateTaskStatus(@PathVariable("id") Long taskId, @RequestBody Map<String, String> requestBody) {
48+
String newStatus = requestBody.get("status");
49+
try {
50+
Task updatedTask = taskService.updateTaskStatus(taskId, newStatus);
51+
return ResponseEntity.ok(updatedTask);
52+
} catch (ApiRequestException e) {
53+
return ResponseEntity.badRequest().body(Map.of("mensaje", e.getMessage()));
54+
}
55+
}
56+
4357
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class Task {
2626
private String description;
2727

2828
@Column(name = "status")
29+
@Enumerated(EnumType.STRING)
2930
private String taskStatus;
3031

3132
@Column(name = "tipo")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.example.rest.entities;
22

33
public enum TaskStatus {
4-
TODO, INPROGRESS, BLOCKED, DONE
4+
TODO, IN_PROGRESS, BLOCKED, DONE
55
}

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

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

7+
import java.util.List;
8+
79
@Repository
810
public interface TaskRepository extends JpaRepository<Task, Long> {
911

1012
Task findTaskById(Long id);
13+
List<Task> findByProjectId(Long projectId);
14+
1115
}

0 commit comments

Comments
 (0)