@RequestMapping(
      value = {"/add-document-{userId}"},
      method = RequestMethod.POST)
  public String uploadDocument(
      @Valid FileBucket fileBucket, BindingResult result, ModelMap model, @PathVariable int userId)
      throws IOException {

    if (result.hasErrors()) {
      System.out.println("validation errors");
      User user = userService.findById(userId);
      model.addAttribute("user", user);

      List<UserDocument> documents = userDocumentService.findAllByUserId(userId);
      model.addAttribute("documents", documents);

      return "managedocuments";
    } else {

      System.out.println("Fetching file");

      User user = userService.findById(userId);
      model.addAttribute("user", user);

      saveDocument(fileBucket, user);

      return "redirect:/add-document-" + userId;
    }
  }
 @RequestMapping(
     value = {"/delete-document-{userId}-{docId}"},
     method = RequestMethod.GET)
 public String deleteDocument(@PathVariable int userId, @PathVariable int docId) {
   userDocumentService.deleteById(docId);
   return "redirect:/add-document-" + userId;
 }
  private void saveDocument(FileBucket fileBucket, User user) throws IOException {

    UserDocument document = new UserDocument();

    MultipartFile multipartFile = fileBucket.getFile();

    document.setName(multipartFile.getOriginalFilename());
    document.setDescription(fileBucket.getDescription());
    document.setType(multipartFile.getContentType());
    document.setContent(multipartFile.getBytes());
    document.setUser(user);
    userDocumentService.saveDocument(document);
  }
  @RequestMapping(
      value = {"/add-document-{userId}"},
      method = RequestMethod.GET)
  public String addDocuments(@PathVariable int userId, ModelMap model) {
    User user = userService.findById(userId);
    model.addAttribute("user", user);

    FileBucket fileModel = new FileBucket();
    model.addAttribute("fileBucket", fileModel);

    List<UserDocument> documents = userDocumentService.findAllByUserId(userId);
    model.addAttribute("documents", documents);

    return "managedocuments";
  }
  @RequestMapping(
      value = {"/download-document-{userId}-{docId}"},
      method = RequestMethod.GET)
  public String downloadDocument(
      @PathVariable int userId, @PathVariable int docId, HttpServletResponse response)
      throws IOException {
    UserDocument document = userDocumentService.findById(docId);
    response.setContentType(document.getType());
    response.setContentLength(document.getContent().length);
    response.setHeader(
        "Content-Disposition", "attachment; filename=\"" + document.getName() + "\"");

    FileCopyUtils.copy(document.getContent(), response.getOutputStream());

    return "redirect:/add-document-" + userId;
  }