diff --git a/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java b/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java index d423ddb9dab27ac9795fe8837d93a196388ecdcb..fc38b7d3b2887935e51eacdb4f6c7a6604cd2897 100644 --- a/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java +++ b/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java @@ -101,10 +101,10 @@ public class AccountVerificationRequestRepository implements BaseRepository<Acco } - public int delete(int id) throws SQLException { + public int delete(AccountVerificationRequest data) throws SQLException { String query = "DELETE FROM account_verification_requests WHERE id = ?"; PreparedStatement ps = conn.prepareStatement(query); - ps.setInt(1, id); + ps.setInt(1, data.getId()); return ps.executeUpdate(); } } diff --git a/src/main/java/org/soapService/Repository/BaseRepository.java b/src/main/java/org/soapService/Repository/BaseRepository.java index f0f6a7e652b0945e52c593f67ac049229003c765..2dceb6aa3a13dcc9d28d9006d12fc3ab0cfc70f0 100644 --- a/src/main/java/org/soapService/Repository/BaseRepository.java +++ b/src/main/java/org/soapService/Repository/BaseRepository.java @@ -1,9 +1,9 @@ package org.soapService.Repository; -import java.sql.SQLException; - import org.soapService.Domain.GetAllResponse; +import java.sql.SQLException; + public interface BaseRepository<T> { public int add(T data) throws SQLException; @@ -15,6 +15,6 @@ public interface BaseRepository<T> { public void deleteAll() throws SQLException; - public int delete(int id) throws SQLException; + public int delete(T data) throws SQLException; } diff --git a/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java b/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java index 86aa6eaca2e0df7da66c9730e66c86824ab63a46..16bcae8560370e91231decf59941bfab1e386c06 100644 --- a/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java +++ b/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java @@ -1,7 +1,6 @@ package org.soapService.Repository; import org.soapService.Config.Database; -import org.soapService.Domain.AccountVerificationRequest; import org.soapService.Domain.CatalogRequest; import org.soapService.Domain.GetAllResponse; @@ -17,6 +16,7 @@ import java.util.UUID; public class CatalogReqeustRepository implements BaseRepository<CatalogRequest> { private static Connection conn = Database.getConnection(); + public int add(CatalogRequest data) throws SQLException { String query = "INSERT INTO catalog_requests(uuid, title, description, poster, trailer, category) VALUES (?, ?, ?, ?, ? ,?)"; PreparedStatement ps = conn.prepareStatement(query); @@ -111,10 +111,10 @@ public class CatalogReqeustRepository implements BaseRepository<CatalogRequest> } - public int delete(int id) throws SQLException { + public int delete(CatalogRequest data) throws SQLException { String query = "DELETE FROM catalog_requests WHERE id = ?"; PreparedStatement ps = conn.prepareStatement(query); - ps.setInt(1, id); + ps.setInt(1, data.getId()); return ps.executeUpdate(); } } diff --git a/src/main/java/org/soapService/Repository/LogRepository.java b/src/main/java/org/soapService/Repository/LogRepository.java index 1aceb7067cdc6d0f58d8724392a9cee7efc41401..e783401e0a375fb4fd94cc2e5eb55f62ecfe7728 100644 --- a/src/main/java/org/soapService/Repository/LogRepository.java +++ b/src/main/java/org/soapService/Repository/LogRepository.java @@ -7,7 +7,6 @@ import org.soapService.Domain.Log; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; -import java.util.List; public class LogRepository implements BaseRepository<Log> { @@ -39,7 +38,7 @@ public class LogRepository implements BaseRepository<Log> { } - public int delete(int id) throws SQLException { + public int delete(Log data) 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 b5c8507d780d5f077ee043bbd65ab56ae176bf4a..865a48185ee11c4438236dc764e47d0a137bdd29 100644 --- a/src/main/java/org/soapService/Repository/ReportUserRepository.java +++ b/src/main/java/org/soapService/Repository/ReportUserRepository.java @@ -6,7 +6,9 @@ import org.soapService.Domain.ReportUser; 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; @@ -25,7 +27,44 @@ public class ReportUserRepository implements BaseRepository<ReportUser> { } public GetAllResponse<ReportUser> getAll(int page, int pageSize) throws SQLException { - return null; + page = Math.max(page, 0); + pageSize = Math.max(pageSize, 1); + int offset = pageSize * (page - 1); + String query = "SELECT * FROM report_users ORDER BY created_at DESC LIMIT ? OFFSET ?"; + String totalPageQuery = "SELECT COUNT(*) AS total_page FROM report_users"; + PreparedStatement ps = conn.prepareStatement(query); + PreparedStatement totalPagePs = conn.prepareStatement(totalPageQuery); + ps.setInt(1, pageSize); + ps.setInt(2, 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<ReportUser> rows = new ArrayList<>(); + + while (rs.next()) { + ReportUser row = new ReportUser(); + row.setId(rs.getInt(1)); + row.setUuid(rs.getString(2)); + row.setContent(rs.getString(3)); + row.setReporterId(rs.getString(4)); + row.setReportedId(rs.getString(5)); + row.setCreatedAt(rs.getString(6)); + row.setUpdatedAt(rs.getString(7)); + + rows.add(row); + } + + GetAllResponse<ReportUser> response = new GetAllResponse<>(); + response.setPage(page); + response.setTotalPage(totalPage); + response.setData(rows); + + return response; } public ReportUser getById(int id) throws SQLException { @@ -40,7 +79,16 @@ public class ReportUserRepository implements BaseRepository<ReportUser> { } - public int delete(int id) throws SQLException { - return 0; + public int delete(ReportUser data) throws SQLException { + String query = "DELETE FROM report_users WHERE uuid = ?"; + String identifier = data.getUuid(); + if (data.getReportedId() != null) { + query = "DELETE FROM report_users WHERE reported_id = ?"; + identifier = data.getReportedId(); + } + + PreparedStatement ps = conn.prepareStatement(query); + ps.setString(1, identifier); + return ps.executeUpdate(); } } diff --git a/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java index eb15d1d87146cfec7528393324248b987ac73b29..1132e84211d61eb9d3128e1794d8497105849f25 100644 --- a/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java +++ b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java @@ -21,7 +21,7 @@ public class AccountVerificationRequestServiceImpl extends BaseService implement private static AccountVerificationRequestValidation accountVerificationServiceValidation = new AccountVerificationRequestValidation(); public ServiceResponse<GetAllResponse<AccountVerificationRequest>> getAccountVerificationRequests(Integer page, - Integer pageSize) + Integer pageSize) throws SOAPFaultException { if (page == null) { page = 1; @@ -191,7 +191,11 @@ public class AccountVerificationRequestServiceImpl extends BaseService implement List<AccountVerificationRequest> lru = new ArrayList<>(); try { accountVerificationServiceValidation.validateDeleteVerificationRequest(requestId); - int res = accountVerificationRepository.delete(requestId); + + AccountVerificationRequest accountVerificationRequest = new AccountVerificationRequest(); + accountVerificationRequest.setId(requestId); + + int res = accountVerificationRepository.delete(accountVerificationRequest); if (res == 0) { new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), diff --git a/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java b/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java index 936c124c85a0a125c0723c7429bf6b66e9a73fc6..425d711c26f694deea075aa90043d4e271391116 100644 --- a/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java +++ b/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java @@ -1,30 +1,19 @@ package org.soapService.Services; -import com.google.gson.Gson; import org.soapService.Common.HTTPStatusCode; import org.soapService.Common.ServiceResponse; -import org.soapService.Domain.AccountVerificationRequest; import org.soapService.Domain.CatalogRequest; import org.soapService.Domain.GetAllResponse; import org.soapService.Exceptions.RequestException; import org.soapService.Exceptions.ValidationException; -import org.soapService.Models.CatalogRequest.AcceptRequest; -import org.soapService.Repository.AccountVerificationRequestRepository; import org.soapService.Repository.CatalogReqeustRepository; import org.soapService.Utils.FileType; import org.soapService.Utils.FileUploader; -import org.soapService.Utils.HTTPRequest; -import org.soapService.Utils.HTTPRequestMethod; -import org.soapService.Validations.AccountVerificationRequestValidation; import org.soapService.Validations.CatalogValidation; import javax.activation.DataHandler; import javax.jws.WebService; import javax.xml.ws.soap.SOAPFaultException; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.HttpURLConnection; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -219,7 +208,11 @@ public class CatalogRequestServiceImpl extends BaseService implements CatalogReq List<CatalogRequest> lcr = new ArrayList<>(); try { catalogValidation.validateDeleteCatalogRequest(requestId); - int res = catalogRepository.delete(requestId); + + CatalogRequest catalogRequest = new CatalogRequest(); + catalogRequest.setId(requestId); + + int res = catalogRepository.delete(catalogRequest); if (res == 0) { new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), diff --git a/src/main/java/org/soapService/Services/ReportUserService.java b/src/main/java/org/soapService/Services/ReportUserService.java index cfd8fb4dfbad9d51ee184f38e4a3518243c301e6..398220354c6852c3ef797f6c2b429e6e5afb1806 100644 --- a/src/main/java/org/soapService/Services/ReportUserService.java +++ b/src/main/java/org/soapService/Services/ReportUserService.java @@ -1,6 +1,7 @@ package org.soapService.Services; import org.soapService.Common.ServiceResponse; +import org.soapService.Domain.GetAllResponse; import org.soapService.Domain.ReportUser; import javax.jws.HandlerChain; @@ -17,19 +18,20 @@ import javax.xml.ws.soap.SOAPFaultException; public interface ReportUserService { @WebMethod(operationName = "CreateReport") @RequestWrapper(className = "ReportUserService.CreateReport") - public ServiceResponse<ReportUser> createReportUser(@WebParam(name = "content") String content, - @WebParam(name = "reportedId") String reportedId, - @WebParam(name = "reporterId") String reporterId) throws SOAPFaultException; + public ServiceResponse createReportUser(@WebParam(name = "content") String content, + @WebParam(name = "reportedId") String reportedId, + @WebParam(name = "reporterId") String reporterId) throws SOAPFaultException; @WebMethod(operationName = "GetReportedUsers") @RequestWrapper(className = "ReportUserService.GetReportedUsers") - public ServiceResponse<ReportUser> getReportedUsers() throws SOAPFaultException; + public ServiceResponse<GetAllResponse<ReportUser>> getReportedUsers(@WebParam(name = "page") Integer page, + @WebParam(name = "perPage") Integer perPage) throws SOAPFaultException; @WebMethod(operationName = "BlockUser") @RequestWrapper(className = "ReportUserService.BlockUser") - public ServiceResponse blockUser(@WebParam(name = "userId") int userId) throws SOAPFaultException; + public ServiceResponse blockUser(@WebParam(name = "reportedId") String reportedId) throws SOAPFaultException; @WebMethod(operationName = "DeleteReport") @RequestWrapper(className = "ReportUserService.DeleteReport") - public ServiceResponse deleteReportUser(@WebParam(name = "reportId") int reportId) throws SOAPFaultException; + public ServiceResponse deleteReportUser(@WebParam(name = "reportId") String reportId) throws SOAPFaultException; } diff --git a/src/main/java/org/soapService/Services/ReportUserServiceImpl.java b/src/main/java/org/soapService/Services/ReportUserServiceImpl.java index d6cfa5913241c5be06531b8b62a87f0faacc872a..ded0449dd43f53300952b67f9451429466242621 100644 --- a/src/main/java/org/soapService/Services/ReportUserServiceImpl.java +++ b/src/main/java/org/soapService/Services/ReportUserServiceImpl.java @@ -2,6 +2,7 @@ package org.soapService.Services; import org.soapService.Common.HTTPStatusCode; import org.soapService.Common.ServiceResponse; +import org.soapService.Domain.GetAllResponse; import org.soapService.Domain.ReportUser; import org.soapService.Exceptions.RequestException; import org.soapService.Exceptions.ValidationException; @@ -10,6 +11,7 @@ import org.soapService.Validations.ReportUserServiceValidation; import javax.jws.WebService; import javax.xml.ws.soap.SOAPFaultException; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -18,8 +20,7 @@ public class ReportUserServiceImpl extends BaseService implements ReportUserServ private static ReportUserRepository reportUserRepository = new ReportUserRepository(); private static ReportUserServiceValidation reportUserServiceValidation = new ReportUserServiceValidation(); - public ServiceResponse<ReportUser> createReportUser(String content, String reportedId, String reporterId) throws SOAPFaultException { - List<ReportUser> lru = new ArrayList<>(); + public ServiceResponse createReportUser(String content, String reportedId, String reporterId) throws SOAPFaultException { try { reportUserServiceValidation.validateCreateReport(content, reportedId, reporterId); @@ -28,9 +29,6 @@ public class ReportUserServiceImpl extends BaseService implements ReportUserServ reportUser.setReportedId(reportedId); reportUser.setReporterId(reporterId); - lru.add(reportUser); - lru.add(reportUser); - reportUserRepository.add(reportUser); } catch (ValidationException e) { System.out.println(e.getMessage()); @@ -43,23 +41,110 @@ public class ReportUserServiceImpl extends BaseService implements ReportUserServ ServiceResponse<ReportUser> response = new ServiceResponse<>(); response.setStatus(HTTPStatusCode.OK.getCodeStr()); response.setMessage("Report successfully created"); - response.setData(lru); return response; } - public ServiceResponse<ReportUser> getReportedUsers() throws SOAPFaultException { - return null; + public ServiceResponse<GetAllResponse<ReportUser>> getReportedUsers(Integer page, Integer pageSize) throws SOAPFaultException { + if (page == null) { + page = 1; + } + if (pageSize == null) { + pageSize = 20; + } + + List<GetAllResponse<ReportUser>> lru = new ArrayList<>(); + try { + lru.add(reportUserRepository.getAll(page, pageSize)); + } catch (Exception e) { + e.printStackTrace(); + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), "Something went wrong, please try again later"); + } + + ServiceResponse<GetAllResponse<ReportUser>> response = new ServiceResponse<>(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Success"); + response.setData(lru); + + return response; } - public ServiceResponse blockUser(int userId) throws SOAPFaultException { - return null; + public ServiceResponse blockUser(String reportedId) throws SOAPFaultException { + try { + reportUserServiceValidation.validateBlockUser(reportedId); + + ReportUser reportUser = new ReportUser(); + reportUser.setReportedId(reportedId); + + // delete all report to the user + int res = reportUserRepository.delete(reportUser); + + if (res == 0) { + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "The report with following User ID doesn't exist"); + } + } catch (ValidationException e) { + e.printStackTrace(); + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), e.getMessage()); + } catch (SQLException e) { + e.printStackTrace(); + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), "Something weneg 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 response = new ServiceResponse(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Success"); + + return response; } - public ServiceResponse deleteReportUser(int reportId) throws SOAPFaultException { - return null; + public ServiceResponse deleteReportUser(String reportId) throws SOAPFaultException { + try { + reportUserServiceValidation.validateDeleteReportUser(reportId); + + ReportUser reportUser = new ReportUser(); + reportUser.setUuid(reportId); + + int res = reportUserRepository.delete(reportUser); + if (res == 0) { + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "This report doesn't exist"); + } + } catch (ValidationException e) { + e.printStackTrace(); + new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), e.getMessage()); + } catch (SQLException e) { + e.printStackTrace(); + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), "Something went wrong, please try again later"); + } catch (SOAPFaultException e) { + e.printStackTrace(); + 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) { + e.printStackTrace(); + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), "Something went wrong, please try again later"); + } + + ServiceResponse response = new ServiceResponse(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Success"); + + + return response; } } diff --git a/src/main/java/org/soapService/Validations/ReportUserServiceValidation.java b/src/main/java/org/soapService/Validations/ReportUserServiceValidation.java index 1b95ebc9dc3244821a9ed982087d1ca2f860aa2c..92489c4dc6be770c436e91736198aed62a64ff19 100644 --- a/src/main/java/org/soapService/Validations/ReportUserServiceValidation.java +++ b/src/main/java/org/soapService/Validations/ReportUserServiceValidation.java @@ -5,14 +5,26 @@ import org.soapService.Exceptions.ValidationException; public class ReportUserServiceValidation { public void validateCreateReport(String content, String reportedId, String reporterId) throws ValidationException { - if (content == null || content.trim().equals("")) { + if (content == null || content.trim().isEmpty()) { throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Content is required"); } - if (reportedId == null || reportedId.trim().equals("")) { + if (reportedId == null || reportedId.trim().isEmpty()) { throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Reported ID is required"); } - if (reporterId == null || reporterId.trim().equals("")) { + if (reporterId == null || reporterId.trim().isEmpty()) { throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Reporter ID is required"); } } + + public void validateBlockUser(String reportedId) throws ValidationException { + if (reportedId == null || reportedId.trim().isEmpty()) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Reported ID is required"); + } + } + + public void validateDeleteReportUser(String reportId) throws ValidationException { + if (reportId == null || reportId.trim().isEmpty()) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Report ID is required"); + } + } }