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. 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..7fd1f9f --- /dev/null +++ b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java @@ -0,0 +1,11 @@ +package com.softserve.itacademy.exception; + +public class NullEntityReferenceException extends RuntimeException { + public NullEntityReferenceException() { + super(); + } + + public NullEntityReferenceException(String message) { + super(message); + } +} 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..c2c39d3 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java @@ -3,6 +3,7 @@ import com.softserve.itacademy.model.Role; import com.softserve.itacademy.repository.RoleRepository; import com.softserve.itacademy.service.RoleService; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -10,13 +11,10 @@ import java.util.Optional; @Service +@RequiredArgsConstructor public class RoleServiceImpl implements RoleService { - private RoleRepository roleRepository; - - public RoleServiceImpl(RoleRepository roleRepository) { - this.roleRepository = roleRepository; - } + private final RoleRepository roleRepository; @Override public Role create(Role role) { 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..a966c37 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java @@ -1,10 +1,12 @@ package com.softserve.itacademy.service.impl; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.User; import com.softserve.itacademy.repository.UserRepository; import com.softserve.itacademy.service.UserService; import org.springframework.stereotype.Service; +import javax.persistence.EntityNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -12,6 +14,9 @@ @Service public class UserServiceImpl implements UserService { + private static final String NULL_ENTITY_EXCEPTION = "Reference to an entity is null"; + private static final String ENTITY_NOT_FOUND_EXCEPTION = "Reference to an entity is not found"; + private UserRepository userRepository; public UserServiceImpl(UserRepository userRepository) { @@ -20,23 +25,36 @@ public UserServiceImpl(UserRepository userRepository) { @Override public User create(User user) { + if(user == null){ + throw new NullEntityReferenceException(NULL_ENTITY_EXCEPTION); + } + return userRepository.save(user); } @Override public User readById(long id) { + if(!userRepository.existsById(id)){ + throw new EntityNotFoundException(ENTITY_NOT_FOUND_EXCEPTION); + } Optional optional = userRepository.findById(id); return optional.get(); } @Override public User update(User user) { + if(user == null){ + throw new NullEntityReferenceException(NULL_ENTITY_EXCEPTION); + } User oldUser = readById(user.getId()); return userRepository.save(user); } @Override public void delete(long id) { + if(!userRepository.existsById(id)){ + throw new EntityNotFoundException(ENTITY_NOT_FOUND_EXCEPTION); + } User user = readById(id); userRepository.delete(user); } diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html new file mode 100644 index 0000000..76140ac --- /dev/null +++ b/src/main/resources/templates/400.html @@ -0,0 +1,21 @@ + + + + + + + 400 Bad Request + + +
+
+

400 Bad Request

+ +

+ +

+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/404.html b/src/main/resources/templates/404.html new file mode 100644 index 0000000..e67b843 --- /dev/null +++ b/src/main/resources/templates/404.html @@ -0,0 +1,29 @@ + + + + + + + + + Page Not Found + + + +
+

404 Page Not Found

+
+

Sorry, the page you are looking for does not exist. You will be redirected to the home page in 5 seconds.

+
+
+

+
+ Go to Home page +
+ + \ No newline at end of file