OSDN Git Service

c22419f8ca23d6843a858951062460fdd1ed1155
[rabbit-bts/RabbitBTS.git] / src / jp / sourceforge / rabbitBTS / controllers / BbsController.java
1 /* vim:set ts=4 sts=4 sw=4 noet fenc=utf-8:
2
3    Copyright 2009 senju@users.sourceforge.jp
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16  */
17
18 package jp.sourceforge.rabbitBTS.controllers;
19
20 import javax.servlet.http.HttpServletRequest;
21
22 import jp.sourceforge.rabbitBTS.Sht;
23 import jp.sourceforge.rabbitBTS.models.Account;
24 import jp.sourceforge.rabbitBTS.models.BbsPost;
25 import jp.sourceforge.rabbitBTS.services.AccountService;
26 import jp.sourceforge.rabbitBTS.services.BbsService;
27
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Controller;
30 import org.springframework.ui.ModelMap;
31 import org.springframework.validation.BindingResult;
32 import org.springframework.validation.Validator;
33 import org.springframework.web.bind.annotation.RequestMapping;
34 import org.springframework.web.bind.annotation.RequestMethod;
35
36 @Controller
37 public final class BbsController extends BaseController {
38         @Autowired
39         private Validator validator;
40
41         @Autowired
42         private AccountService accountService;
43
44         @Autowired
45         private BbsService bbsService;
46
47         @RequestMapping(method = RequestMethod.GET)
48         public void index(ModelMap model, HttpServletRequest req) {
49                 final BbsPost g = new BbsPost();
50                 model.addAttribute(g);
51                 model.addAttribute("posts", this.bbsService.fetchPosts());
52         }
53
54         @RequestMapping(method = RequestMethod.POST)
55         public String postEdit(BbsPost post, BindingResult result,
56                         HttpServletRequest req, ModelMap map) {
57                 if (!this.isCsrfSafe(result)) {
58                         return null;
59                 }
60
61                 this.validator.validate(post, result);
62                 if (result.hasErrors()) {
63                         Sht.log(this).trace("BBS Post Validate fail.");
64                         return null;
65                 }
66
67                 final Account acc = this.accountService.getCurrentAccount();
68                 this.bbsService.addNewPost(post, acc);
69
70                 // TODO:メッセージ
71                 return "redirect:index.html";
72         }
73
74 }