-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodoService.java
More file actions
55 lines (38 loc) · 1.36 KB
/
TodoService.java
File metadata and controls
55 lines (38 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.example.spring_todo.Service;
import com.example.spring_todo.Model.Todo;
import com.example.spring_todo.Repository.TodoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class TodoService {
private final TodoRepository todoRepository;
@Autowired
public TodoService(TodoRepository todoRepository) {
this.todoRepository = todoRepository;
}
// returns all data from table
public List<Todo> findAll() {
return todoRepository.findAllByOrderByCreatedAtDesc(); // Use the sorted method
}
// save todos
public Todo save(Todo todo) {
return todoRepository.save(todo);
}
// single todoo
public Optional<Todo> findById(Long id) {
return todoRepository.findById(id);
}
// delete single todoo
public void deleteById(Long id) {
todoRepository.deleteById(id);
}
// update todoo
public Todo update(Long id, String title, boolean completed) {
Todo todo = todoRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid todo id: " + id));
todo.setTitle(title);
todo.setCompleted(completed);
return todoRepository.save(todo);
}
}