Skip to content

Commit

Permalink
include the 'of' item when searching for dupes
Browse files Browse the repository at this point in the history
  • Loading branch information
barneyb committed Apr 27, 2024
1 parent 562b383 commit d374bc0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
import com.google.common.annotations.VisibleForTesting;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;

@Slf4j
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public class PantryItemSearchRepositoryImpl implements PantryItemSearchRepository {

Expand Down Expand Up @@ -51,6 +55,7 @@ private record IdAndCount(Long id, Long count) {}
// Type shenanigans to allow Hibernate's reflective instantiation. An empty
// string is used as semaphore for "no value" since the JDBC ResultSet won't
// type a null-valued column, and JPA doesn't have casts like SQL.
@SuppressWarnings("unused")
private record ItemAndCounts(PantryItem item, Long useCount, Long dupeCount) {

ItemAndCounts(PantryItem item, Long useCount, String ignoredDupeCount) {
Expand Down Expand Up @@ -181,11 +186,13 @@ or upper(lbl.name) like upper('%%' || :%1$s || '%%') escape '\\')
if (request.isDuplicateOf()) {
stmt.append(request.isFiltered() ? "and " : "where ")
.append("""
exists (from PantryItemDuplicate
where pantryItem.id = :dupeOf
and duplicate.id = item.id
and not loose
)
(item.id = :dupeOf
or exists (from PantryItemDuplicate
where pantryItem.id = :dupeOf
and duplicate.id = item.id
and not loose
)
)
""", "dupeOf", request.getDuplicateOf());
}
stmt.append("order by ");
Expand Down Expand Up @@ -221,6 +228,21 @@ or upper(lbl.name) like upper('%%' || :%1$s || '%%') escape '\\')
} else {
pantryItems = query(request, stmt, PantryItem.class);
}
if (request.isDuplicateOf()) {
// If the target is in the page being returned, ensure it's first.
Iterator<PantryItem> itr = pantryItems.iterator();
for (int i = 0; itr.hasNext(); i++) {
PantryItem curr = itr.next();
if (request.getDuplicateOf().equals(curr.getId())) {
// Make a new connection, in case Hibernate returns
// something immutable.
pantryItems = new ArrayList<>(pantryItems);
pantryItems.add(0,
pantryItems.remove(i));
break;
}
}
}
return SearchResponse.of(request, pantryItems);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.brennaswitzer.cookbook.repositories;

import com.brennaswitzer.cookbook.domain.PantryItem;
import com.brennaswitzer.cookbook.domain.PantryItem_;
import com.brennaswitzer.cookbook.repositories.impl.PantryItemSearchRequest;
import com.brennaswitzer.cookbook.services.indexing.RefreshPantryItemDuplicates;
import com.brennaswitzer.cookbook.services.indexing.ReindexIngredients;
Expand Down Expand Up @@ -142,7 +143,8 @@ void duplicatesOf_none() {
.duplicateOf(box.salt.getId())
.build());

assertEquals(0, result.size());
assertEquals(1, result.size());
assertEquals("salt", result.getContent().iterator().next().getName());
}

@Test
Expand All @@ -152,10 +154,22 @@ void duplicatesOf_some() {
SearchResponse<PantryItem> result = repo.search(
PantryItemSearchRequest.builder()
.duplicateOf(box.chicken.getId())
.sort(Sort.by(Sort.Direction.DESC, PantryItem_.NAME))
.build());

assertEquals(1, result.size());
assertEquals("chicken thigh", result.getContent().iterator().next().getName());
assertEquals(2, result.size());
assertEquals(List.of("chicken", "chicken thigh"),
result.getContent().stream().map(PantryItem::getName).toList());
}

@Test
void duplicatesOf_unknown() {
SearchResponse<PantryItem> result = repo.search(
PantryItemSearchRequest.builder()
.duplicateOf(-99999L)
.build());

assertEquals(0, result.size());
}

private PantryItem extractItemByName(SearchResponse<PantryItem> result, String name) {
Expand Down

0 comments on commit d374bc0

Please sign in to comment.