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