feat: add chapter count and manga rating from Jikan
This commit is contained in:
parent
a31584ad35
commit
ed92ad879c
@ -29,7 +29,8 @@ public interface JikanClient {
|
||||
String status,
|
||||
boolean publishing,
|
||||
String synopsis,
|
||||
Double score,
|
||||
float score,
|
||||
int chapters,
|
||||
PublishData published,
|
||||
List<AuthorData> authors,
|
||||
List<GenreData> genres) {
|
||||
|
||||
@ -23,7 +23,8 @@ public record MangaDTO(
|
||||
@NotNull List<String> genres,
|
||||
@NotNull List<String> authors,
|
||||
@NotNull Double score,
|
||||
@NotNull List<MangaProviderDTO> providers) {
|
||||
@NotNull List<MangaProviderDTO> providers,
|
||||
@NotNull Integer chapterCount) {
|
||||
public static MangaDTO from(Manga manga) {
|
||||
return new MangaDTO(
|
||||
manga.getId(),
|
||||
@ -39,8 +40,9 @@ public record MangaDTO(
|
||||
manga.getMangaAuthors().stream()
|
||||
.map(mangaAuthor -> mangaAuthor.getAuthor().getName())
|
||||
.toList(),
|
||||
0.0,
|
||||
manga.getMangaProviders().stream().map(MangaProviderDTO::from).toList());
|
||||
manga.getScore(),
|
||||
manga.getMangaProviders().stream().map(MangaProviderDTO::from).toList(),
|
||||
manga.getChapterCount());
|
||||
}
|
||||
|
||||
public record MangaProviderDTO(
|
||||
|
||||
@ -33,7 +33,7 @@ public record MangaListDTO(
|
||||
manga.getMangaAuthors().stream()
|
||||
.map(mangaAuthor -> mangaAuthor.getAuthor().getName())
|
||||
.toList(),
|
||||
0.0,
|
||||
manga.getScore(),
|
||||
favorite);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,4 +3,8 @@ package com.magamochi.mangamochi.model.dto;
|
||||
import java.util.List;
|
||||
|
||||
public record MangaListFilterDTO(
|
||||
String searchQuery, List<Long> genreIds, List<String> statuses, Boolean userFavorites) {}
|
||||
String searchQuery,
|
||||
List<Long> genreIds,
|
||||
List<String> statuses,
|
||||
Boolean userFavorites,
|
||||
Double score) {}
|
||||
|
||||
@ -60,4 +60,6 @@ public class Manga {
|
||||
|
||||
@OneToMany(mappedBy = "manga")
|
||||
private List<UserFavoriteManga> userFavoriteMangas;
|
||||
|
||||
private Integer chapterCount;
|
||||
}
|
||||
|
||||
@ -54,6 +54,10 @@ public class MangaSpecification {
|
||||
}
|
||||
}
|
||||
|
||||
if (nonNull(filterDTO.score()) && filterDTO.score() > 0) {
|
||||
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("score"), filterDTO.score()));
|
||||
}
|
||||
|
||||
query.distinct(true);
|
||||
|
||||
return criteriaBuilder.and(predicates.toArray(Predicate[]::new));
|
||||
|
||||
@ -10,6 +10,7 @@ import com.magamochi.mangamochi.model.entity.MangaAuthor;
|
||||
import com.magamochi.mangamochi.model.entity.MangaGenre;
|
||||
import com.magamochi.mangamochi.model.repository.*;
|
||||
import com.magamochi.mangamochi.service.ImageService;
|
||||
import com.magamochi.mangamochi.util.DoubleUtil;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
@ -31,12 +32,12 @@ public class UpdateMangaDataTask {
|
||||
private final GenreRepository genreRepository;
|
||||
private final MangaGenreRepository mangaGenreRepository;
|
||||
|
||||
// @Scheduled(fixedDelayString = "1d")
|
||||
// @Scheduled(fixedDelayString = "1d")
|
||||
public void updateMangaData() {
|
||||
var rateLimiter = RateLimiter.create(1);
|
||||
|
||||
var mangas =
|
||||
mangaRepository.findAll().stream().filter(manga -> isNull(manga.getCoverImage())).toList();
|
||||
mangaRepository.findAll().stream().filter(manga -> isNull(manga.getScore())).toList();
|
||||
|
||||
mangas.forEach(
|
||||
manga -> {
|
||||
@ -49,9 +50,10 @@ public class UpdateMangaDataTask {
|
||||
manga.setAlternativeTitles(mangaData.data().title_synonyms());
|
||||
manga.setSynopsis(mangaData.data().synopsis());
|
||||
manga.setStatus(mangaData.data().status());
|
||||
// manga.setScore(mangaData.data().score());
|
||||
manga.setScore(DoubleUtil.round((double) mangaData.data().score(), 2));
|
||||
manga.setPublishedFrom(mangaData.data().published().from());
|
||||
manga.setPublishedTo(mangaData.data().published().to());
|
||||
manga.setChapterCount(mangaData.data().chapters());
|
||||
|
||||
var authors =
|
||||
mangaData.data().authors().stream()
|
||||
@ -118,19 +120,21 @@ public class UpdateMangaDataTask {
|
||||
|
||||
manga.setMangaGenres(mangaGenres);
|
||||
|
||||
var inputStream =
|
||||
new BufferedInputStream(
|
||||
new URL(
|
||||
new URI(mangaData.data().images().jpg().large_image_url())
|
||||
.toASCIIString())
|
||||
.openStream());
|
||||
if (isNull(manga.getCoverImage())) {
|
||||
var inputStream =
|
||||
new BufferedInputStream(
|
||||
new URL(
|
||||
new URI(mangaData.data().images().jpg().large_image_url())
|
||||
.toASCIIString())
|
||||
.openStream());
|
||||
|
||||
var bytes = inputStream.readAllBytes();
|
||||
var bytes = inputStream.readAllBytes();
|
||||
|
||||
inputStream.close();
|
||||
var image = imageService.uploadImage(bytes, "image/jpeg", "cover");
|
||||
inputStream.close();
|
||||
var image = imageService.uploadImage(bytes, "image/jpeg", "cover");
|
||||
|
||||
manga.setCoverImage(image);
|
||||
manga.setCoverImage(image);
|
||||
}
|
||||
|
||||
mangaRepository.save(manga);
|
||||
|
||||
|
||||
18
src/main/java/com/magamochi/mangamochi/util/DoubleUtil.java
Normal file
18
src/main/java/com/magamochi/mangamochi/util/DoubleUtil.java
Normal file
@ -0,0 +1,18 @@
|
||||
package com.magamochi.mangamochi.util;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE)
|
||||
public class DoubleUtil {
|
||||
public static double round(double value, int places) {
|
||||
if (places < 0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
BigDecimal bd = BigDecimal.valueOf(value);
|
||||
bd = bd.setScale(places, RoundingMode.HALF_UP);
|
||||
return bd.doubleValue();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,2 @@
|
||||
ALTER TABLE mangas
|
||||
ADD COLUMN chapter_count INT DEFAULT 0;
|
||||
Loading…
x
Reference in New Issue
Block a user