コード例 #1
0
  @RequestMapping(method = RequestMethod.POST)
  public String addNewPost(
      WallPost wallPost,
      BindingResult result,
      HttpServletRequest request,
      @RequestParam(value = "holler") MultipartFile file) {
    HttpSession session = request.getSession();
    User user = (User) session.getAttribute("loggedUser");
    wallPost.setAudioFileName(file.getOriginalFilename());
    wallPost.setAudioContentType(file.getContentType());
    wallPost.setPostBy(user);
    wallPost.setPostTo(user);
    wallPost.setDate(new Date());
    try {
      wallPost.setAudioContent(file.getBytes());
    } catch (IOException e) {
      e.printStackTrace();
    }
    postValidator.validate(wallPost, result);
    if (!result.hasErrors()) {
      postService.saveNewPost(wallPost);
    }

    return "redirect:/posts";
  }
コード例 #2
0
  @RequestMapping(value = "/saveFriendsPost/{id}", method = RequestMethod.POST)
  public String saveFriendsPost(
      WallPost wallPost,
      BindingResult result,
      HttpServletRequest request,
      @PathVariable(value = "id") int friendId,
      @RequestParam(value = "holler") MultipartFile file) {
    User postBy = (User) request.getSession().getAttribute("loggedUser");
    User postTo = userService.getUser(friendId);
    wallPost.setPostTo(postTo);
    wallPost.setPostBy(postBy);
    wallPost.setDate(new Date());
    wallPost.setAudioContentType(file.getContentType());
    wallPost.setAudioFileName(file.getOriginalFilename());
    try {
      wallPost.setAudioContent(file.getBytes());
    } catch (IOException e) {
      e.printStackTrace();
    }
    postValidator.validate(wallPost, result);
    if (!result.hasErrors()) {
      log.debug("Saved the wall Post" + wallPost.getPostContent());
      postService.saveNewPost(wallPost);
    }

    if (request.getSession().getAttribute("friend") != null) {
      User friend = (User) request.getSession().getAttribute("friend");
      String id = Integer.toString(friend.getUserId());
      request.getSession().setAttribute("friend", null);
      return "redirect:/friends/showFullProfile/" + id;
    } else {
      return "redirect:/posts";
    }
  }