From 2738a7241a2618f8f8a2665c1623822c3755c885 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Wed, 3 Jul 2024 10:55:33 +0000 Subject: [PATCH 1/4] add deadline --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 40c3372..5fa0a49 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/fcC7yEpn) # Java Online Marathon ## To-Do List Project 1. Implement exception handling for custom exception. From c2305e3d2dad7d71168eab466f32c8cc63870591 Mon Sep 17 00:00:00 2001 From: alyonaMol <alen4hukm@gmail.com> Date: Sat, 6 Jul 2024 08:15:55 +0300 Subject: [PATCH 2/4] Implement error-pages --- .../itacademy/controller/UserController.java | 80 +++++++++++++++++-- .../exception/EntityNotFoundException.java | 7 ++ .../exception/GlobalExceptionHandler.java | 40 ++++++++++ .../NullEntityReferenceException.java | 7 ++ .../service/impl/RoleServiceImpl.java | 29 +++++-- .../service/impl/StateServiceImpl.java | 31 +++++-- .../service/impl/TaskServiceImpl.java | 30 +++++-- .../service/impl/ToDoServiceImpl.java | 28 +++++-- .../service/impl/UserServiceImpl.java | 29 +++++-- src/main/resources/templates/400-page.html | 14 ++++ src/main/resources/templates/404-page.html | 16 ++++ src/main/resources/templates/500-page.html | 15 ++++ 12 files changed, 289 insertions(+), 37 deletions(-) create mode 100644 src/main/java/com/softserve/itacademy/exception/EntityNotFoundException.java create mode 100644 src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java create mode 100644 src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java create mode 100644 src/main/resources/templates/400-page.html create mode 100644 src/main/resources/templates/404-page.html create mode 100644 src/main/resources/templates/500-page.html diff --git a/src/main/java/com/softserve/itacademy/controller/UserController.java b/src/main/java/com/softserve/itacademy/controller/UserController.java index d32288d..b5f85eb 100644 --- a/src/main/java/com/softserve/itacademy/controller/UserController.java +++ b/src/main/java/com/softserve/itacademy/controller/UserController.java @@ -1,5 +1,7 @@ package com.softserve.itacademy.controller; +import com.softserve.itacademy.exception.EntityNotFoundException; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.User; import com.softserve.itacademy.service.RoleService; import com.softserve.itacademy.service.UserService; @@ -9,6 +11,8 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.text.AttributedString; + @Controller @RequestMapping("/users") public class UserController { @@ -24,40 +28,106 @@ public UserController(UserService userService, RoleService roleService) { @GetMapping("/create") public String create(Model model) { model.addAttribute("user", new User()); + model.addAttribute("roles", roleService.getAll()); return "create-user"; } @PostMapping("/create") - public String create(@Validated @ModelAttribute("user") User user, BindingResult result) { + public String create(@Validated @ModelAttribute("user") User user, BindingResult result, Model model) { if (result.hasErrors()) { + model.addAttribute("roles", roleService.getAll()); return "create-user"; } +// throw new RuntimeException("Testing 500 Internal Server Error"); + try { + user.setPassword(user.getPassword()); user.setRole(roleService.readById(2)); + User newUser = userService.create(user); return "redirect:/todos/all/users/" + newUser.getId(); + } catch (NullEntityReferenceException e) { + model.addAttribute("errorMessage", e.getMessage()); + model.addAttribute("roles", roleService.getAll()); + return "create-user"; + } catch (Exception e) { + model.addAttribute("errorMessage", e.getMessage()); + return "create-user"; + } } @GetMapping("/{id}/read") public String read(@PathVariable long id, Model model) { - User user = userService.readById(id); - model.addAttribute("user", user); - return "user-info"; + try { + + User user = userService.readById(id); + model.addAttribute("user", user); + return "user-info"; + } catch (EntityNotFoundException e) { + model.addAttribute("errorMessage", e.getMessage()); + return "404-page"; + } catch (Exception e) { + model.addAttribute("errorMessage", e.getMessage()); + return "500-page"; + } } @GetMapping("/{id}/update") public String update(@PathVariable long id, Model model) { + try { + + User user = userService.readById(id); model.addAttribute("user", user); model.addAttribute("roles", roleService.getAll()); return "update-user"; + } catch (EntityNotFoundException e) { + model.addAttribute("errorMessage", e.getMessage()); + return "404-page"; + } catch (Exception e) { + model.addAttribute("errorMessage", e.getMessage()); + return "500-page"; + } + } + + @PostMapping("/{id}/update") + public String update(@Validated @ModelAttribute("user") User user, BindingResult result, Model model) { + if (result.hasErrors()) { + model.addAttribute("roles", roleService.getAll()); + return "update-user"; + } + + try { + user.setRole(roleService.readById(user.getRole().getId())); + userService.update(user); + return "redirect:/users/" + user.getId() + "/read"; + } catch (NullEntityReferenceException e) { + model.addAttribute("errorMessage", e.getMessage()); + model.addAttribute("roles", roleService.getAll()); + return "update-user"; + } catch (EntityNotFoundException e) { + model.addAttribute("errorMessage", e.getMessage()); + return "404-page"; + } catch (Exception e) { + model.addAttribute("errorMessage", e.getMessage()); + return "500-page"; + } } @GetMapping("/{id}/delete") - public String delete(@PathVariable("id") long id) { + public String delete(@PathVariable("id") long id, Model model) { + try { + userService.delete(id); return "redirect:/users/all"; + } catch (EntityNotFoundException e) { + model.addAttribute("errorMessage", e.getMessage()); + return "404-page"; + } catch (Exception e) { + model.addAttribute("errorMessage", e.getMessage()); + return "500-page"; + } } @GetMapping("/all") diff --git a/src/main/java/com/softserve/itacademy/exception/EntityNotFoundException.java b/src/main/java/com/softserve/itacademy/exception/EntityNotFoundException.java new file mode 100644 index 0000000..192b426 --- /dev/null +++ b/src/main/java/com/softserve/itacademy/exception/EntityNotFoundException.java @@ -0,0 +1,7 @@ +package com.softserve.itacademy.exception; + +public class EntityNotFoundException extends RuntimeException { + public EntityNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java b/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..fc9af33 --- /dev/null +++ b/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java @@ -0,0 +1,40 @@ +package com.softserve.itacademy.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.servlet.NoHandlerFoundException; + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(NoHandlerFoundException.class) + @ResponseStatus(HttpStatus.NOT_FOUND) + public String handleNoHandlerFoundException(NoHandlerFoundException ex, Model model) { + model.addAttribute("errorMessage", "Page not found"); + return "404-page"; + } + + @ExceptionHandler(EntityNotFoundException.class) + @ResponseStatus(HttpStatus.NOT_FOUND) + public String handleEntityNotFoundException(EntityNotFoundException ex, Model model) { + model.addAttribute("errorMessage", ex.getMessage()); + return "404-page"; + } + + @ExceptionHandler(NullEntityReferenceException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public String handleNullEntityReferenceException(NullEntityReferenceException ex, Model model) { + model.addAttribute("errorMessage", ex.getMessage()); + return "400-page"; + } + + @ExceptionHandler(Exception.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public String handleGenericException(Exception ex, Model model) { + model.addAttribute("errorMessage", ex.getMessage()); + return "500-page"; + } +} \ No newline at end of file diff --git a/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java new file mode 100644 index 0000000..e7f886f --- /dev/null +++ b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java @@ -0,0 +1,7 @@ +package com.softserve.itacademy.exception; + +public class NullEntityReferenceException extends RuntimeException { + public NullEntityReferenceException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java index 7d517c6..ff98046 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java @@ -1,5 +1,7 @@ package com.softserve.itacademy.service.impl; +import com.softserve.itacademy.exception.EntityNotFoundException; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.Role; import com.softserve.itacademy.repository.RoleRepository; import com.softserve.itacademy.service.RoleService; @@ -20,25 +22,38 @@ public RoleServiceImpl(RoleRepository roleRepository) { @Override public Role create(Role role) { - return roleRepository.save(role); + if (role == null) { + throw new NullEntityReferenceException("Role cannot be null"); + } + + return roleRepository.save(role); } @Override public Role readById(long id) { Optional<Role> optional = roleRepository.findById(id); - return optional.get(); + if (optional.isEmpty()) { + throw new EntityNotFoundException("Role with id " + id + " not found"); + } + return optional.get(); } @Override public Role update(Role role) { - Role oldRole = readById(role.getId()); - return roleRepository.save(role); + if (role == null) { + throw new NullEntityReferenceException("Role cannot be null"); + } + if (!roleRepository.existsById(role.getId())) { + throw new EntityNotFoundException("Role with id " + role.getId() + " not found"); + } + return roleRepository.save(role); } - @Override public void delete(long id) { - Role role = readById(id); - roleRepository.delete(role); + if (!roleRepository.existsById(id)) { + throw new EntityNotFoundException("Role with id " + id + " not found"); + } + roleRepository.deleteById(id); } @Override diff --git a/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java index ac95e84..44954c0 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java @@ -1,5 +1,7 @@ package com.softserve.itacademy.service.impl; +import com.softserve.itacademy.exception.EntityNotFoundException; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.State; import com.softserve.itacademy.repository.StateRepository; import com.softserve.itacademy.service.StateService; @@ -19,31 +21,48 @@ public StateServiceImpl(StateRepository stateRepository) { @Override public State create(State state) { - return stateRepository.save(state); + if (state == null) { + throw new NullEntityReferenceException("State cannot be null"); + } + + return stateRepository.save(state); } @Override public State readById(long id) { Optional<State> optional = stateRepository.findById(id); - return optional.get(); + if (optional.isEmpty()) { + throw new EntityNotFoundException("State with id " + id + " not found"); + } + return optional.get(); } @Override public State update(State state) { - State oldState = readById(state.getId()); + if (state == null) { + throw new NullEntityReferenceException("State cannot be null"); + } + if (!stateRepository.existsById(state.getId())) { + throw new EntityNotFoundException("State with id " + state.getId() + " not found"); + } return stateRepository.save(state); } @Override public void delete(long id) { - State state = readById(id); - stateRepository.delete(state); + if (!stateRepository.existsById(id)) { + throw new EntityNotFoundException("State with id " + id + " not found"); + } + stateRepository.deleteById(id); } @Override public State getByName(String name) { Optional<State> optional = Optional.ofNullable(stateRepository.getByName(name)); - return optional.get(); + if (optional.isEmpty()) { + throw new EntityNotFoundException("State with name " + name + " not found"); + } + return optional.get(); } @Override diff --git a/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java index 6161dc8..b315a1b 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java @@ -1,5 +1,7 @@ package com.softserve.itacademy.service.impl; +import com.softserve.itacademy.exception.EntityNotFoundException; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.Task; import com.softserve.itacademy.repository.TaskRepository; import com.softserve.itacademy.service.TaskService; @@ -18,26 +20,40 @@ public TaskServiceImpl(TaskRepository taskRepository) { } @Override - public Task create(Task user) { - return taskRepository.save(user); + public Task create(Task task) { + if (task == null) { + throw new NullEntityReferenceException("Task cannot be null"); + } + return taskRepository.save(task); } @Override public Task readById(long id) { Optional<Task> optional = taskRepository.findById(id); - return optional.get(); + if (optional.isEmpty()) { + throw new EntityNotFoundException("Task with id " + id + " not found"); + } + return optional.get(); } @Override public Task update(Task task) { - Task oldTask = readById(task.getId()); - return taskRepository.save(task); + if (task == null) { + throw new NullEntityReferenceException("Task cannot be null"); + } + if (!taskRepository.existsById(task.getId())) { + throw new EntityNotFoundException("Task with id " + task.getId() + " not found"); + } + return taskRepository.save(task); } + @Override public void delete(long id) { - Task task = readById(id); - taskRepository.delete(task); + if (!taskRepository.existsById(id)) { + throw new EntityNotFoundException("Task with id " + id + " not found"); + } + taskRepository.deleteById(id); } @Override diff --git a/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java index d000571..78fc7fe 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java @@ -1,5 +1,7 @@ package com.softserve.itacademy.service.impl; +import com.softserve.itacademy.exception.EntityNotFoundException; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.ToDo; import com.softserve.itacademy.repository.ToDoRepository; import com.softserve.itacademy.service.ToDoService; @@ -20,25 +22,39 @@ public ToDoServiceImpl(ToDoRepository todoRepository) { @Override public ToDo create(ToDo todo) { - return todoRepository.save(todo); + if (todo == null) { + throw new NullEntityReferenceException("ToDo cannot be null"); + } + return todoRepository.save(todo); } @Override public ToDo readById(long id) { Optional<ToDo> optional = todoRepository.findById(id); - return optional.get(); + if (optional.isEmpty()) { + throw new EntityNotFoundException("ToDo with id " + id + " not found"); + } + return optional.get(); } + @Override public ToDo update(ToDo todo) { - ToDo oldTodo = readById(todo.getId()); - return todoRepository.save(todo); + if (todo == null || todo.getTitle() == null || todo.getTitle().trim().isEmpty()) { + throw new NullEntityReferenceException("ToDo cannot be null"); + } + if (!todoRepository.existsById(todo.getId())) { + throw new EntityNotFoundException("ToDo with id " + todo.getId() + " not found"); + } + return todoRepository.save(todo); } @Override public void delete(long id) { - ToDo todo = readById(id); - todoRepository.delete(todo); + if (!todoRepository.existsById(id)) { + throw new EntityNotFoundException("ToDo with id " + id + " not found"); + } + todoRepository.deleteById(id); } @Override diff --git a/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java index 5b3645f..6c50482 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java @@ -1,5 +1,7 @@ package com.softserve.itacademy.service.impl; +import com.softserve.itacademy.exception.EntityNotFoundException; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.User; import com.softserve.itacademy.repository.UserRepository; import com.softserve.itacademy.service.UserService; @@ -20,25 +22,40 @@ public UserServiceImpl(UserRepository userRepository) { @Override public User create(User user) { - return userRepository.save(user); + if (user == null) { + throw new NullEntityReferenceException("User cannot be null"); + } + + return userRepository.save(user); } @Override public User readById(long id) { Optional<User> optional = userRepository.findById(id); - return optional.get(); + if (optional.isEmpty()) { + throw new EntityNotFoundException("User with id " + id + " not found"); + } + return optional.get(); } + @Override public User update(User user) { - User oldUser = readById(user.getId()); - return userRepository.save(user); + if (user == null) { + throw new NullEntityReferenceException("User cannot be null"); + } + if (!userRepository.existsById(user.getId())) { + throw new EntityNotFoundException("User with id " + user.getId() + " not found"); + } + return userRepository.save(user); } @Override public void delete(long id) { - User user = readById(id); - userRepository.delete(user); + if (!userRepository.existsById(id)) { + throw new EntityNotFoundException("User with id " + id + " not found"); + } + userRepository.deleteById(id); } @Override diff --git a/src/main/resources/templates/400-page.html b/src/main/resources/templates/400-page.html new file mode 100644 index 0000000..7842a0e --- /dev/null +++ b/src/main/resources/templates/400-page.html @@ -0,0 +1,14 @@ +<!DOCTYPE html> +<html xmlns:th="http://www.thymeleaf.org" lang="en"> +<head> + <meta charset="UTF-8"> + <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> + <title>Bad Request</title> +</head> +<body> +<div th:replace="header :: header"></div> +<h1>400 - Bad Request</h1> +<p th:text="${errorMessage}"></p> +<a href="/">Go Home</a> +</body> +</html> \ No newline at end of file diff --git a/src/main/resources/templates/404-page.html b/src/main/resources/templates/404-page.html new file mode 100644 index 0000000..efd3fe2 --- /dev/null +++ b/src/main/resources/templates/404-page.html @@ -0,0 +1,16 @@ +<!DOCTYPE html> +<html xmlns:th="http://www.thymeleaf.org" lang="en"> +<head> + <meta charset="UTF-8"> + <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> + <title>Page Not Found</title> +</head> +<body> +<div th:replace="header :: header"></div> +<h1>404 - Page Not Found</h1> +<!--<p>The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.</p>--> +<p th:text="${errorMessage}"></p> +<a href="/">Go Home</a> + +</body> +</html> \ No newline at end of file diff --git a/src/main/resources/templates/500-page.html b/src/main/resources/templates/500-page.html new file mode 100644 index 0000000..4a4b560 --- /dev/null +++ b/src/main/resources/templates/500-page.html @@ -0,0 +1,15 @@ +<!DOCTYPE html> +<html xmlns:th="http://www.thymeleaf.org" lang="en"> +<head> + <meta charset="UTF-8"> + <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> + <title>Error</title> +</head> +<body> +<div th:replace="header :: header"></div> +<h1>500 - Internal Server Error</h1> +<p th:text="${errorMessage}"></p> +<a href="/">Go Home</a> + +</body> +</html> \ No newline at end of file From ba5cde5d66d53f165ada6517bc51351e84c178dc Mon Sep 17 00:00:00 2001 From: alyonaMol <alen4hukm@gmail.com> Date: Sat, 6 Jul 2024 19:48:06 +0300 Subject: [PATCH 3/4] Implement error-pages and GlobalExceptionHandler --- .../softserve/itacademy/exception/GlobalExceptionHandler.java | 1 - .../itacademy/exception/NullEntityReferenceException.java | 1 + .../com/softserve/itacademy/service/impl/RoleServiceImpl.java | 1 - .../com/softserve/itacademy/service/impl/StateServiceImpl.java | 1 + .../com/softserve/itacademy/service/impl/TaskServiceImpl.java | 1 - .../com/softserve/itacademy/service/impl/ToDoServiceImpl.java | 1 - .../com/softserve/itacademy/service/impl/UserServiceImpl.java | 1 - src/main/resources/templates/400-page.html | 1 + src/main/resources/templates/404-page.html | 1 - src/main/resources/templates/500-page.html | 1 - 10 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java b/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java index fc9af33..8542273 100644 --- a/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java @@ -16,7 +16,6 @@ public String handleNoHandlerFoundException(NoHandlerFoundException ex, Model mo model.addAttribute("errorMessage", "Page not found"); return "404-page"; } - @ExceptionHandler(EntityNotFoundException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public String handleEntityNotFoundException(EntityNotFoundException ex, Model model) { diff --git a/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java index e7f886f..8a1a5cc 100644 --- a/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java +++ b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java @@ -4,4 +4,5 @@ public class NullEntityReferenceException extends RuntimeException { public NullEntityReferenceException(String message) { super(message); } + } \ No newline at end of file diff --git a/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java index ff98046..d58c605 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java @@ -55,7 +55,6 @@ public void delete(long id) { } roleRepository.deleteById(id); } - @Override public List<Role> getAll() { List<Role> roles = roleRepository.findAll(); diff --git a/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java index 44954c0..2849ed2 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java @@ -65,6 +65,7 @@ public State getByName(String name) { return optional.get(); } + @Override public List<State> getAll() { List<State> states = stateRepository.getAll(); diff --git a/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java index b315a1b..b4e6056 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java @@ -47,7 +47,6 @@ public Task update(Task task) { return taskRepository.save(task); } - @Override public void delete(long id) { if (!taskRepository.existsById(id)) { diff --git a/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java index 78fc7fe..18a5296 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java @@ -37,7 +37,6 @@ public ToDo readById(long id) { return optional.get(); } - @Override public ToDo update(ToDo todo) { if (todo == null || todo.getTitle() == null || todo.getTitle().trim().isEmpty()) { diff --git a/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java index 6c50482..a31d399 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java @@ -49,7 +49,6 @@ public User update(User user) { } return userRepository.save(user); } - @Override public void delete(long id) { if (!userRepository.existsById(id)) { diff --git a/src/main/resources/templates/400-page.html b/src/main/resources/templates/400-page.html index 7842a0e..5d42685 100644 --- a/src/main/resources/templates/400-page.html +++ b/src/main/resources/templates/400-page.html @@ -11,4 +11,5 @@ <h1>400 - Bad Request</h1> <p th:text="${errorMessage}"></p> <a href="/">Go Home</a> </body> + </html> \ No newline at end of file diff --git a/src/main/resources/templates/404-page.html b/src/main/resources/templates/404-page.html index efd3fe2..6b3009b 100644 --- a/src/main/resources/templates/404-page.html +++ b/src/main/resources/templates/404-page.html @@ -11,6 +11,5 @@ <h1>404 - Page Not Found</h1> <!--<p>The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.</p>--> <p th:text="${errorMessage}"></p> <a href="/">Go Home</a> - </body> </html> \ No newline at end of file diff --git a/src/main/resources/templates/500-page.html b/src/main/resources/templates/500-page.html index 4a4b560..c048b69 100644 --- a/src/main/resources/templates/500-page.html +++ b/src/main/resources/templates/500-page.html @@ -10,6 +10,5 @@ <h1>500 - Internal Server Error</h1> <p th:text="${errorMessage}"></p> <a href="/">Go Home</a> - </body> </html> \ No newline at end of file From 0139180d30aaeabc355e7df026f63d50b2e1e2be Mon Sep 17 00:00:00 2001 From: alyonaMol <alen4hukm@gmail.com> Date: Sat, 6 Jul 2024 19:57:42 +0300 Subject: [PATCH 4/4] Implement error-pages and GlobalExceptionHandler --- .../softserve/itacademy/exception/EntityNotFoundException.java | 1 + .../softserve/itacademy/exception/GlobalExceptionHandler.java | 1 + .../com/softserve/itacademy/service/impl/RoleServiceImpl.java | 1 + .../com/softserve/itacademy/service/impl/StateServiceImpl.java | 1 + .../com/softserve/itacademy/service/impl/TaskServiceImpl.java | 1 + .../com/softserve/itacademy/service/impl/ToDoServiceImpl.java | 1 + .../com/softserve/itacademy/service/impl/UserServiceImpl.java | 1 + src/main/resources/templates/400-page.html | 1 - src/main/resources/templates/404-page.html | 1 + src/main/resources/templates/500-page.html | 1 + 10 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/softserve/itacademy/exception/EntityNotFoundException.java b/src/main/java/com/softserve/itacademy/exception/EntityNotFoundException.java index 192b426..870f1a6 100644 --- a/src/main/java/com/softserve/itacademy/exception/EntityNotFoundException.java +++ b/src/main/java/com/softserve/itacademy/exception/EntityNotFoundException.java @@ -4,4 +4,5 @@ public class EntityNotFoundException extends RuntimeException { public EntityNotFoundException(String message) { super(message); } + } diff --git a/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java b/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java index 8542273..379ed82 100644 --- a/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java @@ -23,6 +23,7 @@ public String handleEntityNotFoundException(EntityNotFoundException ex, Model mo return "404-page"; } + @ExceptionHandler(NullEntityReferenceException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public String handleNullEntityReferenceException(NullEntityReferenceException ex, Model model) { diff --git a/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java index d58c605..1311296 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java @@ -48,6 +48,7 @@ public Role update(Role role) { } return roleRepository.save(role); } + @Override public void delete(long id) { if (!roleRepository.existsById(id)) { diff --git a/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java index 2849ed2..fc506ec 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java @@ -56,6 +56,7 @@ public void delete(long id) { stateRepository.deleteById(id); } + @Override public State getByName(String name) { Optional<State> optional = Optional.ofNullable(stateRepository.getByName(name)); diff --git a/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java index b4e6056..6b2dcc4 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java @@ -61,6 +61,7 @@ public List<Task> getAll() { return tasks.isEmpty() ? new ArrayList<>() : tasks; } + @Override public List<Task> getByTodoId(long todoId) { List<Task> tasks = taskRepository.getByTodoId(todoId); diff --git a/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java index 18a5296..568b9d2 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java @@ -62,6 +62,7 @@ public List<ToDo> getAll() { return todos.isEmpty() ? new ArrayList<>() : todos; } + @Override public List<ToDo> getByUserId(long userId) { List<ToDo> todos = todoRepository.getByUserId(userId); diff --git a/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java index a31d399..f41df4f 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java @@ -57,6 +57,7 @@ public void delete(long id) { userRepository.deleteById(id); } + @Override public List<User> getAll() { List<User> users = userRepository.findAll(); diff --git a/src/main/resources/templates/400-page.html b/src/main/resources/templates/400-page.html index 5d42685..7842a0e 100644 --- a/src/main/resources/templates/400-page.html +++ b/src/main/resources/templates/400-page.html @@ -11,5 +11,4 @@ <h1>400 - Bad Request</h1> <p th:text="${errorMessage}"></p> <a href="/">Go Home</a> </body> - </html> \ No newline at end of file diff --git a/src/main/resources/templates/404-page.html b/src/main/resources/templates/404-page.html index 6b3009b..57ec7d6 100644 --- a/src/main/resources/templates/404-page.html +++ b/src/main/resources/templates/404-page.html @@ -12,4 +12,5 @@ <h1>404 - Page Not Found</h1> <p th:text="${errorMessage}"></p> <a href="/">Go Home</a> </body> + </html> \ No newline at end of file diff --git a/src/main/resources/templates/500-page.html b/src/main/resources/templates/500-page.html index c048b69..1da0587 100644 --- a/src/main/resources/templates/500-page.html +++ b/src/main/resources/templates/500-page.html @@ -11,4 +11,5 @@ <h1>500 - Internal Server Error</h1> <p th:text="${errorMessage}"></p> <a href="/">Go Home</a> </body> + </html> \ No newline at end of file