diff --git a/CHANGELOG.md b/CHANGELOG.md
index 87220ca53e75c7b21a48f2bb5509f8e2e6142945..168c4dea3982d44d0579c17180108d229dadf972 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,6 @@
+## [1.0.71-RELEASE]
### Added
-- New features that have been added.
+- MYD-628/add pagination, sort and search for sale ssesion
### Changed
- Changes in existing functionality.
diff --git a/src/main/java/com/marketingconfort/mydressin/constants/Paths.java b/src/main/java/com/marketingconfort/mydressin/constants/Paths.java
index ba74ada440d1bcbce8067b03a4df01773f1d19ab..503aacd584365a56bcad191418fb794cb4b9d533 100644
--- a/src/main/java/com/marketingconfort/mydressin/constants/Paths.java
+++ b/src/main/java/com/marketingconfort/mydressin/constants/Paths.java
@@ -27,4 +27,6 @@ public class Paths {
public static final String ADD_TO_CART_TRENDS = "/add-to-cart-trends";
public static final String CART_COUNT = "/cart-count";
public static final String THEORETICAL_AMOUNT = "/theoretical-amount";
+ public static final String SALE_SESSION_PAGE = "/page";
+ public static final String COUNT_TYPE_SALE_SESSION = "/count-type";
}
diff --git a/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java b/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java
index 73842d30e75488f3321e6a4bc88be03cfb5ab5b0..6d60f5bc015fdb102072b73362d1a37d009123c6 100644
--- a/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java
+++ b/src/main/java/com/marketingconfort/mydressin/controllers/SaleSessionController.java
@@ -1,6 +1,7 @@
package com.marketingconfort.mydressin.controllers;
import com.marketingconfort.mydressin.common.cart.models.SaleSession;
+import com.marketingconfort.mydressin.constants.Paths;
import com.marketingconfort.mydressin.dtos.SaleSessionDTO;
import com.marketingconfort.mydressin.dtos.SessionOrderDTO;
import com.marketingconfort.mydressin.dtos.ItemRequestDTO;
@@ -9,12 +10,15 @@ import com.marketingconfort.mydressin.exceptions.SaleSessionExpiredException;
import com.marketingconfort.mydressin.exceptions.SaleSessionNotFoundException;
import com.marketingconfort.mydressin.services.SaleSessionService;
import lombok.AllArgsConstructor;
+import org.springframework.data.domain.Page;
+import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
+import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@@ -22,7 +26,6 @@ import java.util.concurrent.CompletableFuture;
@RestController
@RequestMapping(value = "/api/cart", method = RequestMethod.OPTIONS)
@AllArgsConstructor
-
public class SaleSessionController {
private final SaleSessionService saleSessionService;
@@ -138,5 +141,33 @@ public class SaleSessionController {
SaleSessionDTO updatedSession = saleSessionService.restockSaleSession(sessionId);
return ResponseEntity.ok(updatedSession);
}
+
+ @GetMapping(Paths.SALE_SESSION_PAGE)
+ @PreAuthorize("@securityCustomExpressions.isClientTrusted(#requestAuthorization)")
+ public Page<SaleSessionDTO> getSaleSessionsPage(
+ @RequestParam(required = false) String name,
+ @RequestParam(required = false) String type,
+ @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startDate,
+ @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endDate,
+ @RequestParam(defaultValue = "0") int page,
+ @RequestParam(defaultValue = "10") int size,
+ @RequestParam(defaultValue = "id") String sortBy,
+ @RequestParam(defaultValue = "ASC") String sortDir,
+ @RequestHeader String requestAuthorization) {
+
+ return saleSessionService.getSaleSessionsPage(name, type, startDate, endDate, page, size, sortBy, sortDir);
+ }
+
+ @GetMapping(Paths.COUNT_TYPE_SALE_SESSION)
+ @PreAuthorize("@securityCustomExpressions.isClientTrusted(#requestAuthorization)")
+ public List<String[]> getStatusCount(
+ @RequestParam(required = false) String name,
+ @RequestParam(required = false) String type,
+ @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startDate,
+ @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endDate,
+ @RequestHeader String requestAuthorization) {
+ return saleSessionService.getTypeCount(name, type, startDate, endDate);
+ }
+
}
diff --git a/src/main/java/com/marketingconfort/mydressin/repositories/SaleSessionRepository.java b/src/main/java/com/marketingconfort/mydressin/repositories/SaleSessionRepository.java
index 650fb3fdb28f95338e70a148c4561c42c308b269..84317f3a1d3ae691a00bdabc0a97a19712e27918 100644
--- a/src/main/java/com/marketingconfort/mydressin/repositories/SaleSessionRepository.java
+++ b/src/main/java/com/marketingconfort/mydressin/repositories/SaleSessionRepository.java
@@ -2,11 +2,14 @@ package com.marketingconfort.mydressin.repositories;
import com.marketingconfort.mydressin.common.cart.enumurations.ItemCartStatus;
import com.marketingconfort.mydressin.common.cart.models.SaleSession;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
+import java.util.Date;
import java.util.List;
@Repository
@@ -68,4 +71,26 @@ public interface SaleSessionRepository extends JpaRepository<SaleSession, Long>
"JOIN so.itemCart ic " +
"WHERE ss.id = :sessionId")
Long getTotalPaidProducts(@Param("sessionId") Long sessionId);
+
+ @Query("SELECT sl FROM SaleSession sl WHERE " +
+ "(:name IS NULL OR :name = '' OR LOWER(sl.name) LIKE LOWER(CONCAT('%', :name, '%'))) " +
+ "AND (:type IS NULL OR :type = '' OR LOWER(sl.type) like LOWER(CONCAT('%', :type,'%')))" +
+ "AND ( CAST(:startDate as string) IS NULL OR CAST(sl.startedDate as date) BETWEEN :startDate AND :endDate)" +
+ "AND sl.status != 'DELETED' ")
+ Page<SaleSession> getSaleSessionsPage(@Param("type") String type,
+ @Param("name") String name ,
+ @Param("startDate") Date startDate ,
+ @Param("endDate") Date endDate ,
+ Pageable pageable);
+
+ @Query("SELECT sl.type, COUNT(sl) FROM SaleSession sl WHERE " +
+ "(:name IS NULL OR :name = '' OR LOWER(sl.name) LIKE LOWER(CONCAT('%', :name, '%'))) " +
+ "AND (:type IS NULL OR :type = '' OR LOWER(sl.type) like LOWER(CONCAT('%', :type,'%')))" +
+ "AND ( CAST(:startDate as string) IS NULL OR CAST(sl.startedDate as date) BETWEEN :startDate AND :endDate)" +
+ "AND sl.status != 'DELETED' " +
+ "GROUP BY sl.type")
+ List<Object[]> getTypeCount(@Param("type") String type,
+ @Param("name") String name ,
+ @Param("startDate") Date startDate ,
+ @Param("endDate") Date endDate);
}
diff --git a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java
index 14bff7e90379851c63cf98bbbeae9a821031fa71..3f86b8917530ef170519cd131738b650d87f4256 100644
--- a/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java
+++ b/src/main/java/com/marketingconfort/mydressin/services/SaleSessionService.java
@@ -6,10 +6,12 @@ import com.marketingconfort.mydressin.dtos.SaleSessionDTO;
import com.marketingconfort.mydressin.dtos.SessionOrderDTO;
import com.marketingconfort.mydressin.dtos.ItemRequestDTO;
import com.marketingconfort.mydressin.exceptions.SaleSessionExpiredException;
+import org.springframework.data.domain.Page;
import java.time.LocalDateTime;
+import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@@ -29,4 +31,7 @@ public interface SaleSessionService {
SaleSessionDTO updateSaleSessionDetails(Long sessionId, String newName, String newDescription);
boolean deleteSaleSessionById(Long sessionId);
SaleSessionDTO restockSaleSession(Long sessionId);
+ Page<SaleSessionDTO> getSaleSessionsPage(String name , String type, Date startDate, Date endDate,
+ int page, int size, String sortBy, String order);
+ List<String[]> getTypeCount(String name , String type, Date startDate, Date endDate);
}
diff --git a/src/main/java/com/marketingconfort/mydressin/services/impl/SaleSessionServiceImp.java b/src/main/java/com/marketingconfort/mydressin/services/impl/SaleSessionServiceImp.java
index cc5ef6afe44d2529c664a6f095afebaecb52aae7..86e8f0840a5007c9874f14785ee8f4df661e2aed 100644
--- a/src/main/java/com/marketingconfort/mydressin/services/impl/SaleSessionServiceImp.java
+++ b/src/main/java/com/marketingconfort/mydressin/services/impl/SaleSessionServiceImp.java
@@ -19,16 +19,17 @@ import com.marketingconfort.mydressin.repositories.SaleSessionRepository;
import com.marketingconfort.mydressin.repositories.SessionOrderRepository;
import com.marketingconfort.mydressin.services.*;
import lombok.AllArgsConstructor;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Objects;
-import java.util.Optional;
+import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
@@ -244,4 +245,42 @@ public class SaleSessionServiceImp implements SaleSessionService {
SaleSession updatedSaleSession = saleSessionRepository.save(saleSession);
return saleSessionMapper.toDTO(updatedSaleSession);
}
+
+ @Override
+ public Page<SaleSessionDTO> getSaleSessionsPage(String name, String type, Date startDate, Date endDate, int page, int size, String sortBy, String sortDir) {
+ if("createdAt".equals(sortBy))
+ sortBy = "id";
+ Sort.Direction direction = Sort.Direction.fromString(sortDir);
+ Pageable pageable = PageRequest.of(page, size, Sort.by(direction, sortBy));
+
+ return saleSessionRepository.getSaleSessionsPage(type,name,startDate,endDate,pageable)
+ .map(saleSessionMapper::toDTO);
+ }
+
+ @Override
+ public List<String[]> getTypeCount(String name, String type, Date startDate, Date endDate) {
+ return convertObjectListToStringList(saleSessionRepository.getTypeCount(type, name, startDate, endDate));
+ }
+
+ private List<String[]> convertObjectListToStringList(List<Object[]> objectList) {
+ if (objectList == null) {
+ return new ArrayList<>();
+ }
+
+ List<String[]> stringList = new ArrayList<>();
+
+ for (Object[] objectArray : objectList) {
+ if (objectArray != null && objectArray.length == 2) {
+ String[] stringArray = new String[2];
+
+ stringArray[0] = objectArray[0] != null ? String.valueOf(objectArray[0]) : "";
+ stringArray[1] = objectArray[1] != null ? String.valueOf(objectArray[1]) : "";
+
+ stringList.add(stringArray);
+ }
+ }
+
+ return stringList;
+ }
+
}