001 | package com.withstars.controller; |
003 | import com.withstars.domain.Reply; |
004 | import com.withstars.domain.Tab; |
005 | import com.withstars.domain.Topic; |
006 | import com.withstars.domain.User; |
007 | import com.withstars.service.impl.ReplyServiceImpl; |
008 | import com.withstars.service.impl.TabServiceImpl; |
009 | import com.withstars.service.impl.TopicServiceImpl; |
010 | import com.withstars.service.impl.UserServiceImpl; |
011 | import org.springframework.beans.factory.annotation.Autowired; |
012 | import org.springframework.stereotype.Controller; |
013 | import org.springframework.web.bind.annotation.PathVariable; |
014 | import org.springframework.web.bind.annotation.RequestMapping; |
015 | import org.springframework.web.bind.annotation.RequestMethod; |
016 | import org.springframework.web.servlet.ModelAndView; |
017 | import org.springframework.web.servlet.mvc.support.RedirectAttributes; |
019 | import javax.servlet.http.HttpServletRequest; |
020 | import javax.servlet.http.HttpSession; |
021 | import java.util.Date; |
022 | import java.util.List; |
025 | import org.apache.commons.logging.Log; |
026 | import org.apache.commons.logging.LogFactory; |
032 | public class TopicController { |
035 | public TopicServiceImpl topicService; |
037 | public ReplyServiceImpl replyService; |
039 | public UserServiceImpl userService; |
041 | public TabServiceImpl tabService; |
044 | private final Log log = LogFactory.getLog(getClass()); |
052 | public ModelAndView toMain(HttpSession session){ |
053 | ModelAndView indexPage= new ModelAndView( "cate" ); |
055 | List<Topic> topics=topicService.listTopicsAndUsers(); |
058 | int topicsNum=topicService.getTopicsNum(); |
059 | int usersNum=userService.getUserCount(); |
061 | Integer uid=(Integer) session.getAttribute( "userId" ); |
062 | User user=userService.getUserById(uid); |
064 | List<Topic> hotestTopics=topicService.listMostCommentsTopics(); |
066 | indexPage.addObject( "topics" ,topics); |
067 | indexPage.addObject( "hotestTopics" ,hotestTopics); |
068 | indexPage.addObject( "topicsNum" ,topicsNum); |
069 | indexPage.addObject( "usersNum" ,usersNum); |
070 | indexPage.addObject( "user" ,user); |
080 | @RequestMapping ( "/t/{id}" ) |
081 | public ModelAndView toTopic( @PathVariable ( "id" )Integer id,HttpSession session){ |
083 | boolean ifSucc=topicService.clickAddOne(id); |
085 | Topic topic=topicService.selectById(id); |
087 | List<Reply> replies=replyService.getRepliesOfTopic(id); |
089 | int repliesNum=replyService.repliesNum(id); |
091 | int topicsNum=topicService.getTopicsNum(); |
092 | int usersNum=userService.getUserCount(); |
094 | Integer uid=(Integer) session.getAttribute( "userId" ); |
095 | User user=userService.getUserById(uid); |
097 | List<Topic> hotestTopics=topicService.listMostCommentsTopics(); |
100 | ModelAndView topicPage= new ModelAndView( "detail" ); |
101 | topicPage.addObject( "topic" ,topic); |
102 | topicPage.addObject( "replies" ,replies); |
103 | topicPage.addObject( "repliesNum" ,repliesNum); |
104 | topicPage.addObject( "topicsNum" ,topicsNum); |
105 | topicPage.addObject( "usersNum" ,usersNum); |
106 | topicPage.addObject( "user" ,user); |
107 | topicPage.addObject( "hotestTopics" ,hotestTopics); |
114 | @RequestMapping ( "/tab/{tabNameEn}" ) |
115 | public ModelAndView toTabPage( @PathVariable ( "tabNameEn" )String tabNameEn,HttpSession session){ |
116 | Tab tab=tabService.getByTabNameEn(tabNameEn); |
117 | Integer tabId=tab.getId(); |
119 | ModelAndView indexPage= new ModelAndView( "cate" ); |
121 | List<Topic> topics=topicService.listTopicsAndUsersOfTab(tabId); |
124 | int topicsNum=topicService.getTopicsNum(); |
125 | int usersNum=userService.getUserCount(); |
128 | Integer uid=(Integer) session.getAttribute( "userId" ); |
129 | User user=userService.getUserById(uid); |
131 | List<Topic> hotestTopics=topicService.listMostCommentsTopics(); |
133 | indexPage.addObject( "topics" ,topics); |
134 | indexPage.addObject( "topicsNum" ,topicsNum); |
135 | indexPage.addObject( "usersNum" ,usersNum); |
136 | indexPage.addObject( "tab" ,tab); |
137 | indexPage.addObject( "user" ,user); |
138 | indexPage.addObject( "hotestTopics" ,hotestTopics); |
148 | @RequestMapping (value = "/topic/add" , method = RequestMethod.POST) |
149 | public ModelAndView addTopic(HttpServletRequest request,HttpSession session){ |
150 | ModelAndView indexPage; |
152 | if (session.getAttribute( "userId" )== null ){ |
153 | indexPage= new ModelAndView( "redirect:/signin" ); |
157 | Integer userId=(Integer) session.getAttribute( "userId" ); |
158 | String title=request.getParameter( "title" ); |
159 | String content=request.getParameter( "content" ); |
160 | Byte tabId=Byte.parseByte(request.getParameter( "tab" )); |
162 | Topic topic= new Topic(); |
163 | topic.setUserId(userId); |
164 | topic.setTitle(title); |
165 | topic.setContent(content); |
166 | topic.setTabId(tabId); |
167 | topic.setCreateTime( new Date()); |
168 | topic.setUpdateTime( new Date()); |
170 | boolean ifSucc=topicService.addTopic(topic); |
171 | boolean ifSuccAddCredit=userService.addCredit( 1 ,userId); |
173 | if (log.isInfoEnabled()){ |
177 | indexPage= new ModelAndView( "redirect:/" ); |