コード例 #1
0
  /*
   * This method will provide the medium to add a new rating.
   */
  @PreAuthorize("hasAuthority('USER')")
  @RequestMapping(
      value = {"/rating"},
      method = RequestMethod.GET)
  public String addNewRating(
      @PathVariable Long book_id,
      ModelMap model,
      @AuthenticationPrincipal UserDetails userDetails) {
    User currentUser = userService.findByUsername(userDetails.getUsername());
    Book book = bookService.findById(book_id);
    Rating rating = new Rating();

    model.addAttribute("rating", rating);
    model.addAttribute("book", book);

    CommonAttributesPopulator.populate(currentUser, model);

    return "books/rating";
  }
コード例 #2
0
  /*
   * This method will be called on form submission, handling POST request for
   * saving book's rating in database.
   */
  @PreAuthorize("hasAuthority('USER')")
  @RequestMapping(
      value = {"/rating"},
      method = RequestMethod.POST)
  public String saveRating(
      @AuthenticationPrincipal UserDetails userDetails,
      @PathVariable Long book_id,
      @Valid Rating rating,
      BindingResult result,
      ModelMap model) {
    User currentUser = userService.findByUsername(userDetails.getUsername());
    Rating ratingToSave = new Rating();

    ratingToSave.setBook(bookService.findById(book_id));
    ratingToSave.setRatingValue(rating.getRatingValue());
    ratingToSave.setUser(currentUser);

    ratingService.saveRating(ratingToSave);

    return "redirect:/authors/{author_id}/books/{book_id}/preview";
  }