From 2bb84da3fd5510696741b9ae0cc5316067fb4c69 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Wed, 3 Jul 2024 15:09:52 +0000 Subject: [PATCH 01/13] 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 068926d2d5948ec45b2234a954651b6a0c8d120b Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 12:18:06 +0300 Subject: [PATCH 02/13] 404 page creation --- src/main/resources/templates/404.html | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/resources/templates/404.html diff --git a/src/main/resources/templates/404.html b/src/main/resources/templates/404.html new file mode 100644 index 0000000..9f05a33 --- /dev/null +++ b/src/main/resources/templates/404.html @@ -0,0 +1,19 @@ + + + + + + + + + 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 From 75704ec00d61190ffc05b4945dfba24cd45cf8f2 Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 12:42:56 +0300 Subject: [PATCH 03/13] modification for 404 page --- .../softserve/itacademy/service/impl/RoleServiceImpl.java | 8 +++----- src/main/resources/templates/404.html | 7 ++++++- 2 files changed, 9 insertions(+), 6 deletions(-) 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/resources/templates/404.html b/src/main/resources/templates/404.html index 9f05a33..c69e80e 100644 --- a/src/main/resources/templates/404.html +++ b/src/main/resources/templates/404.html @@ -12,7 +12,12 @@

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.

+
+

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
From 5eb565ecbe1aba4a159e33ea030fa1c444ea5934 Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 12:54:07 +0300 Subject: [PATCH 04/13] modification for 404 page --- src/main/resources/templates/404.html | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/resources/templates/404.html b/src/main/resources/templates/404.html index c69e80e..a358193 100644 --- a/src/main/resources/templates/404.html +++ b/src/main/resources/templates/404.html @@ -8,6 +8,11 @@ Page Not Found +
From c1ce0570057145dee73404a7af85fe1da04da808 Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 13:02:48 +0300 Subject: [PATCH 05/13] modification for 404 page --- src/main/resources/templates/404.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/templates/404.html b/src/main/resources/templates/404.html index a358193..e67b843 100644 --- a/src/main/resources/templates/404.html +++ b/src/main/resources/templates/404.html @@ -10,7 +10,7 @@ Page Not Found From e673145af0312d0b09d9c256c69cc505f199a0b2 Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 13:19:28 +0300 Subject: [PATCH 06/13] new 400 page --- src/main/resources/templates/400.html | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/resources/templates/400.html diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html new file mode 100644 index 0000000..30db404 --- /dev/null +++ b/src/main/resources/templates/400.html @@ -0,0 +1,19 @@ + + + + + + + 400 Bad Request + + +

400 Bad Request

+

+ +

+
    +
  • +
+ + \ No newline at end of file From 695951755c09bdf417a61fbf3f400407c4acadfa Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 16:52:59 +0300 Subject: [PATCH 07/13] new 400 page --- src/main/resources/templates/400.html | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html index 30db404..274cc99 100644 --- a/src/main/resources/templates/400.html +++ b/src/main/resources/templates/400.html @@ -8,12 +8,12 @@ 400 Bad Request -

400 Bad Request

-

- -

-
    -
  • -
+

400 Bad Request

+

+ +

+
    +
  • +
\ No newline at end of file From 0c9e56839d85b6c1fe5e4b2e3a78c4ea7e84c199 Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 16:55:55 +0300 Subject: [PATCH 08/13] updated 400 page --- src/main/resources/templates/400.html | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html index 274cc99..f5e1e7e 100644 --- a/src/main/resources/templates/400.html +++ b/src/main/resources/templates/400.html @@ -8,12 +8,15 @@ 400 Bad Request -

400 Bad Request

-

- -

-
    -
  • -
+
+
+

400 Bad Request

+ + +

+ +

+
+
\ No newline at end of file From ea055a0eb483a1b14b0cd762a77d21c3a4c78c23 Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 16:56:35 +0300 Subject: [PATCH 09/13] updated 400 page --- src/main/resources/templates/400.html | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html index f5e1e7e..da259dd 100644 --- a/src/main/resources/templates/400.html +++ b/src/main/resources/templates/400.html @@ -12,7 +12,6 @@

400 Bad Request

-

From 87c91c2ca0ad9693b76d7908babafe693ee4949c Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 21:32:23 +0300 Subject: [PATCH 10/13] updated 400 page --- src/main/resources/templates/400.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html index da259dd..76140ac 100644 --- a/src/main/resources/templates/400.html +++ b/src/main/resources/templates/400.html @@ -12,8 +12,8 @@

400 Bad Request

-

- +

+

From c08c1bb65a5075734e7051c3b4c98002dc8c6b6e Mon Sep 17 00:00:00 2001 From: Vadym Honcharuk Date: Fri, 5 Jul 2024 13:42:26 +0300 Subject: [PATCH 11/13] add NullEntityReferenceException --- .../exception/NullEntityReferenceException.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java 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..ca114cc --- /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 Exception { + public NullEntityReferenceException() { + super("Reference to an entity is null"); + } + + public NullEntityReferenceException(String message) { + super(message); + } +} From 9d12b9cc320f1d7704c56375d39d4af64aecf209 Mon Sep 17 00:00:00 2001 From: Vadym Honcharuk Date: Fri, 5 Jul 2024 14:10:15 +0300 Subject: [PATCH 12/13] do some changes in NullEntityReferenceException --- .../itacademy/exception/NullEntityReferenceException.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java index ca114cc..7fd1f9f 100644 --- a/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java +++ b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java @@ -1,8 +1,8 @@ package com.softserve.itacademy.exception; -public class NullEntityReferenceException extends Exception { +public class NullEntityReferenceException extends RuntimeException { public NullEntityReferenceException() { - super("Reference to an entity is null"); + super(); } public NullEntityReferenceException(String message) { From ccb1288f3f494e02468db192d9ec78377821ddc3 Mon Sep 17 00:00:00 2001 From: Vadym Honcharuk Date: Fri, 5 Jul 2024 14:11:03 +0300 Subject: [PATCH 13/13] add handle exception in methods for UserServiceImpl --- .../service/impl/UserServiceImpl.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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); }