2
2
3
3
import com .cevher .ms .task .domain .Task ;
4
4
import com .cevher .ms .task .repository .TaskRepository ;
5
+ import com .cevher .ms .task .web .rest .DepartmentVM ;
6
+ import com .cevher .ms .task .web .rest .ResponseVM ;
5
7
import com .cevher .ms .task .web .rest .UserVM ;
6
8
import lombok .extern .slf4j .Slf4j ;
7
9
import org .springframework .beans .factory .annotation .Autowired ;
8
10
import org .springframework .stereotype .Service ;
11
+ import org .springframework .web .client .RestTemplate ;
9
12
10
13
import java .util .List ;
14
+ import java .util .Optional ;
11
15
12
16
@ Service
13
17
@ Slf4j
14
18
public class TaskService {
15
19
@ Autowired
16
20
private TaskRepository taskRepository ;
21
+ @ Autowired
22
+ RestTemplate restTemplate ;
17
23
18
24
public List <Task > findAllTask () {
19
- log .info ("findAllTask method of TaskService" );
20
- return taskRepository .findAll ();
25
+ log .info ("findAllTask method of TaskService" );
26
+ return taskRepository .findAll ();
21
27
}
22
28
23
29
public Task saveTask (Task task ) {
@@ -30,9 +36,25 @@ public Task findByTaskId(Long id) {
30
36
return taskRepository .findById (id ).orElse (new Task ());
31
37
}
32
38
33
- public Task findByTaskIdWithUserAndDepartment (Long id ) {
34
- log .info ("findAllTask method of TaskService" );
35
-
36
- return taskRepository .findById (id ).orElse (new Task ());
39
+ public ResponseVM findByTaskIdWithUserAndDepartment (Long id ) {
40
+ log .info ("findByTaskIdWithUserAndDepartment method of TaskService" );
41
+
42
+ Optional <Task > task = taskRepository .findById (id );
43
+ if (task .isEmpty ()) {
44
+ log .warn ("findByTaskIdWithUserAndDepartment method of TaskService, task is empty" );
45
+ return null ;
46
+ }
47
+
48
+ // TODO : not for production.
49
+ // TODO : We can use Message Broker for example Apache Kafka
50
+ UserVM user = restTemplate .getForObject (
51
+ "http://user-service/users/" + task .get ().getAssignedUserId ()
52
+ , UserVM .class );
53
+
54
+ DepartmentVM department = restTemplate .getForObject (
55
+ "http://department-service/departments/" + task .get ().getDepartmentId ()
56
+ , DepartmentVM .class );
57
+ ResponseVM responseVM = new ResponseVM (task .get (), user , department );
58
+ return responseVM ;
37
59
}
38
60
}
0 commit comments