Last active
December 4, 2019 12:03
-
-
Save duese22/4a07d5e0306aaade89cdde9920c9660f to your computer and use it in GitHub Desktop.
Fix Set-User for Link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @PostMapping("/link/submit") | |
| public String createLink(@Valid Link link, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes){ | |
| User user = null; | |
| Optional optionalemail = BeanUtil.getBean(AuditorAwareImpl.class).getCurrentAuditor(); | |
| if(optionalemail.isPresent()) { | |
| Optional optionalUser = userRepository.findByEmail((String) optionalemail.get()); | |
| if(optionalUser.isPresent()){ | |
| user = (User) optionalUser.get(); | |
| }else{ | |
| user = null; | |
| } | |
| } | |
| if (bindingResult.hasErrors()){ | |
| logger.info("Validation Error Found while submitting a new Link"); | |
| model.addAttribute("link", link); | |
| return "link/submit"; | |
| }else{ | |
| link.setUser(user); | |
| linkService.save(link); | |
| redirectAttributes.addAttribute("id", link.getId()).addFlashAttribute("success", "true"); | |
| return "redirect:/link/{id}"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @PostMapping("/link/submit") | |
| public String createLink(@Valid Link link, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes){ | |
| User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | |
| if (bindingResult.hasErrors()){ | |
| logger.info("Validation Error Found while submitting a new Link"); | |
| model.addAttribute("link", link); | |
| return "link/submit"; | |
| }else{ | |
| link.setUser(user); | |
| linkService.save(link); | |
| redirectAttributes.addAttribute("id", link.getId()).addFlashAttribute("success", "true"); | |
| return "redirect:/link/{id}"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment