Skip to content

Commit

Permalink
feat: modify findNearestExpiryItem to return unique value
Browse files Browse the repository at this point in the history
  • Loading branch information
Kang1221 committed Aug 20, 2024
1 parent 8a4cd12 commit 79276cc
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

public interface ItemRepository extends JpaRepository<Item, String> {
@Query("SELECT MAX(CAST(SUBSTRING(i.id, 14, 2) AS integer)) FROM Item i WHERE i.product = :product")
Integer findMaxSequenceNumberByProduct(@Param("product") Product product);

@Query("SELECT i FROM Item i WHERE i.status = 'ON_SALE' AND i.product = :product ORDER BY i.dueDate ASC")
Optional<Item> findNearestExpiryItem(@Param("product") Product product);
List<Item> findNearestExpiryItems(@Param("product") Product product);

default Optional<Item> findNearestExpiryItem(@Param("product") Product product) {
List<Item> items = findNearestExpiryItems(product);
return items.isEmpty() ? Optional.empty() : Optional.of(items.get(0));
}

}

0 comments on commit 79276cc

Please sign in to comment.