저번 포스팅에서 Todo 업데이트 구현을 해보았는데요, description의 내용이 로딩은 잘 되었으나 내용을 바꿔서 저장을 하면 저장이 안되었을 겁니다. 이번에는 저장이 안되는 문제를 고쳐보겠습니다.
TodoService에 할 일을 저장하는 updateTodo()를 추가하겠습니다.
좋은 코드는 아니지만 간단하게 객체를 지우고 다시 추가하는 방식을 사용하겠습니다.
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@Service
public class TodoService {
private static List<Todo> todos = new ArrayList<>();
private static int todosCount = 0;
static {
todos.add(new Todo(++todosCount, "test1", "Get AWS Certified",
LocalDate.now().plusYears(1), false));
todos.add(new Todo(++todosCount, "test1", "Learn DevOps",
LocalDate.now().plusYears(2), false));
todos.add(new Todo(++todosCount, "test1", "Learn Full Stack Development",
LocalDate.now().plusYears(3), false));
}
public List<Todo> findByUsername(String username) {
return todos;
}
public void addTodo(String username, String description, LocalDate targetDate, boolean done) {
Todo todo = new Todo(++todosCount, username, description, targetDate, done);
todos.add(todo);
}
public void deleteById(int id) {
todos.removeIf(todo -> todo.getId() == id);
}
public Todo findById(int id) {
Todo findTodo = todos.stream().filter(todo -> todo.getId() == id).findFirst().get();
return findTodo;
}
public void updateTodo(Todo todo) {
deleteById(todo.getId());
todos.add(todo);
}
}
TodoController를 수정하겠습니다.
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import java.time.LocalDate;
import java.util.List;
@Controller
@SessionAttributes("name")
public class TodoController {
private TodoService todoService;
public TodoController(TodoService todoService) {
this.todoService = todoService;
}
//list-todos
@RequestMapping("/list-todos")
public String listAllTodos(ModelMap model) {
List<Todo> todos = todoService.findByUsername("test1");
model.addAttribute("todos", todos);
return "listTodos";
}
@RequestMapping(value = "/add-todo", method = RequestMethod.GET)
public String showNewTodoPage(ModelMap model) {
String username = (String) model.get("name");
Todo todo = new Todo(0, username, "", LocalDate.now().plusYears(1), false);
model.put("todo", todo);
return "todo";
}
@RequestMapping(value = "/add-todo", method = RequestMethod.POST)
public String addNewTodo(ModelMap model, @Valid Todo todo, BindingResult result) {
if (result.hasErrors()) {
return "todo";
}
String username = (String) model.get("name");
todoService.addTodo(username, todo.getDescription(), LocalDate.now().plusYears(1), false);
return "redirect:list-todos";
}
@RequestMapping("/delete-todo")
public String deleteTodo(@RequestParam int id) {
//Delete todo
todoService.deleteById(id);
return "redirect:list-todos";
}
@RequestMapping(value = "/update-todo", method = RequestMethod.GET)
public String showUpdateTodoPage(@RequestParam int id, ModelMap model) {
Todo todo = todoService.findById(id);
model.addAttribute("todo", todo);
return "todo";
}
@RequestMapping(value = "/update-todo", method = RequestMethod.POST)
public String updateTodo(ModelMap model, @Valid Todo todo, BindingResult result) {
if (result.hasErrors()) {
return "todo";
}
String username = (String) model.get("name");
todo.setUsername(username);
todoService.updateTodo(todo);
return "redirect:list-todos";
}
}
updateTodo()를 통해서 할 일 목록이 수정되는 걸 확인할 수 있습니다.