// Adding new snippet - POST @RequestMapping(value = "/add", method = RequestMethod.POST) public String setAddSnippet( @ModelAttribute("newSnippet") Snippet newSnippet, HttpServletRequest request) { MultipartFile snippetImage = newSnippet.getSnippetImage(); MultipartFile snippetFile = newSnippet.getSnippetFile(); String rootDirectory = request.getSession().getServletContext().getRealPath("/"); // Adding snippet image if (snippetImage != null && !snippetImage.isEmpty()) { try { snippetImage.transferTo( new File(rootDirectory + "resources\\images\\" + newSnippet.getSnippetId() + ".png")); } catch (Exception e) { throw new RuntimeException("Snippet Image saving failed", e); } } // Adding snippet file if (snippetFile != null && !snippetFile.isEmpty()) { try { snippetFile.transferTo( new File(rootDirectory + "resources\\files\\" + newSnippet.getSnippetId() + ".pdf")); } catch (Exception e) { throw new RuntimeException("Snippet File saving failed", e); } } snippetService.addSnippet(newSnippet); return "redirect:/snippets/all"; }
// List snippets by project @RequestMapping("/{project}") public String getSnippetsByProject(Model model, @PathVariable("project") String snippetProject) { List<Snippet> snippets = snippetService.getSnippetsByProject(snippetProject); if (snippets == null || snippets.isEmpty()) { throw new NoSnippetsFoundUnderProjectException(); } model.addAttribute("snippets", snippets); return "snippets"; }
// Fetch snippet by id @RequestMapping("/snippet") public String getSnippetById(Model model, @RequestParam("id") String snippetId) { Snippet snippet = snippetService.getSnippetById(snippetId); model.addAttribute("snippet", snippet); return "snippet"; }
// Filter snippets by filter (label, project) @RequestMapping("/filter/{Criteria}") public String getSnippetsByFilter( @MatrixVariable(pathVar = "Criteria") Map<String, List<String>> filterParams, Model model) { model.addAttribute("snippets", snippetService.getSnippetsByFilter(filterParams)); return "snippets"; }
// List all snippets @RequestMapping("/all") public String allSnippets(Model model) { model.addAttribute("snippets", snippetService.getAllSnippets()); return "snippets"; }
@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET) public String deleteSnippetAction(Model model, @PathVariable("id") String snippetId) { Snippet snippet = snippetService.getSnippetById(snippetId); snippetService.deleteSnippet(snippet); return "redirect:/snippets/"; }