This commit is contained in:
roma-dxunvrs
2026-10-03 22:09:58 +03:00
commit 8d4b15dbcf
33 changed files with 1655 additions and 0 deletions
@@ -0,0 +1,55 @@
package ru.dxunvrs.todo.service;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.dxunvrs.todo.dto.TaskCreateRequest;
import ru.dxunvrs.todo.dto.TaskResponse;
import ru.dxunvrs.todo.dto.TaskUpdateRequest;
import ru.dxunvrs.todo.entity.Task;
import ru.dxunvrs.todo.exception.TaskNotFoundException;
import ru.dxunvrs.todo.mapper.TaskMapper;
import ru.dxunvrs.todo.repository.TaskRepository;
@Service
@RequiredArgsConstructor
public class TaskServiceImpl implements TaskService {
private final TaskRepository taskRepository;
private final TaskMapper taskMapper;
@Override
@Transactional(readOnly = true)
public TaskResponse getById(Long id) {
Task task = taskRepository.findById(id)
.orElseThrow(() -> new TaskNotFoundException("Task not found with id: " + id));
return taskMapper.toResponse(task);
}
@Override
@Transactional
public TaskResponse create(TaskCreateRequest request) {
Task task = taskMapper.toEntity(request);
Task saved = taskRepository.save(task);
return taskMapper.toResponse(saved);
}
@Override
@Transactional
public TaskResponse update(Long id, TaskUpdateRequest request) {
Task task = taskRepository.findById(id)
.orElseThrow(() -> new TaskNotFoundException("Task not found with id: " + id));
taskMapper.updateEntity(task, request);
Task updated = taskRepository.save(task);
return taskMapper.toResponse(updated);
}
@Override
@Transactional
public void delete(Long id) {
if (!taskRepository.existsById(id)) {
throw new TaskNotFoundException("Task not found with id: " + id);
}
taskRepository.deleteById(id);
}
}