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/Repository/CatalogReqeustRepository.java b/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java index ad4df469f738bbd91f8e5febbb7c57709ba8891f..86aa6eaca2e0df7da66c9730e66c86824ab63a46 100644 --- a/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java +++ b/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java @@ -18,15 +18,14 @@ 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(title, description, poster, trailer, category, uuid) VALUES (?, ?, ?, ?, ? ,?)"; + String query = "INSERT INTO catalog_requests(uuid, title, description, poster, trailer, category) VALUES (?, ?, ?, ?, ? ,?)"; PreparedStatement ps = conn.prepareStatement(query); - ps.setString(1, data.getTitle()); - ps.setString(2, data.getDescription()); - ps.setString(3, data.getPoster()); - ps.setString(4, data.getTrailer()); - ps.setString(5, data.getCategory()); - ps.setString(6, UUID.randomUUID().toString()); - + ps.setString(1, UUID.randomUUID().toString()); + ps.setString(2, data.getTitle()); + ps.setString(3, data.getDescription()); + ps.setString(4, data.getPoster()); + ps.setString(5, data.getTrailer()); + ps.setString(6, data.getCategory()); return ps.executeUpdate(); } @@ -35,22 +34,24 @@ public class CatalogReqeustRepository implements BaseRepository<CatalogRequest> page = Math.max(page, 0); pageSize = Math.max(pageSize, 1); int offset = pageSize * (page - 1); - String query = "SELECT id, uuid, title, description, poster, trailer, category, created_at, updated_at, count(*) over() AS total_page FROM catalog_requests LIMIT ? OFFSET ?"; + String query = "SELECT id, uuid, title, description, poster, trailer, category, created_at, updated_at FROM catalog_requests LIMIT ? OFFSET ?"; + String totalPageQuery = "SELECT COUNT(*) AS total_page FROM catalog_requests"; PreparedStatement ps = conn.prepareStatement(query); + PreparedStatement totalPagePs = conn.prepareStatement(totalPageQuery); ps.setInt(1, pageSize); ps.setInt(2, offset); ResultSet rs = ps.executeQuery(); - List<CatalogRequest> rows = new ArrayList<>(); + ResultSet totalPageRs = totalPagePs.executeQuery(); int totalPage = 0; + while (totalPageRs.next()) { + totalPage = totalPageRs.getInt(1) / pageSize + 1; + } + + List<CatalogRequest> rows = new ArrayList<>(); while (rs.next()) { System.out.println(rs.getString(1)); - System.out.println(rs.getString(10)); - - if (totalPage == 0) { - totalPage = rs.getInt(10); - } CatalogRequest row = new CatalogRequest(); row.setId(rs.getInt(1)); @@ -102,7 +103,7 @@ public class CatalogReqeustRepository implements BaseRepository<CatalogRequest> return rows.get(0); } - public int update(CatalogRequest id) throws SQLException { + public int update(CatalogRequest data) throws SQLException { return 0; } diff --git a/src/main/java/org/soapService/Services/CatalogRequestService.java b/src/main/java/org/soapService/Services/CatalogRequestService.java index a6e6bff73592234a299082e9fb4fbe13e15be113..9cc9bf634d82201689d34605cee3aa6e8c11e2bc 100644 --- a/src/main/java/org/soapService/Services/CatalogRequestService.java +++ b/src/main/java/org/soapService/Services/CatalogRequestService.java @@ -16,7 +16,8 @@ import javax.xml.ws.soap.SOAPFaultException; public interface CatalogRequestService { @WebMethod(operationName = "CatalogCreateRequest") @RequestWrapper(className = "CatalogRequestService.CatalogCreateRequest") - public ServiceResponse createCatalogRequest(@WebParam(name = "title") String title, + public ServiceResponse createCatalogRequest(@WebParam(name = "uuid") String uuid, + @WebParam(name = "title") String title, @WebParam(name = "description") String description, @WebParam(name = "poster") String poster, @WebParam(name = "trailer") String trailer, @@ -24,7 +25,7 @@ public interface CatalogRequestService { @WebMethod(operationName = "GetRequests") @RequestWrapper(className = "CatalogRequestService.GetRequests") - public ServiceResponse<CatalogRequest> getCatalogRequests() throws SOAPFaultException; + public ServiceResponse<CatalogRequest> getCatalogRequests(@WebParam(name = "page") Integer page, @WebParam(name = "pageSize") Integer pageSize) throws SOAPFaultException; @WebMethod(operationName = "GetRequest") @RequestWrapper(className = "CatalogRequestService.GetRequest") @@ -32,11 +33,11 @@ public interface CatalogRequestService { @WebMethod(operationName = "AcceptRequest") @RequestWrapper(className = "CatalogRequestService.AcceptRequest") - public ServiceResponse acceptCatalogRequest(@WebParam(name = "requestId") int requestId) throws SOAPFaultException; + public ServiceResponse acceptCatalogRequest(@WebParam(name = "uuid") String uuid) throws SOAPFaultException; @WebMethod(operationName = "RejectRequest") @RequestWrapper(className = "CatalogRequestService.RejectRequest") - public ServiceResponse rejectCatalogRequest(@WebParam(name = "requestId") int requestId) throws SOAPFaultException; + public ServiceResponse rejectCatalogRequest(@WebParam(name = "uuid") String uuid) throws SOAPFaultException; @WebMethod(operationName = "DeleteRequest") @RequestWrapper(className = "CatalogRequestService.DeleteRequest") diff --git a/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java b/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java index 6f87ee3cca9cface0a962f808bd1b77c6a909b8b..936c124c85a0a125c0723c7429bf6b66e9a73fc6 100644 --- a/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java +++ b/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java @@ -3,13 +3,20 @@ 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; @@ -18,14 +25,44 @@ 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; @WebService(endpointInterface = "org.soapService.Services.CatalogRequestService") public class CatalogRequestServiceImpl extends BaseService implements CatalogRequestService { - public ServiceResponse createCatalogRequest(String title, String description, String poster, String trailer, String category) throws SOAPFaultException { - return null; - } - public ServiceResponse<CatalogRequest> getCatalogRequests() throws SOAPFaultException { + private static CatalogReqeustRepository catalogRepository = new CatalogReqeustRepository(); + private static CatalogValidation catalogValidation = new CatalogValidation(); + + public ServiceResponse<CatalogRequest> getCatalogRequests(Integer page, Integer pageSize) throws SOAPFaultException { + if (page == null) { + page = 1; + } + if (pageSize == null) { + pageSize = 20; + } + + List<GetAllResponse<CatalogRequest>> lcr = new ArrayList<>(); + try { + lcr.add(catalogRepository.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<CatalogRequest>> response = new ServiceResponse<>(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Successfully get all catalog requests"); + response.setData(lcr); + + lcr.forEach((item) -> { + item.getData().forEach((data) -> { + System.out.println(data.getId()); + }); + }); + return null; } @@ -37,51 +74,188 @@ public class CatalogRequestServiceImpl extends BaseService implements CatalogReq return null; } - public ServiceResponse acceptCatalogRequest(int requestId) throws SOAPFaultException { - // Example of sending http request to php + + public ServiceResponse<CatalogRequest> createCatalogRequest(String uuid, String title, String description, String poster, String trailer, String category) + throws SOAPFaultException { + List<CatalogRequest> lcr = new ArrayList<>(); try { - HttpURLConnection conn = HTTPRequest.getConnection("/catalog", HTTPRequestMethod.POST); - conn.setRequestProperty("Content-Type", "application/json"); - conn.setRequestProperty("Accept", "application/json"); - conn.setDoOutput(true); + catalogValidation.validateCreateCatalogRequest(uuid, title, description, poster, trailer, category); + + CatalogRequest catalogRequest = new CatalogRequest(); + catalogRequest.setUuid(uuid); + catalogRequest.setTitle(title); + catalogRequest.setDescription(description); + catalogRequest.setPoster(poster); + catalogRequest.setTrailer(trailer); + catalogRequest.setCategory(category); - Gson gson = new Gson(); - String requestBody = gson.toJson(new AcceptRequest("title", "description")); + lcr.add(catalogRequest); - try (OutputStream os = conn.getOutputStream()) { - byte[] input = requestBody.getBytes("utf-8"); - os.write(input, 0, input.length); + catalogRepository.add(catalogRequest); + + + } 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"); + } + + ServiceResponse<CatalogRequest> response = new ServiceResponse<>(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Report successfully created"); + response.setData(lcr); - int responseCode = conn.getResponseCode(); - if (responseCode == HttpURLConnection.HTTP_OK) { - BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String inputLine; - StringBuffer response = new StringBuffer(); + return response; - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } + } - in.close(); - System.out.println(response.toString()); - } else { - System.out.println(responseCode); + public ServiceResponse acceptCatalogRequest(String uuid) throws SOAPFaultException { + List<CatalogRequest> lcr = new ArrayList<>(); + try { + catalogValidation.validateAcceptCatalogRequest(uuid); + CatalogRequest catalogRequest = new CatalogRequest(); + catalogRequest.setUuid(uuid); + +// int res = catalogRepository.update(catalogRequest); +// if (res == 0) { +// new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), +// "This catalog doesn't have a request"); +// } + + } 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 catalog 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) { - new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), e.getMessage()); + System.out.println(e.getMessage()); + new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), + "Something went wrong, please try again later"); } - return null; + ServiceResponse<CatalogRequest> response = new ServiceResponse<>(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Catalog request with UUID " + uuid + " successfully accepted"); + response.setData(lcr); + + return response; } - public ServiceResponse rejectCatalogRequest(int requestId) throws SOAPFaultException { - return null; + public ServiceResponse rejectCatalogRequest(String uuid) throws SOAPFaultException { + List<CatalogRequest> lcr = new ArrayList<>(); + try { + catalogValidation.validateRejectCatalogRequest(uuid); + CatalogRequest catalogRequest = new CatalogRequest(); + catalogRequest.setUuid(uuid); + +// int res = catalogRepository.update(catalogRequest); +// if (res == 0) { +// new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), +// "This catalog doesn't have a request"); +// } + + } 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 catalog 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<CatalogRequest> response = new ServiceResponse<>(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Catalog request with UUID " + uuid + " successfully rejected"); + response.setData(lcr); + + return response; } public ServiceResponse deleteCatalogRequest(int requestId) throws SOAPFaultException { - System.out.println(requestId); - return null; + List<CatalogRequest> lcr = new ArrayList<>(); + try { + catalogValidation.validateDeleteCatalogRequest(requestId); + int res = catalogRepository.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 catalog already has 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<CatalogRequest> response = new ServiceResponse<>(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Catalog request with ID " + requestId + " successfully deleted"); + response.setData(lcr); + + return response; } public ServiceResponse testUpload(DataHandler dataHandler) throws SOAPFaultException { diff --git a/src/main/java/org/soapService/Validations/CatalogValidation.java b/src/main/java/org/soapService/Validations/CatalogValidation.java new file mode 100644 index 0000000000000000000000000000000000000000..9b07e15955b412bfba7b82defa74bde7a7656619 --- /dev/null +++ b/src/main/java/org/soapService/Validations/CatalogValidation.java @@ -0,0 +1,49 @@ +package org.soapService.Validations; + +import org.soapService.Common.HTTPStatusCode; +import org.soapService.Exceptions.ValidationException; + +public class CatalogValidation { + public void validateCreateCatalogRequest(String uuid, String title, String description, String poster, String trailer, String category) throws ValidationException { + if (uuid == null || uuid.trim().isEmpty()) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Title is required"); + } + if (title == null || title.trim().isEmpty()) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Title is required"); + } + if (description == null || description.trim().isEmpty()) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Description is required"); + } + if (poster == null || poster.trim().isEmpty()) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Poster is required"); + } + if (trailer == null || trailer.trim().isEmpty()) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Trailer is required"); + } + if (category == null || category.trim().isEmpty()) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Category is required"); + } + } + + public void validateAcceptCatalogRequest(String uuid) throws ValidationException { + System.out.println(uuid); + System.out.println(uuid.trim()); + if (uuid == null || uuid.trim().equals("")) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "User ID is required"); + } + } + + public void validateRejectCatalogRequest(String uuid) throws ValidationException { + if (uuid == null || uuid.trim().equals("")) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "User ID is required"); + } + } + + public void validateDeleteCatalogRequest(int requestId) throws ValidationException { + System.out.print(requestId); + if (requestId <= 0) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Request ID is required"); + } + } + +}