View Javadoc

1   package org.lanyonm.playground.web.controller;
2   
3   import java.util.Date;
4   
5   import org.lanyonm.playground.domain.Todo;
6   import org.lanyonm.playground.service.TodoService;
7   import org.lanyonm.playground.web.command.TodoCommand;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  import org.springframework.beans.factory.annotation.Autowired;
11  import org.springframework.stereotype.Controller;
12  import org.springframework.ui.Model;
13  import org.springframework.validation.BindingResult;
14  import org.springframework.web.bind.annotation.ModelAttribute;
15  import org.springframework.web.bind.annotation.PathVariable;
16  import org.springframework.web.bind.annotation.RequestMapping;
17  import org.springframework.web.bind.annotation.RequestMethod;
18  
19  /**
20   * @author lanyonm
21   */
22  @Controller
23  public class TodoController {
24  
25  	@Autowired
26  	private TodoService todoService;
27  
28  	private static final Logger log = LoggerFactory.getLogger(TodoController.class);
29  
30  	/**
31  	 * @param model
32  	 * @return
33  	 */
34  	@RequestMapping(value = "/todo/", method = RequestMethod.GET)
35  	public String index(Model model) {
36  		log.debug("for logging's sake");
37  		model.addAttribute("todoList", todoService.getAllTodos());
38  		return "todo/index";
39  	}
40  
41  	/**
42  	 * @param id
43  	 * @param model
44  	 * @return
45  	 */
46  	@RequestMapping(value = "/todo/{id}/edit", method = RequestMethod.GET)
47  	public String todoAddEditShow(@PathVariable("id") final int id, Model model) {
48  		Todo todo = todoService.getTodo(id);
49  		if (todo == null && id > 0) {
50  			// TODO message something
51  			return "redirect:/todo/";
52  		} else if (id == 0) {
53  			todo = new Todo();
54  		}
55  		model.addAttribute("todo", todo);
56  		return "todo/addEdit";
57  	}
58  
59  	/**
60  	 * @param id
61  	 * @param model
62  	 * @param todoCommand
63  	 * @param bindingResult
64  	 * @return
65  	 */
66  	@RequestMapping(value = "/todo/{id}/edit", method = RequestMethod.POST)
67  	public String todoAddEditSave(@PathVariable("id") final int id, Model model, @ModelAttribute("todo") TodoCommand todoCommand, BindingResult bindingResult) {
68  		Todo todo = new Todo();
69  		if (id == 0) {
70  			todo.setTitle(todoCommand.getTitle());
71  			todo.setDateCreated(new Date());
72  			todoService.saveTodo(todo);
73  			// TODO verify the result of saveTodo()
74  		} else {
75  			todo = todoService.getTodo(id);
76  			if (todo != null) {
77  				todo.setTitle(todoCommand.getTitle());
78  				todo.setDateModified(new Date());
79  				todoService.saveTodo(todo);
80  				// TODO verify the result of saveTodo()
81  			} else {
82  				// TODO message something
83  			}
84  		}
85  		return "redirect:/todo/";
86  	}
87  
88  	@RequestMapping(value = "/todo/{id}/delete", method = RequestMethod.GET)
89  	public String todoDelete(@PathVariable("id") final int id, Model model) {
90  		Todo todo = todoService.getTodo(id);
91  		if (todoService.deleteTodo(todo)) {
92  			// TODO send a success message!
93  		} else {
94  			// TODO send a failture message  >:|
95  		}
96  		return "redirect:/todo/";
97  	}
98  }