@RequestMapping(value = "/editUser", method = RequestMethod.POST) public @ResponseBody String editUserPost( @ModelAttribute("user") User user, BindingResult result, Principal principal, @RequestParam String password, @RequestParam String name, @RequestParam String surname, @RequestParam String mailAddress, @RequestParam String img, @RequestParam int id) { String returnText; if (!result.hasErrors()) { User u = userService.getUser(id); if (!"".equals(name)) u.setName(name); if (!"".equals(surname)) u.setSurname(surname); if (!"".equals(img)) u.setImg(img); if (!"".equals(mailAddress)) u.setMailAddress(mailAddress); if (!"".equals(password)) { u.setPassword(password); userService.updateUser(u); } else { userService.updateUserByAdmin(u); } returnText = "Your edit was successful"; } else { returnText = "Sorry, an error has occur..."; } return returnText; }
// MAJ d'utilisateur @RequestMapping(value = "/updateUser", method = RequestMethod.POST) public ModelAndView update( @ModelAttribute("user") User user, BindingResult result, Principal principal) { int userId = userService.getUserByPseudo(principal.getName()).getId(); // Utilisateur avec le meme id user.setId(userId); User uSameId = userService.getUser(user.getId()); // Utilisateur avec le meme pseudo User uSamePseudo = userService.getUserByPseudo(user.getPseudo()); // Utilisateur avec le meme email User uSameEmail = userService.getUserByMail(user.getMailAddress()); UserMethod methods = new UserMethod(); String msg = methods.canUpdate(user, uSameId, uSamePseudo, uSameEmail); if (msg.equals("")) { user.setId(userId); userService.updateUser(user); } else { ModelAndView mav = new ModelAndView("user/editUser"); mav.addObject("error", msg); return mav; } return new ModelAndView("redirect:/user/index.html"); }
@PUT public boolean updateUser( @QueryParam("username") String username, @QueryParam("password") String password, @QueryParam("description") String description) { User user = new User(); user.setUsername(username); user.setPassword(password); user.setDescription(description); return userService.updateUser(user); }