-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathToDoController.java
75 lines (57 loc) · 1.98 KB
/
ToDoController.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.todo.list.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.todo.list.entity.ToDo;
import com.todo.list.repository.ToDoRepository;
//@RestController
@Controller
public class ToDoController {
@Autowired
private ToDoRepository toDoRepository;
@GetMapping("")
public String index() {
return "index";
}
@GetMapping("/logout")
public String logoutPage() {
return "logout";
}
@GetMapping("/todos")
public String todos(Model model){
model.addAttribute("todos", toDoRepository.findAll());
return "todos";
}
@PostMapping("/todo")
public String add(@RequestParam String todoItem,@RequestParam String status,Model model) {
ToDo toDo=new ToDo();
toDo.setTodoItem(todoItem);
toDo.setCompleted(status);
toDoRepository.save(toDo);
model.addAttribute("todos", toDoRepository.findAll());
return "redirect:/todos";
}
@PostMapping("/todoDelete/{id}")
public String delete(@PathVariable long id, Model model){
toDoRepository.deleteById(id);
model.addAttribute("todos", toDoRepository.findAll());
return "redirect:/todos";
}
@PostMapping("/todoUpdate/{id}")
public String update(@PathVariable long id, Model model){
ToDo toDo = toDoRepository.findById(id).get();
if("Yes".equals(toDo.getCompleted())){
toDo.setCompleted("No");
} else {
toDo.setCompleted("Yes");
}
toDoRepository.save(toDo);
model.addAttribute("todos", toDoRepository.findAll());
return "redirect:/todos";
}
}