From 1c10ef6a28cb0dc17805783cbd4b42fdd6dff663 Mon Sep 17 00:00:00 2001 From: Lilia Date: Sat, 20 Jul 2024 16:46:21 +0300 Subject: [PATCH 1/8] created login method --- .../java/mate/academy/service/AuthenticationService.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 8f7c7c975..fe07bac28 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,5 +1,7 @@ package mate.academy.service; +import mate.academy.model.User; + public class AuthenticationService { /** * Imagine that some user wants to login to your site. @@ -11,6 +13,13 @@ public class AuthenticationService { * Return false in any other cases. */ public boolean login(String email, String password) { + UserService user = new UserService(); + User found = user.findByEmail(email); + if (found != null) { + if (found.getPassword().equals(password)) { + return true; + } + } return false; } } From eb0dac09ef9fd7dad20bf71c82a8b06076fe52e0 Mon Sep 17 00:00:00 2001 From: Lilia Date: Sat, 20 Jul 2024 16:47:10 +0300 Subject: [PATCH 2/8] created findByEmail method --- src/main/java/mate/academy/service/UserService.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/mate/academy/service/UserService.java b/src/main/java/mate/academy/service/UserService.java index 1652d7d68..caf7902f3 100644 --- a/src/main/java/mate/academy/service/UserService.java +++ b/src/main/java/mate/academy/service/UserService.java @@ -15,6 +15,11 @@ public class UserService { * Return null if there is no suitable user */ public User findByEmail(String email) { + for (User i: users) { + if (i.getEmail().equals(email)) { + return i; + } + } return null; } } From 7deefefb9f0f98255788663df4de19912be981dc Mon Sep 17 00:00:00 2001 From: Lilia Date: Sun, 21 Jul 2024 01:39:38 +0300 Subject: [PATCH 3/8] corrected naming and method --- .../mate/academy/service/AuthenticationService.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index fe07bac28..859f25467 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -13,13 +13,9 @@ public class AuthenticationService { * Return false in any other cases. */ public boolean login(String email, String password) { - UserService user = new UserService(); - User found = user.findByEmail(email); - if (found != null) { - if (found.getPassword().equals(password)) { - return true; - } - } - return false; + UserService userService = new UserService(); + User userToCheck = userService.findByEmail(email); + + return userToCheck != null && userToCheck.getPassword().equals(password); } } From 8f15ec6d266fc8b6af084b46f5f8b39a5edcf0d3 Mon Sep 17 00:00:00 2001 From: Lilia Date: Sun, 21 Jul 2024 22:16:28 +0300 Subject: [PATCH 4/8] added setter and made the userService variable private and moved outside the method --- .../java/mate/academy/service/AuthenticationService.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 859f25467..9027c2580 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -12,9 +12,14 @@ public class AuthenticationService { * @return true if user by email exists and passed password is equal to user's password. * Return false in any other cases. */ + private UserService userService = new UserService(); + + public UserService getUserService() { + return userService; + } + public boolean login(String email, String password) { - UserService userService = new UserService(); - User userToCheck = userService.findByEmail(email); + User userToCheck = getUserService().findByEmail(email); return userToCheck != null && userToCheck.getPassword().equals(password); } From b37e6a5852c8195cbb3630a99ed5cf3d9b7324d1 Mon Sep 17 00:00:00 2001 From: Lilia Date: Sun, 21 Jul 2024 22:18:38 +0300 Subject: [PATCH 5/8] renamed name of elements in the loop --- src/main/java/mate/academy/service/UserService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/mate/academy/service/UserService.java b/src/main/java/mate/academy/service/UserService.java index caf7902f3..4782a793f 100644 --- a/src/main/java/mate/academy/service/UserService.java +++ b/src/main/java/mate/academy/service/UserService.java @@ -15,9 +15,9 @@ public class UserService { * Return null if there is no suitable user */ public User findByEmail(String email) { - for (User i: users) { - if (i.getEmail().equals(email)) { - return i; + for (User user: users) { + if (user.getEmail().equals(email)) { + return user; } } return null; From 7fdca45727065a396c60fd4a9a2707880c564567 Mon Sep 17 00:00:00 2001 From: Lilia Date: Sun, 21 Jul 2024 23:52:48 +0300 Subject: [PATCH 6/8] renamed User object and corrected private UserService to private final UserService --- .../java/mate/academy/service/AuthenticationService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 9027c2580..a5c27e4eb 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -12,15 +12,15 @@ public class AuthenticationService { * @return true if user by email exists and passed password is equal to user's password. * Return false in any other cases. */ - private UserService userService = new UserService(); + private final UserService userService = new UserService(); public UserService getUserService() { return userService; } public boolean login(String email, String password) { - User userToCheck = getUserService().findByEmail(email); + User user = getUserService().findByEmail(email); - return userToCheck != null && userToCheck.getPassword().equals(password); + return user != null && user.getPassword().equals(password); } } From 9c4cf1e839b0cc0605144b9bfdef4d9a9c920e51 Mon Sep 17 00:00:00 2001 From: Lilia Date: Mon, 22 Jul 2024 15:17:10 +0300 Subject: [PATCH 7/8] corrected getter --- src/main/java/mate/academy/service/AuthenticationService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index a5c27e4eb..f409c2de6 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -15,7 +15,9 @@ public class AuthenticationService { private final UserService userService = new UserService(); public UserService getUserService() { + return userService; + } public boolean login(String email, String password) { From 41ba5ec4766290a8d0830d5c3ecdaa014f8bda45 Mon Sep 17 00:00:00 2001 From: Lilia Date: Mon, 22 Jul 2024 19:56:22 +0300 Subject: [PATCH 8/8] removed get method --- .../java/mate/academy/service/AuthenticationService.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index f409c2de6..419c10a4f 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -14,14 +14,8 @@ public class AuthenticationService { */ private final UserService userService = new UserService(); - public UserService getUserService() { - - return userService; - - } - public boolean login(String email, String password) { - User user = getUserService().findByEmail(email); + User user = userService.findByEmail(email); return user != null && user.getPassword().equals(password); }