Add search-fast route
This commit is contained in:
@@ -11,7 +11,7 @@ import jakarta.ws.rs.GET;
|
|||||||
import jakarta.ws.rs.Path;
|
import jakarta.ws.rs.Path;
|
||||||
import jakarta.ws.rs.QueryParam;
|
import jakarta.ws.rs.QueryParam;
|
||||||
|
|
||||||
@Path("/search")
|
@Path("/")
|
||||||
public class SearchResource {
|
public class SearchResource {
|
||||||
private final KodikSearchService kodikSearchService;
|
private final KodikSearchService kodikSearchService;
|
||||||
private final KodikResponseConvertService kodikConvertService;
|
private final KodikResponseConvertService kodikConvertService;
|
||||||
@@ -23,10 +23,18 @@ public class SearchResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
|
@Path("/search")
|
||||||
public Uni<SearchResponseDTO> search(@QueryParam("title") String title) {
|
public Uni<SearchResponseDTO> search(@QueryParam("title") String title) {
|
||||||
return kodikSearchService.searchAsync(title)
|
return kodikSearchService.searchAsync(title)
|
||||||
.onItem().ifNotNull()
|
.onItem().ifNotNull()
|
||||||
.transformToUni(response -> kodikConvertService.convertAsync(response));
|
.transformToUni(response -> kodikConvertService.convertAsync(response));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("/search-fast")
|
||||||
|
public SearchResponseDTO searchFast(@QueryParam("title") String title) {
|
||||||
|
var result = kodikSearchService.search(title);
|
||||||
|
return kodikConvertService.convert(result);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.backend.unifier.title.service.service;
|
package com.backend.unifier.title.service.service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import org.mapstruct.factory.Mappers;
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
@@ -18,13 +17,15 @@ public class KodikResponseConvertService {
|
|||||||
|
|
||||||
private final KodikResponseMapper responseMapper = Mappers.getMapper(KodikResponseMapper.class);
|
private final KodikResponseMapper responseMapper = Mappers.getMapper(KodikResponseMapper.class);
|
||||||
private final PosterUrlValidator posterUrlValidator;
|
private final PosterUrlValidator posterUrlValidator;
|
||||||
|
private final PosterUrlNormalizer posterUrlNormalizer;
|
||||||
|
|
||||||
public KodikResponseConvertService(PosterUrlValidator posterUrlValidator) {
|
public KodikResponseConvertService(PosterUrlValidator posterUrlValidator, PosterUrlNormalizer posterUrlNormalizer) {
|
||||||
this.posterUrlValidator = posterUrlValidator;
|
this.posterUrlValidator = posterUrlValidator;
|
||||||
|
this.posterUrlNormalizer = posterUrlNormalizer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SearchResponseDTO convert(KodikResponse response) {
|
public SearchResponseDTO convert(KodikResponse response) {
|
||||||
return responseMapper.toSearchResponseDTO(response);
|
return convertSimple(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@@ -33,7 +34,7 @@ public class KodikResponseConvertService {
|
|||||||
return Uni.createFrom().item(new SearchResponseDTO(List.of()));
|
return Uni.createFrom().item(new SearchResponseDTO(List.of()));
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Uni<SearchEntryDTO>> entries = responseMapper.toSearchResponseDTO(response).result()
|
List<Uni<SearchEntryDTO>> entries = convertSimple(response).result()
|
||||||
.stream()
|
.stream()
|
||||||
.map(this::resolveEntryPosters)
|
.map(this::resolveEntryPosters)
|
||||||
.toList();
|
.toList();
|
||||||
@@ -47,4 +48,17 @@ public class KodikResponseConvertService {
|
|||||||
.map(entry::withPosterURLs)
|
.map(entry::withPosterURLs)
|
||||||
.onFailure().recoverWithItem(entry);
|
.onFailure().recoverWithItem(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SearchResponseDTO convertSimple(KodikResponse response) {
|
||||||
|
return normalizeUrls(responseMapper.toSearchResponseDTO(response));
|
||||||
|
}
|
||||||
|
|
||||||
|
private SearchResponseDTO normalizeUrls(SearchResponseDTO response) {
|
||||||
|
var normalizedResults = response.result().stream()
|
||||||
|
.map(r -> r.withPosterURLs(r.posterURLs().stream()
|
||||||
|
.map(posterUrlNormalizer::normalize)
|
||||||
|
.toList()))
|
||||||
|
.toList();
|
||||||
|
return response.withResult(normalizedResults);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.backend.unifier.title.service.service;
|
||||||
|
|
||||||
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
|
|
||||||
|
@ApplicationScoped
|
||||||
|
public class PosterUrlNormalizer {
|
||||||
|
public String normalize(String url) {
|
||||||
|
if (url == null)
|
||||||
|
return null;
|
||||||
|
if (url.contains("shikimori")) {
|
||||||
|
return url.replaceFirst("https?://[^/]*shikimori[^/]*/", "https://shikimori.io/");
|
||||||
|
}
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,9 +33,8 @@ public class PosterUrlValidator {
|
|||||||
return Uni.createFrom().item(List.of());
|
return Uni.createFrom().item(List.of());
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> normalized = urls.stream().map(this::normalizeDomain).toList();
|
return findFirstReachable(urls, 0)
|
||||||
return findFirstReachable(normalized, 0)
|
.map(firstIndex -> reorderToFront(urls, firstIndex));
|
||||||
.map(firstIndex -> reorderToFront(normalized, firstIndex));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Uni<Integer> findFirstReachable(List<String> urls, int index) {
|
private Uni<Integer> findFirstReachable(List<String> urls, int index) {
|
||||||
@@ -54,13 +53,6 @@ public class PosterUrlValidator {
|
|||||||
return reordered;
|
return reordered;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String normalizeDomain(String url) {
|
|
||||||
if (url == null)
|
|
||||||
return null;
|
|
||||||
return url.contains("shikimori") ? url.replaceFirst("https?://[^/]*shikimori[^/]*/", "https://shikimori.io/")
|
|
||||||
: url;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Uni<Boolean> isReachable(String url) {
|
private Uni<Boolean> isReachable(String url) {
|
||||||
if (url == null || url.isEmpty()) {
|
if (url == null || url.isEmpty()) {
|
||||||
return Uni.createFrom().item(false);
|
return Uni.createFrom().item(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user