Java tutorial
/* * Copyright 2014 Azige. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.github.azige.bbs.web.controller; import java.io.IOException; import java.util.List; import java.util.stream.IntStream; import javax.annotation.PostConstruct; import io.github.azige.bbs.data.TopicRepository; import io.github.azige.bbs.entity.Comment; import io.github.azige.bbs.service.ServiceException; import io.github.azige.bbs.service.TopicService; import io.github.azige.bbs.entity.Profile; import io.github.azige.bbs.entity.Topic; import io.github.azige.bbs.web.ResourceNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.web.PageableDefault; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * * @author Azige */ @Controller public class TopicController { @Autowired private TopicRepository topicRepository; @Autowired private TopicService topicService; @RequestMapping(value = "/topic", method = RequestMethod.POST) public String topicPublish(Topic topic) { Profile loginProfile = (Profile) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); topic.setAuthor(loginProfile); topicService.publishTopic(topic); return "redirect:/topic/" + topic.getId(); } @RequestMapping(value = "/topic", method = RequestMethod.GET) public String topicListView(@PageableDefault(sort = "id", direction = Direction.DESC) Pageable pagable, Model model) { Page<Topic> page = topicRepository.findAll(pagable); List<Topic> topics = page.getContent(); model.addAttribute("topics", topics); int firstPageNumber = page.getNumber() - 3; int lastPageNumber = page.getNumber() + 3; if (firstPageNumber < 0) { firstPageNumber = 0; lastPageNumber = Math.min(firstPageNumber + 7, page.getTotalPages() - 1); } else if (lastPageNumber >= page.getTotalPages()) { lastPageNumber = page.getTotalPages() - 1; firstPageNumber = Math.max(lastPageNumber - 7, 0); } model.addAttribute("pageNumbers", IntStream.rangeClosed(firstPageNumber, lastPageNumber).toArray()); model.addAttribute("page", page); return "topic-list"; } @RequestMapping("/topic/{id}") public String topicView(@PathVariable Long id, Model model) throws ResourceNotFoundException { Topic topic = topicRepository.findOne(id); if (topic == null) { throw new ResourceNotFoundException(); } model.addAttribute("topic", topic); return "topic"; } @RequestMapping(value = "/topic/{id}/comment.json", method = RequestMethod.POST, consumes = "application/json", produces = "application/json; charset=UTF-8") public ResponseEntity<?> commentPublishAjax(@PathVariable("id") Long topicId, @RequestBody Comment comment) throws IOException { String content = comment.getContent(); Profile loginProfile = (Profile) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); try { topicService.publishComment(loginProfile, topicId, content); return new ResponseEntity<>(HttpStatus.OK); } catch (ResourceNotFoundException ex) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } }