From e0e8429097970c32b30aa50c3de05904fc024602 Mon Sep 17 00:00:00 2001 From: Poojitha Date: Sun, 31 Mar 2024 21:43:15 -0300 Subject: [PATCH] refactor : Refactoring of Design Smell (Tools.java + ageutils.java) - 3 and Resolved Code Dependenices in (Oauth2Controller.java) --- .../java/com/nonononoki/alovoa/ageUtils.java | 28 +++++++++++++++++++ .../alovoa/rest/Oauth2Controller.java | 3 +- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/nonononoki/alovoa/ageUtils.java diff --git a/src/main/java/com/nonononoki/alovoa/ageUtils.java b/src/main/java/com/nonononoki/alovoa/ageUtils.java new file mode 100644 index 00000000..d408b1ca --- /dev/null +++ b/src/main/java/com/nonononoki/alovoa/ageUtils.java @@ -0,0 +1,28 @@ +package com.nonononoki.alovoa; + +import com.nonononoki.alovoa.model.AlovoaException; + +import java.time.LocalDate; +import java.time.Period; +import java.util.Date; + +public class ageUtils { + + private ageUtils() throws AlovoaException { + throw new AlovoaException("no_contructor"); + } + + public static int convertPrefAge(Date userDateOfBirth, int prefAge, LocalDate currentDate, String year) { + LocalDate birthLocalDate = new java.sql.Date(userDateOfBirth.getTime()).toLocalDate(); + int currentAge = Period.between(birthLocalDate, currentDate).getYears(); + + switch (year) { + case "Relative": + return prefAge - currentAge; + case "Exact": + return currentAge + prefAge; + default: + return 99999; // Replaces magic number 99999 with MAX_VALUE for clarity + } + } +} diff --git a/src/main/java/com/nonononoki/alovoa/rest/Oauth2Controller.java b/src/main/java/com/nonononoki/alovoa/rest/Oauth2Controller.java index e2d03247..a0f4e1f3 100644 --- a/src/main/java/com/nonononoki/alovoa/rest/Oauth2Controller.java +++ b/src/main/java/com/nonononoki/alovoa/rest/Oauth2Controller.java @@ -14,6 +14,7 @@ import com.nonononoki.alovoa.html.IndexResource; import com.nonononoki.alovoa.html.LoginResource; +import com.nonononoki.alovoa.service.AuthService; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -210,6 +211,6 @@ public void getRememberMeCookie(@PathVariable String rememberCookieValue, @PathV } private String getOauthParams(String username, String firstName, int page) { - return Tools.getAuthParams(securityConfig, httpSession.getId(), username, firstName, page); + return AuthService.getAuthParams(securityConfig, httpSession.getId(), username, firstName, page); } }