View Javadoc
1   package fr.tiogars.chat.domains.task.services;
2   
3   import java.util.Optional;
4   
5   import org.springframework.stereotype.Service;
6   
7   import fr.tiogars.chat.domains.task.entities.TaskEntity;
8   import fr.tiogars.chat.domains.task.models.Task;
9   import fr.tiogars.chat.domains.task.repositories.TaskRepository;
10  
11  /** Retrieves a Task by its persistent identifier. */
12  @Service
13  @org.springframework.transaction.annotation.Transactional(readOnly = true)
14  public class TaskFindByIdService {
15  
16      private final TaskRepository repository;
17  
18      public TaskFindByIdService(TaskRepository repository) {
19          this.repository = repository;
20      }
21  
22      /**
23       * Finds a Task by identifier.
24       *
25       * @param id persistent identifier
26       * @return matching template, or an empty optional when none exists
27       */
28      public Optional<Task> findById(Long id) {
29          return repository.findById(id)
30                  .map(TaskFindByIdService::toRecord);
31      }
32  
33      private static Task toRecord(TaskEntity entity) {
34          return new Task(
35                  entity.getId(),
36                  entity.getCode(),
37                  entity.getLabel(),
38                  entity.getDescription(),
39                  entity.getCreatedDate(),
40                  entity.getLastModifiedDate(),
41                  entity.getCreatedBy(),
42                  entity.getLastModifiedBy(),
43                  entity.getStatus(), entity.getPriority(), entity.getDueDate(), entity.getAssignedTo(),
44                  entity.getTagIds(), entity.getNomenclatureId());
45      }
46  }