/** {@inheritDoc} */
 @Override
 public void toggleTopicSubscription(Topic topic) {
   JCUser current = userService.getCurrentUser();
   if (topic.userSubscribed(current)) {
     topic.getSubscribers().remove(current);
   } else {
     topic.getSubscribers().add(current);
   }
   topicDao.update(topic);
 }
 /**
  * Shows page with form for new code review
  *
  * @param branchId {@link Branch} branch, where code review will be created
  * @return {@code ModelAndView} object with "codeReviewForm" view, new {@link TopicDto} and branch
  *     id
  * @throws NotFoundException when branch was not found
  */
 @RequestMapping(value = "/reviews/new", method = RequestMethod.GET)
 public ModelAndView showNewCodeReviewPage(@RequestParam(BRANCH_ID) Long branchId)
     throws NotFoundException {
   Branch branch = branchService.get(branchId);
   Topic topic = new Topic();
   topic.setBranch(branch);
   TopicDto dto = new TopicDto(topic);
   return new ModelAndView(CODE_REVIEW_VIEW)
       .addObject(TOPIC_DTO, dto)
       .addObject(BRANCH_ID, branchId)
       .addObject(SUBMIT_URL, "/reviews/new?branchId=" + branchId)
       .addObject(BREADCRUMB_LIST, breadcrumbBuilder.getNewTopicBreadcrumb(branch));
 }
  /** {@inheritDoc} */
  @Override
  @PreAuthorize(
      "hasPermission(#postId, 'org.jtalks.jcommune.model.entity.Post', admin) or "
          + "hasPermission(#postId, 'org.jtalks.jcommune.model.entity.Post', delete)")
  public void deletePost(long postId) throws NotFoundException {
    Post post = get(postId);
    Topic topic = post.getTopic();
    topic.removePost(post);
    topicDao.update(topic);

    securityService.deleteFromAcl(post);

    logger.debug("Deleted post id={}", postId);
  }
  @Test
  public void testGetTopic() throws NotFoundException {
    Topic expectedTopic = new Topic(user, "title");
    when(topicDao.isExist(999L)).thenReturn(true);
    when(topicDao.get(999L)).thenReturn(expectedTopic);

    int viewsCount = expectedTopic.getViews();

    Topic actualTopic = topicFetchService.get(999L);

    assertEquals(actualTopic.getViews(), viewsCount + 1);
    assertEquals(actualTopic, expectedTopic, "Topics aren't equal");
    verify(topicDao).isExist(999L);
    verify(topicDao).get(999L);
  }
  /**
   * Create code review from data entered in form
   *
   * @param topicDto object with data from form
   * @param result {@link BindingResult} validation result
   * @param branchId branch, where topic will be created
   * @return {@code ModelAndView} object which will be redirect to forum.html
   * @throws NotFoundException when branch not found
   */
  @RequestMapping(value = "/reviews/new", method = RequestMethod.POST)
  public ModelAndView createCodeReview(
      @Valid @ModelAttribute TopicDto topicDto,
      BindingResult result,
      @RequestParam(BRANCH_ID) Long branchId)
      throws NotFoundException {
    Branch branch = branchService.get(branchId);
    if (result.hasErrors()) {
      return new ModelAndView(CODE_REVIEW_VIEW)
          .addObject(TOPIC_DTO, topicDto)
          .addObject(BRANCH_ID, branchId)
          .addObject(SUBMIT_URL, "/reviews/new?branchId=" + branchId)
          .addObject(BREADCRUMB_LIST, breadcrumbBuilder.getForumBreadcrumb(branch));
    }
    Topic topic = topicDto.getTopic();
    topic.setBranch(branch);
    Topic createdTopic = topicModificationService.createCodeReview(topic, topicDto.getBodyText());

    return new ModelAndView(REDIRECT_URL + createdTopic.getId());
  }