From 83014ebcbf2dec6babc38668f93850ec6409a82f Mon Sep 17 00:00:00 2001
From: debbyalmadea <almadeaputri@gmail.com>
Date: Wed, 15 Nov 2023 12:07:04 +0700
Subject: [PATCH] add: status query

---
 .../AccountVerificationRequestRepository.java | 43 ++++++++++++++++++-
 .../AccountVerificationRequestService.java    |  3 +-
 ...AccountVerificationRequestServiceImpl.java |  9 +++-
 3 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java b/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java
index e0c1c7b..a7e65b1 100644
--- a/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java
+++ b/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java
@@ -41,7 +41,48 @@ public class AccountVerificationRequestRepository implements BaseRepository<Acco
 
         int totalPage = 0;
         while (totalPageRs.next()) {
-            totalPage = totalPageRs.getInt(1) / pageSize + 1;
+            totalPage = (int) Math.ceil((double) totalPageRs.getInt(1) / pageSize);
+        }
+
+        List<AccountVerificationRequest> rows = new ArrayList<>();
+        while (rs.next()) {
+            System.out.println(rs.getString(1));
+            AccountVerificationRequest row = new AccountVerificationRequest();
+            row.setId(rs.getInt(1));
+            row.setUuid(rs.getString(2));
+            row.setUserId(rs.getString(3));
+            row.setStatus(rs.getString(4));
+            row.setCreatedAt(rs.getString(5));
+            row.setUpdatedAt(rs.getString(6));
+            rows.add(row);
+        }
+
+        GetAllResponse<AccountVerificationRequest> response = new GetAllResponse<>();
+        response.setPage(page);
+        response.setTotalPage(totalPage);
+        response.setData(rows);
+        return response;
+    }
+
+    public GetAllResponse<AccountVerificationRequest> getAll(int page, int pageSize, String status)
+            throws SQLException {
+        page = Math.max(page, 0);
+        pageSize = Math.max(pageSize, 1);
+        int offset = pageSize * (page - 1);
+        String query = "SELECT id, uuid, user_id, status, created_at, updated_at FROM account_verification_requests WHERE status = ? LIMIT ? OFFSET ?";
+        String totalPageQuery = "SELECT COUNT(*) AS total_page FROM account_verification_requests";
+        PreparedStatement ps = conn.prepareStatement(query);
+        PreparedStatement totalPagePs = conn.prepareStatement(totalPageQuery);
+        ps.setString(1, status);
+        ps.setInt(2, pageSize);
+        ps.setInt(3, offset);
+
+        ResultSet rs = ps.executeQuery();
+        ResultSet totalPageRs = totalPagePs.executeQuery();
+
+        int totalPage = 0;
+        while (totalPageRs.next()) {
+            totalPage = (int) Math.ceil((double) totalPageRs.getInt(1) / pageSize);
         }
 
         List<AccountVerificationRequest> rows = new ArrayList<>();
diff --git a/src/main/java/org/soapService/Services/AccountVerificationRequestService.java b/src/main/java/org/soapService/Services/AccountVerificationRequestService.java
index ea3cbfd..2c16058 100644
--- a/src/main/java/org/soapService/Services/AccountVerificationRequestService.java
+++ b/src/main/java/org/soapService/Services/AccountVerificationRequestService.java
@@ -19,7 +19,8 @@ public interface AccountVerificationRequestService {
         @WebMethod(operationName = "GetRequests")
         @RequestWrapper(className = "AccountVerificationRequestService.GetRequests")
         public ServiceResponse<GetAllResponse<AccountVerificationRequest>> getAccountVerificationRequests(
-                        @WebParam(name = "page") Integer page, @WebParam(name = "pageSize") Integer pageSize)
+                        @WebParam(name = "page") Integer page, @WebParam(name = "pageSize") Integer pageSize,
+                        @WebParam(name = "status") String status)
                         throws SOAPFaultException;
 
         @WebMethod(operationName = "CreateRequest")
diff --git a/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java
index fb02575..5cf4128 100644
--- a/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java
+++ b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java
@@ -21,7 +21,8 @@ public class AccountVerificationRequestServiceImpl extends BaseService implement
     private static AccountVerificationRequestValidation accountVerificationServiceValidation = new AccountVerificationRequestValidation();
 
     public ServiceResponse<GetAllResponse<AccountVerificationRequest>> getAccountVerificationRequests(Integer page,
-                                                                                                      Integer pageSize)
+            Integer pageSize,
+            String status)
             throws SOAPFaultException {
         if (page == null) {
             page = 1;
@@ -33,7 +34,11 @@ public class AccountVerificationRequestServiceImpl extends BaseService implement
 
         List<GetAllResponse<AccountVerificationRequest>> lru = new ArrayList<>();
         try {
-            lru.add(accountVerificationRepository.getAll(page, pageSize));
+            if (status == null) {
+                lru.add(accountVerificationRepository.getAll(page, pageSize));
+            } else {
+                lru.add(accountVerificationRepository.getAll(page, pageSize, status));
+            }
         } catch (Exception e) {
             System.out.println(e.getMessage());
             new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(),
-- 
GitLab