diff --git a/.idea/misc.xml b/.idea/misc.xml index ef964bbd7e5479ede91597abdcf706df9ecdf000..2fce26df0011b9f3e8a332595103898493c9d583 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ -<?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="MavenProjectsManager"> @@ -8,7 +7,7 @@ </list> </option> </component> - <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="corretto-21" project-jdk-type="JavaSDK"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="corretto-1.8 (WSL)" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/out" /> </component> </project> \ No newline at end of file diff --git a/src/main/java/org/soapService/Common/ServiceResponse.java b/src/main/java/org/soapService/Common/ServiceResponse.java index 95ae6520bf08b565cdff72e2d97d4bfd5db5162b..5f0446b98d274e34f4765d9ae5d64910e24b13e2 100644 --- a/src/main/java/org/soapService/Common/ServiceResponse.java +++ b/src/main/java/org/soapService/Common/ServiceResponse.java @@ -5,6 +5,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; import org.soapService.Domain.AccountVerificationRequest; import org.soapService.Domain.CatalogRequest; +import org.soapService.Domain.GetAllResponse; import org.soapService.Domain.ReportUser; import javax.xml.bind.annotation.XmlRootElement; @@ -17,7 +18,8 @@ import java.util.List; @Setter @NoArgsConstructor @XmlRootElement -@XmlSeeAlso({AccountVerificationRequest.class, CatalogRequest.class, ReportUser.class, ArrayList.class}) +@XmlSeeAlso({ AccountVerificationRequest.class, GetAllResponse.class, CatalogRequest.class, ReportUser.class, + ArrayList.class }) public class ServiceResponse<T> implements Serializable { private String status; private String message; diff --git a/src/main/java/org/soapService/Domain/GetAllResponse.java b/src/main/java/org/soapService/Domain/GetAllResponse.java new file mode 100644 index 0000000000000000000000000000000000000000..019241676dfa894e6f93e13b2363e7e85786f0c5 --- /dev/null +++ b/src/main/java/org/soapService/Domain/GetAllResponse.java @@ -0,0 +1,18 @@ +package org.soapService.Domain; + +import java.util.List; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class GetAllResponse<T> { + private int page; + private int totalPage; + private List<T> data; +} \ No newline at end of file diff --git a/src/main/java/org/soapService/Exceptions/RequestException.java b/src/main/java/org/soapService/Exceptions/RequestException.java index b15317d597379b5db74b414125b938c28f16fff4..89315059472ff79a6956458d8ccbb5a57203441a 100644 --- a/src/main/java/org/soapService/Exceptions/RequestException.java +++ b/src/main/java/org/soapService/Exceptions/RequestException.java @@ -21,4 +21,3 @@ public class RequestException { } } - diff --git a/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java b/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java index 44f1e5e0d3a71f2d6c71fae481f5dbb916b36865..ebc58de0d6b83460d8df3137c177292ab3d93dc2 100644 --- a/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java +++ b/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java @@ -2,10 +2,13 @@ package org.soapService.Repository; import org.soapService.Config.Database; import org.soapService.Domain.AccountVerificationRequest; +import org.soapService.Domain.GetAllResponse; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -22,12 +25,41 @@ public class AccountVerificationRequestRepository implements BaseRepository<Acco return ps.executeUpdate(); } - public List<AccountVerificationRequest> getAll() throws SQLException { - String query = "SELECT id, uuid, user_id, status, created_at, updated_at FROM account_verification_requests"; + public GetAllResponse<AccountVerificationRequest> getAll(int page, int pageSize) 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, count(*) over() AS total_page FROM account_verification_requests LIMIT ? OFFSET ?"; PreparedStatement ps = conn.prepareStatement(query); + ps.setInt(1, pageSize); + ps.setInt(2, offset); - System.out.println(ps.executeQuery()); - return null; + ResultSet rs = ps.executeQuery(); + List<AccountVerificationRequest> rows = new ArrayList<>(); + int totalPage = 0; + while (rs.next()) { + System.out.println(rs.getString(1)); + System.out.println(rs.getString(7)); + + if (totalPage == 0) { + totalPage = rs.getInt(7); + } + + 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 AccountVerificationRequest getById(int id) throws SQLException { @@ -35,19 +67,42 @@ public class AccountVerificationRequestRepository implements BaseRepository<Acco PreparedStatement ps = conn.prepareStatement(query); ps.setInt(1, id); - System.out.println(ps.executeQuery()); - return null; - } + ResultSet rs = ps.executeQuery(); + List<AccountVerificationRequest> rows = new ArrayList<>(); + while (rs.next()) { + 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); + } + + if (rows.size() == 0) { + return null; + } - public void update(int id) throws SQLException { + return rows.get(0); + } + public int update(AccountVerificationRequest data) throws SQLException { + String query = "UPDATE account_verification_requests SET status = ? WHERE user_id = ? AND status = 'PENDING'"; + PreparedStatement ps = conn.prepareStatement(query); + ps.setString(1, data.getStatus()); + ps.setString(2, data.getUserId()); + return ps.executeUpdate(); } public void deleteAll() throws SQLException { } - public void delete(int id) throws SQLException { - + public int delete(int id) throws SQLException { + String query = "DELETE FROM account_verification_requests WHERE id = ?"; + PreparedStatement ps = conn.prepareStatement(query); + ps.setInt(1, id); + return ps.executeUpdate(); } } diff --git a/src/main/java/org/soapService/Repository/BaseRepository.java b/src/main/java/org/soapService/Repository/BaseRepository.java index ff841a1dd1afa59e86888858a0534685ce3b47b7..f0f6a7e652b0945e52c593f67ac049229003c765 100644 --- a/src/main/java/org/soapService/Repository/BaseRepository.java +++ b/src/main/java/org/soapService/Repository/BaseRepository.java @@ -1,19 +1,20 @@ package org.soapService.Repository; import java.sql.SQLException; -import java.util.List; + +import org.soapService.Domain.GetAllResponse; public interface BaseRepository<T> { public int add(T data) throws SQLException; - public List<T> getAll() throws SQLException; + public GetAllResponse<T> getAll(int page, int pageSize) throws SQLException; public T getById(int id) throws SQLException; - public void update(int id) throws SQLException; + public int update(T data) throws SQLException; public void deleteAll() throws SQLException; - public void delete(int id) throws SQLException; + public int delete(int id) throws SQLException; } diff --git a/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java b/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java index 7627e14b2da00378763819e5cd973d4ad09e7d01..1647345e938ff1b7c1198a186a12cf79774eac45 100644 --- a/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java +++ b/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java @@ -2,6 +2,7 @@ package org.soapService.Repository; import org.soapService.Config.Database; import org.soapService.Domain.CatalogRequest; +import org.soapService.Domain.GetAllResponse; import java.sql.Connection; import java.sql.PreparedStatement; @@ -27,7 +28,7 @@ public class CatalogReqeustRepository implements BaseRepository<CatalogRequest> return ps.executeUpdate(); } - public List<CatalogRequest> getAll() throws SQLException { + public GetAllResponse<CatalogRequest> getAll(int page, int pageSize) throws SQLException { return null; } @@ -35,15 +36,15 @@ public class CatalogReqeustRepository implements BaseRepository<CatalogRequest> return null; } - public void update(int id) throws SQLException { - + public int update(CatalogRequest id) throws SQLException { + return 0; } public void deleteAll() throws SQLException { } - public void delete(int id) throws SQLException { - + public int delete(int id) throws SQLException { + return 0; } } diff --git a/src/main/java/org/soapService/Repository/LogRepository.java b/src/main/java/org/soapService/Repository/LogRepository.java index c9656220f78c335370a1160b034f483d944ff2bd..1aceb7067cdc6d0f58d8724392a9cee7efc41401 100644 --- a/src/main/java/org/soapService/Repository/LogRepository.java +++ b/src/main/java/org/soapService/Repository/LogRepository.java @@ -1,6 +1,7 @@ package org.soapService.Repository; import org.soapService.Config.Database; +import org.soapService.Domain.GetAllResponse; import org.soapService.Domain.Log; import java.sql.Connection; @@ -22,7 +23,7 @@ public class LogRepository implements BaseRepository<Log> { return ps.executeUpdate(); } - public List<Log> getAll() throws SQLException { + public GetAllResponse<Log> getAll(int page, int pageSize) throws SQLException { return null; } @@ -30,15 +31,15 @@ public class LogRepository implements BaseRepository<Log> { return null; } - public void update(int id) throws SQLException { - + public int update(Log id) throws SQLException { + return 0; } public void deleteAll() throws SQLException { } - public void delete(int id) throws SQLException { - + public int delete(int id) throws SQLException { + return 0; } } diff --git a/src/main/java/org/soapService/Repository/ReportUserRepository.java b/src/main/java/org/soapService/Repository/ReportUserRepository.java index 1d8bf8c269fabc507f95dcc478f3fff5084064e7..b5c8507d780d5f077ee043bbd65ab56ae176bf4a 100644 --- a/src/main/java/org/soapService/Repository/ReportUserRepository.java +++ b/src/main/java/org/soapService/Repository/ReportUserRepository.java @@ -1,6 +1,7 @@ package org.soapService.Repository; import org.soapService.Config.Database; +import org.soapService.Domain.GetAllResponse; import org.soapService.Domain.ReportUser; import java.sql.Connection; @@ -23,7 +24,7 @@ public class ReportUserRepository implements BaseRepository<ReportUser> { return ps.executeUpdate(); } - public List<ReportUser> getAll() throws SQLException { + public GetAllResponse<ReportUser> getAll(int page, int pageSize) throws SQLException { return null; } @@ -31,15 +32,15 @@ public class ReportUserRepository implements BaseRepository<ReportUser> { return null; } - public void update(int id) throws SQLException { - + public int update(ReportUser id) throws SQLException { + return 0; } public void deleteAll() throws SQLException { } - public void delete(int id) throws SQLException { - + public int delete(int id) throws SQLException { + return 0; } } diff --git a/src/main/java/org/soapService/Services/AccountVerificationRequestService.java b/src/main/java/org/soapService/Services/AccountVerificationRequestService.java index a90fb0114fd51899ec88abe88e28f927ddb43a3c..ea3cbfd167a52c648e22a338c7ccd985e282e9d3 100644 --- a/src/main/java/org/soapService/Services/AccountVerificationRequestService.java +++ b/src/main/java/org/soapService/Services/AccountVerificationRequestService.java @@ -2,6 +2,7 @@ package org.soapService.Services; import org.soapService.Common.ServiceResponse; import org.soapService.Domain.AccountVerificationRequest; +import org.soapService.Domain.GetAllResponse; import javax.jws.HandlerChain; import javax.jws.WebMethod; @@ -12,27 +13,36 @@ import javax.xml.ws.RequestWrapper; import javax.xml.ws.soap.SOAPFaultException; @WebService -@XmlSeeAlso({ServiceResponse.class}) +@XmlSeeAlso({ ServiceResponse.class }) @HandlerChain(file = "handler-chain.xml") public interface AccountVerificationRequestService { - @WebMethod(operationName = "GetRequests") - @RequestWrapper(className = "AccountVerificationRequestService.GetRequests") - public ServiceResponse<AccountVerificationRequest> getAccountVerificationRequests() throws SOAPFaultException; + @WebMethod(operationName = "GetRequests") + @RequestWrapper(className = "AccountVerificationRequestService.GetRequests") + public ServiceResponse<GetAllResponse<AccountVerificationRequest>> getAccountVerificationRequests( + @WebParam(name = "page") Integer page, @WebParam(name = "pageSize") Integer pageSize) + throws SOAPFaultException; - @WebMethod(operationName = "CreateRequest") - @RequestWrapper(className = "AccountVerificationRequestService.AccountVerificationCreateRequest") - public ServiceResponse<AccountVerificationRequest> createAccountVerificationRequest(@WebParam(name = "userId") String userId) - throws SOAPFaultException; + @WebMethod(operationName = "CreateRequest") + @RequestWrapper(className = "AccountVerificationRequestService.AccountVerificationCreateRequest") + public ServiceResponse<AccountVerificationRequest> createAccountVerificationRequest( + @WebParam(name = "userId") String userId) + throws SOAPFaultException; - @WebMethod(operationName = "AcceptRequest") - @RequestWrapper(className = "AccountVerificationRequestService.AcceptRequest") - public ServiceResponse acceptAccountVerificationRequest(@WebParam(name = "userId") int userId) throws SOAPFaultException; + @WebMethod(operationName = "AcceptRequest") + @RequestWrapper(className = "AccountVerificationRequestService.AcceptRequest") + public ServiceResponse<AccountVerificationRequest> acceptAccountVerificationRequest( + @WebParam(name = "userId") String userId) + throws SOAPFaultException; - @WebMethod(operationName = "RejectRequest") - @RequestWrapper(className = "AccountVerificationRequestService.RejectRequest") - public ServiceResponse rejectAccountVerificationRequest(@WebParam(name = "userId") int userId) throws SOAPFaultException; + @WebMethod(operationName = "RejectRequest") + @RequestWrapper(className = "AccountVerificationRequestService.RejectRequest") + public ServiceResponse<AccountVerificationRequest> rejectAccountVerificationRequest( + @WebParam(name = "userId") String userId) + throws SOAPFaultException; - @WebMethod(operationName = "DeleteRequest") - @RequestWrapper(className = "AccountVerificationRequestService.DeleteRequest") - public ServiceResponse deleteAccountVerificationRequest(@WebParam(name = "requestId") int requestId) throws SOAPFaultException; + @WebMethod(operationName = "DeleteRequest") + @RequestWrapper(className = "AccountVerificationRequestService.DeleteRequest") + public ServiceResponse<AccountVerificationRequest> deleteAccountVerificationRequest( + @WebParam(name = "requestId") int requestId) + throws SOAPFaultException; } diff --git a/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java index d1179c8e0e284bca0a535300e6c09393aaac217b..eb15d1d87146cfec7528393324248b987ac73b29 100644 --- a/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java +++ b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java @@ -3,6 +3,7 @@ package org.soapService.Services; import org.soapService.Common.HTTPStatusCode; import org.soapService.Common.ServiceResponse; import org.soapService.Domain.AccountVerificationRequest; +import org.soapService.Domain.GetAllResponse; import org.soapService.Exceptions.RequestException; import org.soapService.Exceptions.ValidationException; import org.soapService.Repository.AccountVerificationRequestRepository; @@ -19,24 +20,65 @@ public class AccountVerificationRequestServiceImpl extends BaseService implement private static AccountVerificationRequestRepository accountVerificationRepository = new AccountVerificationRequestRepository(); private static AccountVerificationRequestValidation accountVerificationServiceValidation = new AccountVerificationRequestValidation(); - public ServiceResponse<AccountVerificationRequest> getAccountVerificationRequests() throws SOAPFaultException { - return null; + public ServiceResponse<GetAllResponse<AccountVerificationRequest>> getAccountVerificationRequests(Integer page, + Integer pageSize) + throws SOAPFaultException { + if (page == null) { + page = 1; + } + + if (pageSize == null) { + pageSize = 20; + } + + List<GetAllResponse<AccountVerificationRequest>> lru = new ArrayList<>(); + try { + lru.add(accountVerificationRepository.getAll(page, pageSize)); + } catch (Exception e) { + System.out.println(e.getMessage()); + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), + "Something went wrong, please try again later"); + } + + ServiceResponse<GetAllResponse<AccountVerificationRequest>> response = new ServiceResponse<>(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Successfully get all account verification requests"); + response.setData(lru); + + lru.forEach((item) -> { + item.getData().forEach((data) -> { + System.out.println(data.getId()); + }); + }); + + return response; } - public ServiceResponse<AccountVerificationRequest> createAccountVerificationRequest(String userId) throws SOAPFaultException { + public ServiceResponse<AccountVerificationRequest> createAccountVerificationRequest(String userId) + throws SOAPFaultException { List<AccountVerificationRequest> lru = new ArrayList<>(); try { accountVerificationServiceValidation.validateCreateVerificationRequest(userId); AccountVerificationRequest accountVerificationRequest = new AccountVerificationRequest(); accountVerificationRequest.setUserId(userId); + accountVerificationRepository.add(accountVerificationRequest); lru.add(accountVerificationRequest); - accountVerificationRepository.add(accountVerificationRequest); } catch (ValidationException e) { System.out.println(e.getMessage()); new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), e.getMessage()); } catch (SQLException e) { + System.out.println(e.getSQLState()); + System.out.println(e.getMessage()); + if (e.getSQLState().equals("23000")) { + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), + "This user already have a request"); + } else { + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), + "Something went wrong, please try again later"); + } + } catch (Exception e) { System.out.println(e.getMessage()); new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), "Something went wrong, please try again later"); @@ -44,21 +86,147 @@ public class AccountVerificationRequestServiceImpl extends BaseService implement ServiceResponse<AccountVerificationRequest> response = new ServiceResponse<>(); response.setStatus(HTTPStatusCode.OK.getCodeStr()); - response.setMessage("Report successfully created"); + response.setMessage("Account verification request successfully created"); response.setData(lru); - + return response; } - public ServiceResponse acceptAccountVerificationRequest(int userId) throws SOAPFaultException { - return null; + public ServiceResponse<AccountVerificationRequest> acceptAccountVerificationRequest(String userId) + throws SOAPFaultException { + List<AccountVerificationRequest> lru = new ArrayList<>(); + try { + accountVerificationServiceValidation.validateAcceptVerificationRequest(userId); + AccountVerificationRequest accountVerificationRequest = new AccountVerificationRequest(); + accountVerificationRequest.setUserId(userId); + accountVerificationRequest.setStatus("ACCEPTED"); + + int res = accountVerificationRepository.update(accountVerificationRequest); + if (res == 0) { + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), + "This user doesn't have a request or already verified"); + } + } catch (ValidationException e) { + System.out.println(e.getMessage()); + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), e.getMessage()); + } catch (SQLException e) { + System.out.println(e.getSQLState()); + System.out.println(e.getMessage()); + if (e.getSQLState().equals("23000")) { + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), + "This user already have a request"); + } else { + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), + "Something went wrong, please try again later"); + } + } catch (SOAPFaultException e) { + if (e.getFault().getFaultCode().equals(HTTPStatusCode.BAD_REQUEST.getCodeStr())) { + throw e; + } + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), + "Something went wrong, please try again later"); + } catch (Exception e) { + System.out.println(e.getMessage()); + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), + "Something went wrong, please try again later"); + } + + ServiceResponse<AccountVerificationRequest> response = new ServiceResponse<>(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Account verification request with user ID " + userId + " successfully accepted"); + response.setData(lru); + + return response; } - public ServiceResponse rejectAccountVerificationRequest(int userId) throws SOAPFaultException { - return null; + public ServiceResponse<AccountVerificationRequest> rejectAccountVerificationRequest(String userId) + throws SOAPFaultException { + List<AccountVerificationRequest> lru = new ArrayList<>(); + try { + accountVerificationServiceValidation.validateRejectVerificationRequest(userId); + AccountVerificationRequest accountVerificationRequest = new AccountVerificationRequest(); + accountVerificationRequest.setUserId(userId); + accountVerificationRequest.setStatus("REJECTED"); + + int res = accountVerificationRepository.update(accountVerificationRequest); + if (res == 0) { + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), + "This user doesn't have a request or already verified"); + } + } catch (ValidationException e) { + System.out.println(e.getMessage()); + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), e.getMessage()); + } catch (SQLException e) { + System.out.println(e.getSQLState()); + System.out.println(e.getMessage()); + if (e.getSQLState().equals("23000")) { + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), + "This user already have a request"); + } else { + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), + "Something went wrong, please try again later"); + } + } catch (SOAPFaultException e) { + if (e.getFault().getFaultCode().equals(HTTPStatusCode.BAD_REQUEST.getCodeStr())) { + throw e; + } + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), + "Something went wrong, please try again later"); + } catch (Exception e) { + System.out.println(e.getMessage()); + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), + "Something went wrong, please try again later"); + } + + ServiceResponse<AccountVerificationRequest> response = new ServiceResponse<>(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Account verification request with user ID " + userId + " successfully rejected"); + response.setData(lru); + + return response; } - public ServiceResponse deleteAccountVerificationRequest(int requestId) throws SOAPFaultException { - return null; + public ServiceResponse<AccountVerificationRequest> deleteAccountVerificationRequest(int requestId) + throws SOAPFaultException { + List<AccountVerificationRequest> lru = new ArrayList<>(); + try { + accountVerificationServiceValidation.validateDeleteVerificationRequest(requestId); + int res = accountVerificationRepository.delete(requestId); + + if (res == 0) { + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), + "This request doesn't exist"); + } + } catch (ValidationException e) { + System.out.println(e.getMessage()); + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), e.getMessage()); + } catch (SQLException e) { + System.out.println(e.getSQLState()); + System.out.println(e.getMessage()); + if (e.getSQLState().equals("23000")) { + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), + "This user already have a request"); + } else { + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), + "Something went wrong, please try again later"); + } + } catch (SOAPFaultException e) { + if (e.getFault().getFaultCode().equals(HTTPStatusCode.BAD_REQUEST.getCodeStr())) { + throw e; + } + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), + "Something went wrong, please try again later"); + } catch (Exception e) { + System.out.println(e.getMessage()); + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), + "Something went wrong, please try again later"); + } + + ServiceResponse<AccountVerificationRequest> response = new ServiceResponse<>(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Account verification request with ID " + requestId + " successfully deleted"); + response.setData(lru); + + return response; } } diff --git a/src/main/java/org/soapService/Validations/AccountVerificationRequestValidation.java b/src/main/java/org/soapService/Validations/AccountVerificationRequestValidation.java index 3556edd64ab00a6ac5c7fb94ded30884c856aa6a..e7e812503d4c2ca3fa5eb7fcea73758ea6e65ec6 100644 --- a/src/main/java/org/soapService/Validations/AccountVerificationRequestValidation.java +++ b/src/main/java/org/soapService/Validations/AccountVerificationRequestValidation.java @@ -9,4 +9,25 @@ public class AccountVerificationRequestValidation { throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "User ID is required"); } } + + public void validateDeleteVerificationRequest(int requestId) throws ValidationException { + System.out.print(requestId); + if (requestId <= 0) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Request ID is required"); + } + } + + public void validateAcceptVerificationRequest(String userId) throws ValidationException { + System.out.println(userId); + System.out.println(userId.trim()); + if (userId == null || userId.trim().equals("")) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "User ID is required"); + } + } + + public void validateRejectVerificationRequest(String userId) throws ValidationException { + if (userId == null || userId.trim().equals("")) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "User ID is required"); + } + } }