Include grouped by translations result
This commit is contained in:
@@ -2,6 +2,7 @@ package com.backend.metadata.kodik.api.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
@@ -25,6 +26,7 @@ public class KodikResponse {
|
||||
@JsonProperty("other_title")
|
||||
public String otherTitle;
|
||||
public Translation translation;
|
||||
public List<Translation> translations;
|
||||
public int year;
|
||||
@JsonProperty("last_season")
|
||||
public int lastSeason;
|
||||
@@ -71,6 +73,40 @@ public class KodikResponse {
|
||||
public int id;
|
||||
public String title;
|
||||
public String type;
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + id;
|
||||
result = prime * result + ((title == null) ? 0 : title.hashCode());
|
||||
result = prime * result + ((type == null) ? 0 : type.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Translation other = (Translation) obj;
|
||||
if (id != other.id)
|
||||
return false;
|
||||
if (title == null) {
|
||||
if (other.title != null)
|
||||
return false;
|
||||
} else if (!title.equals(other.title))
|
||||
return false;
|
||||
if (type == null) {
|
||||
if (other.type != null)
|
||||
return false;
|
||||
} else if (!type.equals(other.type))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package com.backend.metadata.kodik.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.backend.metadata.kodik.api.model.KodikResponse;
|
||||
|
||||
@@ -16,31 +17,34 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
@ApplicationScoped
|
||||
public class KodikSearchFilterService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(KodikSearchFilterService.class);
|
||||
private static final Set<String> ALLOWED_TYPES = Set.of("anime-serial", "anime");
|
||||
|
||||
public List<KodikResponse.Result> filter(List<KodikResponse.Result> target) {
|
||||
return filter(target, new HashSet<>());
|
||||
return filter(target, Collections.emptyList());
|
||||
}
|
||||
|
||||
public List<KodikResponse.Result> filter(List<KodikResponse.Result> target, List<KodikResponse.Result> fullSeen) {
|
||||
Set<String> seenIds = new HashSet<>();
|
||||
for (KodikResponse.Result seen : fullSeen) {
|
||||
seenIds.add(identifier(seen));
|
||||
}
|
||||
return filter(target, seenIds);
|
||||
}
|
||||
Set<String> seenIds = fullSeen.stream()
|
||||
.map(this::identifier)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
public List<KodikResponse.Result> filter(List<KodikResponse.Result> target, Set<String> seenIds) {
|
||||
Objects.requireNonNull(target, "target cannot be null");
|
||||
List<KodikResponse.Result> filteredResults = new ArrayList<>();
|
||||
Map<String, List<KodikResponse.Result>> grouped = Stream.concat(fullSeen.stream(), target.stream())
|
||||
.filter(this::isAllowedType)
|
||||
.collect(Collectors.groupingBy(this::identifier, LinkedHashMap::new, Collectors.toList()));
|
||||
|
||||
for (KodikResponse.Result result : target) {
|
||||
if (isAllowedType(result) && isUnique(result, seenIds)) {
|
||||
filteredResults.add(result);
|
||||
}
|
||||
}
|
||||
return filteredResults;
|
||||
return grouped.entrySet().stream()
|
||||
.filter(e -> !seenIds.contains(e.getKey()))
|
||||
.map(entry -> {
|
||||
List<KodikResponse.Result> group = entry.getValue();
|
||||
KodikResponse.Result first = group.get(0);
|
||||
first.translations = group.stream()
|
||||
.map(r -> r.translation)
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
return first;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private boolean isAllowedType(KodikResponse.Result result) {
|
||||
|
||||
Reference in New Issue
Block a user