Add search-fast route

This commit is contained in:
2026-03-06 16:54:02 +05:00
parent 9cf368291b
commit a2b6bb1ec6
4 changed files with 44 additions and 15 deletions

View File

@@ -11,7 +11,7 @@ import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;
@Path("/search")
@Path("/")
public class SearchResource {
private final KodikSearchService kodikSearchService;
private final KodikResponseConvertService kodikConvertService;
@@ -23,10 +23,18 @@ public class SearchResource {
}
@GET
@Path("/search")
public Uni<SearchResponseDTO> search(@QueryParam("title") String title) {
return kodikSearchService.searchAsync(title)
.onItem().ifNotNull()
.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);
}
}

View File

@@ -1,7 +1,6 @@
package com.backend.unifier.title.service.service;
import java.util.List;
import java.util.stream.Collectors;
import org.mapstruct.factory.Mappers;
@@ -18,13 +17,15 @@ public class KodikResponseConvertService {
private final KodikResponseMapper responseMapper = Mappers.getMapper(KodikResponseMapper.class);
private final PosterUrlValidator posterUrlValidator;
private final PosterUrlNormalizer posterUrlNormalizer;
public KodikResponseConvertService(PosterUrlValidator posterUrlValidator) {
public KodikResponseConvertService(PosterUrlValidator posterUrlValidator, PosterUrlNormalizer posterUrlNormalizer) {
this.posterUrlValidator = posterUrlValidator;
this.posterUrlNormalizer = posterUrlNormalizer;
}
public SearchResponseDTO convert(KodikResponse response) {
return responseMapper.toSearchResponseDTO(response);
return convertSimple(response);
}
@SuppressWarnings("unchecked")
@@ -33,7 +34,7 @@ public class KodikResponseConvertService {
return Uni.createFrom().item(new SearchResponseDTO(List.of()));
}
List<Uni<SearchEntryDTO>> entries = responseMapper.toSearchResponseDTO(response).result()
List<Uni<SearchEntryDTO>> entries = convertSimple(response).result()
.stream()
.map(this::resolveEntryPosters)
.toList();
@@ -47,4 +48,17 @@ public class KodikResponseConvertService {
.map(entry::withPosterURLs)
.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);
}
}

View File

@@ -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;
}
}

View File

@@ -33,9 +33,8 @@ public class PosterUrlValidator {
return Uni.createFrom().item(List.of());
}
List<String> normalized = urls.stream().map(this::normalizeDomain).toList();
return findFirstReachable(normalized, 0)
.map(firstIndex -> reorderToFront(normalized, firstIndex));
return findFirstReachable(urls, 0)
.map(firstIndex -> reorderToFront(urls, firstIndex));
}
private Uni<Integer> findFirstReachable(List<String> urls, int index) {
@@ -54,13 +53,6 @@ public class PosterUrlValidator {
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) {
if (url == null || url.isEmpty()) {
return Uni.createFrom().item(false);