001package org.lanyonm.playground.web.controller;
002
003import java.util.Date;
004
005import org.lanyonm.playground.domain.Todo;
006import org.lanyonm.playground.service.TodoService;
007import org.lanyonm.playground.web.command.TodoCommand;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010import org.springframework.beans.factory.annotation.Autowired;
011import org.springframework.stereotype.Controller;
012import org.springframework.ui.Model;
013import org.springframework.validation.BindingResult;
014import org.springframework.web.bind.annotation.ModelAttribute;
015import org.springframework.web.bind.annotation.PathVariable;
016import org.springframework.web.bind.annotation.RequestMapping;
017import org.springframework.web.bind.annotation.RequestMethod;
018
019/**
020 * @author lanyonm
021 */
022@Controller
023public class TodoController {
024
025        @Autowired
026        private TodoService todoService;
027
028        private static final Logger log = LoggerFactory.getLogger(TodoController.class);
029
030        /**
031         * @param model
032         * @return
033         */
034        @RequestMapping(value = "/todo/", method = RequestMethod.GET)
035        public String index(Model model) {
036                log.debug("for logging's sake");
037                model.addAttribute("todoList", todoService.getAllTodos());
038                return "todo/index";
039        }
040
041        /**
042         * @param id
043         * @param model
044         * @return
045         */
046        @RequestMapping(value = "/todo/{id}/edit", method = RequestMethod.GET)
047        public String todoAddEditShow(@PathVariable("id") final int id, Model model) {
048                Todo todo = todoService.getTodo(id);
049                if (todo == null && id > 0) {
050                        // TODO message something
051                        return "redirect:/todo/";
052                } else if (id == 0) {
053                        todo = new Todo();
054                }
055                model.addAttribute("todo", todo);
056                return "todo/addEdit";
057        }
058
059        /**
060         * @param id
061         * @param model
062         * @param todoCommand
063         * @param bindingResult
064         * @return
065         */
066        @RequestMapping(value = "/todo/{id}/edit", method = RequestMethod.POST)
067        public String todoAddEditSave(@PathVariable("id") final int id, Model model, @ModelAttribute("todo") TodoCommand todoCommand, BindingResult bindingResult) {
068                Todo todo = new Todo();
069                if (id == 0) {
070                        todo.setTitle(todoCommand.getTitle());
071                        todo.setDateCreated(new Date());
072                        todoService.saveTodo(todo);
073                        // TODO verify the result of saveTodo()
074                } else {
075                        todo = todoService.getTodo(id);
076                        if (todo != null) {
077                                todo.setTitle(todoCommand.getTitle());
078                                todo.setDateModified(new Date());
079                                todoService.saveTodo(todo);
080                                // TODO verify the result of saveTodo()
081                        } else {
082                                // TODO message something
083                        }
084                }
085                return "redirect:/todo/";
086        }
087
088        @RequestMapping(value = "/todo/{id}/delete", method = RequestMethod.GET)
089        public String todoDelete(@PathVariable("id") final int id, Model model) {
090                Todo todo = todoService.getTodo(id);
091                if (todoService.deleteTodo(todo)) {
092                        // TODO send a success message!
093                } else {
094                        // TODO send a failture message  >:|
095                }
096                return "redirect:/todo/";
097        }
098}