Files
java-ai-slop/src/main/java/ru/dxunvrs/todo/service/TaskServiceImpl.java
T
roma-dxunvrs 8d4b15dbcf Pizdec
2026-10-03 22:09:58 +03:00

56 lines
1.8 KiB
Java

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);
}
}