───────┬────────────────────────────────────────────────────────────────────────
       │ File: src/main//java/jp/ac/kobe_u/cs/itspecialist/todoapp/controller/ToDoController.java
───────┼────────────────────────────────────────────────────────────────────────
   1   │ package jp.ac.kobe_u.cs.itspecialist.todoapp.controller;
   2   │ 
   3   │ import java.util.List;
   4   │ 
   5   │ import org.springframework.beans.factory.annotation.Autowired;
   6   │ import org.springframework.stereotype.Controller;
   7   │ import org.springframework.ui.Model;
   8   │ import org.springframework.validation.BindingResult;
   9   │ import org.springframework.validation.annotation.Validated;
  10   │ import org.springframework.web.bind.annotation.*;
  11   │ 
  12   │ import jp.ac.kobe_u.cs.itspecialist.todoapp.dto.LoginForm;
  13   │ import jp.ac.kobe_u.cs.itspecialist.todoapp.dto.ToDoForm;
  14   │ import jp.ac.kobe_u.cs.itspecialist.todoapp.entity.Member;
  15   │ import jp.ac.kobe_u.cs.itspecialist.todoapp.entity.ToDo;
  16   │ import jp.ac.kobe_u.cs.itspecialist.todoapp.service.MemberService;
  17   │ import jp.ac.kobe_u.cs.itspecialist.todoapp.service.ToDoService;
  18   │ 
  19   │ @Controller
  20   │ public class ToDoController {
  21   │     @Autowired
  22   │     MemberService mService;
  23   │     @Autowired
  24   │     ToDoService tService;
  25   │ 
  26   │     /**
  27   │      * トップページ
  28   │      */
  29   │     @GetMapping("/")
  30   │     String showIndex(@ModelAttribute(name = "loginForm") LoginForm loginForm, Model model) {
  31   │         //model.addAttribute("loginForm", loginForm);
  32   │         return "index";
  33   │     }
  34   │ 
  35   │     /**
  36   │      * ログイン処理.midの存在確認をして,ユーザページにリダイレクト
  37   │      */
  38   │     @PostMapping("/login")
  39   │     String login(@Validated @ModelAttribute(name = "loginForm") LoginForm loginForm, BindingResult bindingResult,
  40   │             Model model) {
  41   │ 
  42   │         if (bindingResult.hasErrors()) { // バリデーション
  43   │             return showIndex(loginForm, model);
  44   │         }
  45   │ 
  46   │         Member m = mService.getMember(loginForm.getMid()); // 存在チェック
  47   │         return "redirect:/" + m.getMid() + "/todos";
  48   │     }
  49   │ 
  50   │     /**
  51   │      * ユーザのToDoリストのページ
  52   │      */
  53   │     @GetMapping("/{mid}/todos")
  54   │     String showToDoList(@PathVariable String mid, @ModelAttribute(name = "ToDoForm") ToDoForm form, Model model) {
  55   │         Member m = mService.getMember(mid);
  56   │         model.addAttribute("member", m);
  57   │         model.addAttribute("ToDoForm", form);
  58   │         List<ToDo> todos = tService.getToDoList(mid);
  59   │         model.addAttribute("todos", todos);
  60   │         List<ToDo> dones = tService.getDoneList(mid);
  61   │         model.addAttribute("dones", dones);
  62   │         return "list";
  63   │     }
  64   │ 
  65   │     /**
  66   │      * 全員のToDoリストのページ
  67   │      */
  68   │     @GetMapping("/{mid}/todos/all")
  69   │     String showAllToDoList(@PathVariable String mid, Model model) {
  70   │         Member m = mService.getMember(mid);
  71   │         model.addAttribute("member", m);
  72   │         List<ToDo> todos = tService.getToDoList();
  73   │         model.addAttribute("todos", todos);
  74   │         List<ToDo> dones = tService.getDoneList();
  75   │         model.addAttribute("dones", dones);
  76   │         return "alllist";
  77   │     }
  78   │ 
  79   │     /**
  80   │      * ToDoの作成.作成処理後,ユーザページへリダイレクト
  81   │      */
  82   │     @PostMapping("/{mid}/todos")
  83   │     String createToDo(@PathVariable String mid, @Validated @ModelAttribute(name = "ToDoForm") ToDoForm form,
  84   │             BindingResult bindingResult, Model model) {
  85   │         if (bindingResult.hasErrors()) {
  86   │             return showToDoList(mid, form, model);
  87   │         }
  88   │         tService.createToDo(mid, form);
  89   │         return "redirect:/" + mid + "/todos";
  90   │     }
  91   │ 
  92   │     /**
  93   │      * ToDoの完了.完了処理後,ユーザページへリダイレクト
  94   │      */
  95   │     @GetMapping("/{mid}/todos/{seq}/done")
  96   │     String doneToDo(@PathVariable String mid, @PathVariable Long seq, Model model) {
  97   │         tService.done(mid, seq);
  98   │         return "redirect:/" + mid + "/todos";
  99   │     }
 100   │ 
 101   │     /**
 102   │      * 背景色の更新.更新終了後,ユーザページへリダイレクト.
 103   │      */
 104   │     @PutMapping("/{mid}/todos/{seq}/background")
 105   │     String updateBackground(@PathVariable String mid, @PathVariable Long seq,
 106   │                             @Validated @ModelAttribute(name="ToDoForm") ToDoForm form, Model model) {
 107   │         tService.updateBackground(mid, seq, form.getBackground());
 108   │         return "redirect:/" + mid + "/todos";
 109   │     }
 110   │ }
───────┴────────────────────────────────────────────────────────────────────────