diff --git a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java b/src/main/java/ru/ulstu/user/controller/UserMvcController.java similarity index 80% rename from src/main/java/ru/ulstu/user/controller/UserControllerV2.java rename to src/main/java/ru/ulstu/user/controller/UserMvcController.java index 23bbba9..4a58168 100644 --- a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java +++ b/src/main/java/ru/ulstu/user/controller/UserMvcController.java @@ -3,14 +3,15 @@ package ru.ulstu.user.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.configuration.Constants; import ru.ulstu.odin.controller.OdinController; -import ru.ulstu.user.error.UserSendingMailException; import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserListDto; @@ -23,20 +24,26 @@ import java.util.Map; @Controller @RequestMapping(value = "/users") -public class UserControllerV2 extends OdinController { +public class UserMvcController extends OdinController { - private final Logger log = LoggerFactory.getLogger(UserControllerV2.class); + private final Logger log = LoggerFactory.getLogger(UserMvcController.class); private final UserService userService; private final UserSessionService userSessionService; - public UserControllerV2(UserService userService, - UserSessionService userSessionService) { + public UserMvcController(UserService userService, + UserSessionService userSessionService) { super(UserListDto.class, UserDto.class); this.userService = userService; this.userSessionService = userSessionService; } + @ExceptionHandler(Exception.class) + public String errorHandler(Model model, Exception exception) { + model.addAttribute("error", exception.getMessage()); + return "/error/error"; + } + @GetMapping("/profile") public void getUserProfile(ModelMap modelMap, HttpServletRequest request) { HttpSession session = request.getSession(false); @@ -53,7 +60,7 @@ public class UserControllerV2 extends OdinController { } @PostMapping("/invite") - public String inviteUser(@RequestParam(value = "email") String email) throws UserSendingMailException { + public String inviteUser(@RequestParam(value = "email") String email, ModelMap modelMap) { userService.inviteUser(email); return "redirect:/"; } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index 02f0ddb..63be451 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -52,6 +52,8 @@ import java.util.stream.Collectors; @Service @Transactional public class UserService implements UserDetailsService { + private static final String INVITE_USER_EXCEPTION = "Во время отправки приглашения произошла ошибка"; + private final Logger log = LoggerFactory.getLogger(UserService.class); private final UserRepository userRepository; private final PasswordEncoder passwordEncoder; @@ -213,6 +215,7 @@ public class UserService implements UserDetailsService { user.setFirstName(updateUser.getFirstName()); user.setLastName(updateUser.getLastName()); user.setEmail(updateUser.getEmail()); + user.setLogin(updateUser.getLogin()); user = userRepository.save(user); log.debug("Updated Information for User: {}", user.getLogin()); return userMapper.userEntityToUserDto(user); @@ -334,7 +337,7 @@ public class UserService implements UserDetailsService { try { mailService.sendInviteMail(variables, email); } catch (MessagingException e) { - throw new UserSendingMailException(email); + throw new UserSendingMailException(INVITE_USER_EXCEPTION); } } } diff --git a/src/main/resources/templates/error/error.html b/src/main/resources/templates/error/error.html new file mode 100644 index 0000000..53c9054 --- /dev/null +++ b/src/main/resources/templates/error/error.html @@ -0,0 +1,21 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/users/profile.html b/src/main/resources/templates/users/profile.html index c97e539..93adfc7 100644 --- a/src/main/resources/templates/users/profile.html +++ b/src/main/resources/templates/users/profile.html @@ -49,6 +49,15 @@ class="alert alert-danger">Incorrect email

+
+ + +

Incorrect email

+

+