Checkpoint: Add series and awards metadata
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.adityachandel.booklore.mapper;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.Award;
|
||||
import com.adityachandel.booklore.model.entity.BookAwardEntity;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface AwardMapper {
|
||||
Award toAward(BookAwardEntity awardEntity);
|
||||
List<Award> toAuthorList(List<BookAwardEntity> awardEntities);
|
||||
}
|
||||
+1
-1
@@ -4,7 +4,7 @@ import com.adityachandel.booklore.model.dto.BookMetadata;
|
||||
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
||||
import org.mapstruct.*;
|
||||
|
||||
@Mapper(componentModel = "spring", uses = {AuthorMapper.class, CategoryMapper.class})
|
||||
@Mapper(componentModel = "spring", uses = {AuthorMapper.class, CategoryMapper.class, AwardMapper.class})
|
||||
public interface BookMetadataMapper {
|
||||
|
||||
@Mapping(target = "description", ignore = true)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.adityachandel.booklore.model.dto;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class Award {
|
||||
private String name;
|
||||
private LocalDate awardedAt;
|
||||
private String category;
|
||||
private String designation;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.adityachandel.booklore.model.dto;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@@ -16,6 +17,9 @@ public class BookMetadata {
|
||||
private String publisher;
|
||||
private LocalDate publishedDate;
|
||||
private String description;
|
||||
private String seriesName;
|
||||
private Integer seriesNumber;
|
||||
private Integer seriesTotal;
|
||||
private String isbn13;
|
||||
private String isbn10;
|
||||
private Integer pageCount;
|
||||
@@ -25,4 +29,5 @@ public class BookMetadata {
|
||||
private Instant coverUpdatedOn;
|
||||
private List<Author> authors;
|
||||
private List<Category> categories;
|
||||
private List<Award> awards;
|
||||
}
|
||||
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.adityachandel.booklore.model.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(name = "book_award")
|
||||
public class BookAwardEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "book_id", nullable = false)
|
||||
private BookMetadataEntity book;
|
||||
|
||||
@Column(name = "name", nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(name = "awarded_at", nullable = false)
|
||||
private LocalDate awardedAt;
|
||||
|
||||
@Column(name = "category", nullable = false)
|
||||
private String category;
|
||||
|
||||
@Column(name = "designation", nullable = false)
|
||||
private String designation;
|
||||
}
|
||||
+12
@@ -36,6 +36,15 @@ public class BookMetadataEntity {
|
||||
@Column(name = "description", columnDefinition = "TEXT")
|
||||
private String description;
|
||||
|
||||
@Column(name = "series_name")
|
||||
private String seriesName;
|
||||
|
||||
@Column(name = "series_number")
|
||||
private Integer seriesNumber;
|
||||
|
||||
@Column(name = "series_total")
|
||||
private Integer seriesTotal;
|
||||
|
||||
@Column(name = "isbn_13", length = 13)
|
||||
private String isbn13;
|
||||
|
||||
@@ -80,4 +89,7 @@ public class BookMetadataEntity {
|
||||
inverseJoinColumns = @JoinColumn(name = "category_id")
|
||||
)
|
||||
private List<CategoryEntity> categories;
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY, mappedBy = "book")
|
||||
private List<BookAwardEntity> awards;
|
||||
}
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.adityachandel.booklore.repository;
|
||||
|
||||
import com.adityachandel.booklore.model.entity.BookAwardEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public interface BookAwardRepository extends JpaRepository<BookAwardEntity, Long> {
|
||||
|
||||
|
||||
}
|
||||
+12
@@ -1,7 +1,19 @@
|
||||
package com.adityachandel.booklore.repository;
|
||||
|
||||
import com.adityachandel.booklore.model.entity.BookAwardEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public interface BookMetadataRepository extends JpaRepository<BookMetadataEntity, Long> {
|
||||
|
||||
@Query("SELECT b FROM BookAwardEntity b WHERE b.book.bookId = :bookId AND b.name = :name AND b.category = :category AND b.awardedAt = :awardedAt")
|
||||
BookAwardEntity findAwardByBookIdAndNameAndCategoryAndAwardedAt(@Param("bookId") Long bookId,
|
||||
@Param("name") String name,
|
||||
@Param("category") String category,
|
||||
@Param("awardedAt") LocalDate awardedAt);
|
||||
|
||||
}
|
||||
|
||||
+7
-4
@@ -159,14 +159,13 @@ public class BookMetadataService {
|
||||
}
|
||||
metadata.setThumbnailUrl(resolveFieldAsString(metadataMap, fieldOptions.getCover(), FetchedBookMetadata::getThumbnailUrl));
|
||||
|
||||
|
||||
if(request.getRefreshOptions().getAllP3() != null) {
|
||||
if (request.getRefreshOptions().getAllP3() != null) {
|
||||
setOtherUnspecifiedMetadata(metadataMap, metadata, request.getRefreshOptions().getAllP3());
|
||||
}
|
||||
if(request.getRefreshOptions().getAllP2() != null) {
|
||||
if (request.getRefreshOptions().getAllP2() != null) {
|
||||
setOtherUnspecifiedMetadata(metadataMap, metadata, request.getRefreshOptions().getAllP2());
|
||||
}
|
||||
if(request.getRefreshOptions().getAllP1() != null) {
|
||||
if (request.getRefreshOptions().getAllP1() != null) {
|
||||
setOtherUnspecifiedMetadata(metadataMap, metadata, request.getRefreshOptions().getAllP1());
|
||||
}
|
||||
|
||||
@@ -187,6 +186,10 @@ public class BookMetadataService {
|
||||
metadataCombined.setRating(metadata.getRating() != null ? metadata.getRating() : metadataCombined.getRating());
|
||||
metadataCombined.setRatingCount(metadata.getRatingCount() != null ? metadata.getRatingCount() : metadataCombined.getRatingCount());
|
||||
metadataCombined.setReviewCount(metadata.getReviewCount() != null ? metadata.getReviewCount() : metadataCombined.getReviewCount());
|
||||
metadataCombined.setAwards(metadata.getAwards() != null ? metadata.getAwards() : metadataCombined.getAwards());
|
||||
metadataCombined.setSeriesName(metadata.getSeriesName() != null ? metadata.getSeriesName() : metadataCombined.getSeriesName());
|
||||
metadataCombined.setSeriesNumber(metadata.getSeriesNumber() != null ? metadata.getSeriesNumber() : metadataCombined.getSeriesNumber());
|
||||
metadataCombined.setSeriesTotal(metadata.getSeriesTotal() != null ? metadata.getSeriesTotal() : metadataCombined.getSeriesTotal());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+43
-4
@@ -5,10 +5,7 @@ import com.adityachandel.booklore.mapper.AuthorMapper;
|
||||
import com.adityachandel.booklore.mapper.BookMetadataMapper;
|
||||
import com.adityachandel.booklore.mapper.CategoryMapper;
|
||||
import com.adityachandel.booklore.model.dto.BookMetadata;
|
||||
import com.adityachandel.booklore.model.entity.AuthorEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
||||
import com.adityachandel.booklore.model.entity.CategoryEntity;
|
||||
import com.adityachandel.booklore.model.entity.*;
|
||||
import com.adityachandel.booklore.repository.*;
|
||||
import com.adityachandel.booklore.service.metadata.model.FetchedBookMetadata;
|
||||
import com.adityachandel.booklore.util.FileService;
|
||||
@@ -20,6 +17,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -37,6 +35,7 @@ public class BookMetadataUpdater {
|
||||
private BookMetadataMapper bookMetadataMapper;
|
||||
private AuthorMapper authorMapper;
|
||||
private CategoryMapper categoryMapper;
|
||||
private BookAwardRepository bookAwardRepository;
|
||||
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
@@ -55,6 +54,36 @@ public class BookMetadataUpdater {
|
||||
metadata.setPageCount(newMetadata.getPageCount() != null ? newMetadata.getPageCount() : metadata.getPageCount());
|
||||
metadata.setRating(newMetadata.getRating() != null ? newMetadata.getRating() : metadata.getRating());
|
||||
metadata.setReviewCount(newMetadata.getReviewCount() != null ? newMetadata.getReviewCount() : metadata.getReviewCount());
|
||||
metadata.setSeriesName(newMetadata.getSeriesName() != null ? newMetadata.getSeriesName() : metadata.getSeriesName());
|
||||
metadata.setSeriesNumber(newMetadata.getSeriesNumber() != null ? newMetadata.getSeriesNumber() : metadata.getSeriesNumber());
|
||||
metadata.setSeriesTotal(newMetadata.getSeriesTotal() != null ? newMetadata.getSeriesTotal() : metadata.getSeriesTotal());
|
||||
|
||||
if (newMetadata.getAwards() != null && !newMetadata.getAwards().isEmpty()) {
|
||||
HashSet<BookAwardEntity> newAwards = new HashSet<>();
|
||||
|
||||
newMetadata.getAwards().forEach(award -> {
|
||||
boolean awardExists = bookMetadataRepository.findAwardByBookIdAndNameAndCategoryAndAwardedAt(
|
||||
metadata.getBookId(),
|
||||
award.getName(),
|
||||
award.getCategory(),
|
||||
award.getAwardedAt()) != null;
|
||||
|
||||
if (!awardExists) {
|
||||
BookAwardEntity awardEntity = new BookAwardEntity();
|
||||
awardEntity.setBook(metadata);
|
||||
awardEntity.setName(award.getName());
|
||||
awardEntity.setCategory(award.getCategory());
|
||||
awardEntity.setDesignation(award.getDesignation());
|
||||
awardEntity.setAwardedAt(award.getAwardedAt() != null ? award.getAwardedAt() : Instant.now().atZone(ZoneId.systemDefault()).toLocalDate());
|
||||
|
||||
newAwards.add(awardEntity);
|
||||
}
|
||||
});
|
||||
if(!newAwards.isEmpty()) {
|
||||
metadata.setAwards(new ArrayList<>(newAwards));
|
||||
bookAwardRepository.saveAll(newAwards);
|
||||
}
|
||||
}
|
||||
|
||||
metadata.setAuthors(newMetadata.getAuthors() != null && !newMetadata.getAuthors().isEmpty() ?
|
||||
newMetadata.getAuthors().stream()
|
||||
@@ -117,6 +146,15 @@ public class BookMetadataUpdater {
|
||||
if (updatedMetadata.getPublisher() != null) {
|
||||
metadata.setPublisher(updatedMetadata.getPublisher());
|
||||
}
|
||||
if (updatedMetadata.getSeriesName() != null) {
|
||||
metadata.setSeriesName(updatedMetadata.getSeriesName());
|
||||
}
|
||||
if (updatedMetadata.getSeriesNumber() != null) {
|
||||
metadata.setSeriesNumber(updatedMetadata.getSeriesNumber());
|
||||
}
|
||||
if (updatedMetadata.getSeriesTotal() != null) {
|
||||
metadata.setSeriesTotal(updatedMetadata.getSeriesTotal());
|
||||
}
|
||||
if (updatedMetadata.getPublishedDate() != null) {
|
||||
metadata.setPublishedDate(updatedMetadata.getPublishedDate());
|
||||
}
|
||||
@@ -150,6 +188,7 @@ public class BookMetadataUpdater {
|
||||
if (updatedMetadata.getCategories() != null) {
|
||||
metadata.setCategories(categoryMapper.toCategoryEntities(updatedMetadata.getCategories()));
|
||||
}
|
||||
|
||||
bookRepository.save(bookEntity);
|
||||
return bookMetadataMapper.toBookMetadata(metadata, false);
|
||||
}
|
||||
|
||||
+5
@@ -1,5 +1,6 @@
|
||||
package com.adityachandel.booklore.service.metadata.model;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.Award;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@@ -17,6 +18,9 @@ public class FetchedBookMetadata {
|
||||
private String publisher;
|
||||
private LocalDate publishedDate;
|
||||
private String description;
|
||||
private String seriesName;
|
||||
private Integer seriesNumber;
|
||||
private Integer seriesTotal;
|
||||
private String asin;
|
||||
private String isbn13;
|
||||
private String isbn10;
|
||||
@@ -28,4 +32,5 @@ public class FetchedBookMetadata {
|
||||
private Integer reviewCount;
|
||||
private List<String> authors;
|
||||
private List<String> categories;
|
||||
private List<Award> awards;
|
||||
}
|
||||
+53
@@ -127,6 +127,9 @@ public class AmazonBookParser implements BookParser {
|
||||
.authors(getAuthors(doc).stream().toList())
|
||||
.categories(getBestSellerCategories(doc).stream().toList())
|
||||
.description(cleanDescriptionHtml(getDescription(doc)))
|
||||
.seriesName(getSeriesName(doc))
|
||||
.seriesNumber(getSeriesNumber(doc))
|
||||
.seriesTotal(getSeriesTotal(doc))
|
||||
.isbn13(getIsbn13(doc))
|
||||
.isbn10(getIsbn10(doc))
|
||||
.publisher(getPublisher(doc))
|
||||
@@ -289,6 +292,56 @@ public class AmazonBookParser implements BookParser {
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getSeriesName(Document doc) {
|
||||
try {
|
||||
Element seriesNameElement = doc.selectFirst("#rpi-attribute-book_details-series .rpi-attribute-value a span");
|
||||
if (seriesNameElement != null) {
|
||||
return seriesNameElement.text();
|
||||
} else {
|
||||
log.warn("Error fetching series name: Element not found.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Error fetching series name: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Integer getSeriesNumber(Document doc) {
|
||||
try {
|
||||
Element bookDetailsLabel = doc.selectFirst("#rpi-attribute-book_details-series .rpi-attribute-label span");
|
||||
if (bookDetailsLabel != null) {
|
||||
String bookAndTotal = bookDetailsLabel.text();
|
||||
if (bookAndTotal.matches("Book \\d+ of \\d+")) {
|
||||
String[] parts = bookAndTotal.split(" ");
|
||||
return Integer.parseInt(parts[1]);
|
||||
}
|
||||
} else {
|
||||
log.warn("Error fetching series number: Element not found.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Error fetching series number: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Integer getSeriesTotal(Document doc) {
|
||||
try {
|
||||
Element bookDetailsLabel = doc.selectFirst("#rpi-attribute-book_details-series .rpi-attribute-label span");
|
||||
if (bookDetailsLabel != null) {
|
||||
String bookAndTotal = bookDetailsLabel.text();
|
||||
if (bookAndTotal.matches("Book \\d+ of \\d+")) {
|
||||
String[] parts = bookAndTotal.split(" ");
|
||||
return Integer.parseInt(parts[3]);
|
||||
}
|
||||
} else {
|
||||
log.warn("Error fetching series total: Element not found.");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Error fetching series total: {}", e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getLanguage(Document doc) {
|
||||
try {
|
||||
Element languageElement = doc.select("#rpi-attribute-language .rpi-attribute-value span").first();
|
||||
|
||||
+58
-1
@@ -1,5 +1,6 @@
|
||||
package com.adityachandel.booklore.service.metadata.parser;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.Award;
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.service.metadata.model.FetchMetadataRequest;
|
||||
import com.adityachandel.booklore.service.metadata.model.FetchedBookMetadata;
|
||||
@@ -82,6 +83,7 @@ public class GoodReadsParser implements BookParser {
|
||||
LinkedHashSet<String> keySet = getJsonKeys(apolloStateJson);
|
||||
|
||||
extractContributorDetails(apolloStateJson, keySet, builder);
|
||||
extractSeriesDetails(apolloStateJson, keySet, builder);
|
||||
extractBookDetails(apolloStateJson, keySet, builder);
|
||||
extractWorkDetails(apolloStateJson, keySet, builder);
|
||||
} catch (Exception e) {
|
||||
@@ -102,10 +104,19 @@ public class GoodReadsParser implements BookParser {
|
||||
}
|
||||
}
|
||||
|
||||
private void extractSeriesDetails(JSONObject apolloStateJson, LinkedHashSet<String> keySet, FetchedBookMetadata.FetchedBookMetadataBuilder builder) {
|
||||
String seriesKey = findKeyByPrefix(keySet, "Series:kca");
|
||||
if (seriesKey != null) {
|
||||
String seriesName = getSeriesName(apolloStateJson, seriesKey);
|
||||
if (seriesName != null) {
|
||||
builder.seriesName(seriesName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void extractBookDetails(JSONObject apolloStateJson, LinkedHashSet<String> keySet, FetchedBookMetadata.FetchedBookMetadataBuilder builder) {
|
||||
JSONObject bookJson = getValidBookJson(apolloStateJson, keySet, "Book:kca:");
|
||||
if (bookJson != null) {
|
||||
|
||||
builder.title(handleStringNull(bookJson.optString("title")))
|
||||
.description(handleStringNull(bookJson.optString("description")))
|
||||
.thumbnailUrl(handleStringNull(bookJson.optString("imageUrl")))
|
||||
@@ -125,6 +136,14 @@ public class GoodReadsParser implements BookParser {
|
||||
builder.language(handleStringNull(languageJson.optString("name")));
|
||||
}
|
||||
}
|
||||
|
||||
JSONArray bookSeriesJson = bookJson.optJSONArray("bookSeries");
|
||||
if (bookSeriesJson != null && bookSeriesJson.length() > 0) {
|
||||
JSONObject firstElement = bookSeriesJson.optJSONObject(0);
|
||||
if (firstElement != null) {
|
||||
builder.seriesNumber(parseInteger(firstElement.optString("userPosition")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,6 +158,13 @@ public class GoodReadsParser implements BookParser {
|
||||
.ratingCount(parseInteger(statsJson.optString("ratingsCount")))
|
||||
.reviewCount(parseInteger(statsJson.optString("textReviewsCount")));
|
||||
}
|
||||
|
||||
JSONObject detailsJson = workJson.optJSONObject("details");
|
||||
if (detailsJson != null) {
|
||||
JSONArray awardsWonArray = detailsJson.optJSONArray("awardsWon");
|
||||
List<Award> awards = getAwards(awardsWonArray);
|
||||
builder.awards(awards);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -203,6 +229,37 @@ public class GoodReadsParser implements BookParser {
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<Award> getAwards(JSONArray awardsWonArray) {
|
||||
List<Award> awards = new ArrayList<>();
|
||||
for (int i = 0; i < awardsWonArray.length(); i++) {
|
||||
JSONObject awardsWon = awardsWonArray.optJSONObject(i);
|
||||
String awardName = awardsWon.optString("name");
|
||||
String awardCategory = awardsWon.optString("category");
|
||||
String awardDesignation = awardsWon.optString("designation");
|
||||
LocalDate awardedAt = awardsWon.isNull("awardedAt") ? null :
|
||||
Instant.ofEpochMilli(awardsWon.optLong("awardedAt")).atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
awards.add(Award.builder()
|
||||
.name(awardName)
|
||||
.category(awardCategory)
|
||||
.designation(awardDesignation)
|
||||
.awardedAt(awardedAt)
|
||||
.build());
|
||||
}
|
||||
return awards;
|
||||
}
|
||||
|
||||
private String getSeriesName(JSONObject apolloStateJson, String seriesKey) {
|
||||
try {
|
||||
if (seriesKey != null) {
|
||||
JSONObject seriesJson = apolloStateJson.getJSONObject(seriesKey);
|
||||
return seriesJson.getString("title");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Error fetching series name: {}, Error: {}", seriesKey, e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getContributorName(JSONObject apolloStateJson, String contributorKey) {
|
||||
try {
|
||||
if (contributorKey != null) {
|
||||
|
||||
@@ -15,7 +15,7 @@ spring:
|
||||
name: booklore-api
|
||||
datasource:
|
||||
driver-class-name: org.h2.Driver
|
||||
url: 'jdbc:h2:~/my-library-temp/config/local/db/booklore;AUTO_SERVER=TRUE'
|
||||
url: 'jdbc:h2:tcp://localhost:9092/~/my-library-temp/config/local/db/booklore'
|
||||
username: sa
|
||||
password: password
|
||||
jpa:
|
||||
|
||||
@@ -52,9 +52,24 @@ CREATE TABLE IF NOT EXISTS book_metadata
|
||||
review_count INT,
|
||||
cover VARCHAR(255),
|
||||
cover_updated_on TIMESTAMP NULL,
|
||||
series_name VARCHAR(255),
|
||||
series_number INT,
|
||||
series_total INT,
|
||||
CONSTRAINT fk_book_metadata FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE book_award
|
||||
(
|
||||
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
book_id BIGINT NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
awarded_at TIMESTAMP NOT NULL,
|
||||
category VARCHAR(255) NOT NULL,
|
||||
designation VARCHAR(255) NOT NULL,
|
||||
CONSTRAINT fk_book_awards_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE,
|
||||
CONSTRAINT unique_book_award UNIQUE (book_id, name, category, awarded_at)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS author
|
||||
(
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
|
||||
@@ -83,6 +83,7 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
selectedAuthor = new BehaviorSubject<number | null>(null);
|
||||
selectedCategory = new BehaviorSubject<number | null>(null);
|
||||
selectedPublisher = new BehaviorSubject<string | null>(null);
|
||||
selectedAward = new BehaviorSubject<string | null>(null);
|
||||
|
||||
private activatedRoute = inject(ActivatedRoute);
|
||||
private messageService = inject(MessageService);
|
||||
@@ -212,8 +213,8 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
|
||||
private sideBarFilter(bookState: BookState): Observable<BookState> {
|
||||
return combineLatest([this.selectedAuthor, this.selectedCategory, this.selectedPublisher]).pipe(
|
||||
map(([authorId, categoryId, publisher]) => {
|
||||
return combineLatest([this.selectedAuthor, this.selectedCategory, this.selectedPublisher, this.selectedAward]).pipe(
|
||||
map(([authorId, categoryId, publisher, awardName]) => {
|
||||
let filteredBooks = bookState.books || [];
|
||||
if (authorId !== null) {
|
||||
filteredBooks = filteredBooks.filter(book =>
|
||||
@@ -230,6 +231,11 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
book.metadata?.publisher === publisher
|
||||
);
|
||||
}
|
||||
if (awardName !== null) {
|
||||
filteredBooks = filteredBooks.filter(book =>
|
||||
book.metadata?.awards?.some(award => award.name === awardName && award.designation === 'WINNER')
|
||||
);
|
||||
}
|
||||
return {...bookState, books: filteredBooks};
|
||||
})
|
||||
);
|
||||
@@ -321,11 +327,6 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
}
|
||||
|
||||
viewChanged() {
|
||||
this.selectedBooks.clear();
|
||||
this.isDrawerVisible = false;
|
||||
}
|
||||
|
||||
unshelfBooks() {
|
||||
if (this.entity) {
|
||||
this.bookService.updateBookShelves(this.selectedBooks, new Set(), new Set([this.entity.id])).subscribe(
|
||||
@@ -418,7 +419,10 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
|
||||
get isFilterActive(): boolean {
|
||||
return this.selectedAuthor.value !== null || this.selectedCategory.value !== null || this.selectedPublisher.value !== null;
|
||||
return this.selectedAuthor.value !== null ||
|
||||
this.selectedCategory.value !== null ||
|
||||
this.selectedPublisher.value !== null ||
|
||||
this.selectedAward.value !== null;
|
||||
}
|
||||
|
||||
clearFilter() {
|
||||
@@ -428,13 +432,16 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
if (this.selectedCategory.value !== null) {
|
||||
this.selectedCategory.next(null);
|
||||
}
|
||||
|
||||
if (this.selectedPublisher.value !== null) {
|
||||
this.selectedPublisher.next(null);
|
||||
}
|
||||
if (this.selectedAward.value !== null) {
|
||||
this.selectedAward.next(null);
|
||||
}
|
||||
this.bookFilterComponent.activeCategory = null;
|
||||
this.bookFilterComponent.activeAuthor = null;
|
||||
this.bookFilterComponent.activePublisher = null;
|
||||
this.bookFilterComponent.activeAward = null;
|
||||
this.clearSearch();
|
||||
}
|
||||
|
||||
@@ -469,5 +476,9 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
this.bookFilterComponent.publisherSelected.subscribe((publisher: string) => {
|
||||
this.selectedPublisher.next(publisher);
|
||||
});
|
||||
|
||||
this.bookFilterComponent.awardSelected.subscribe((award: string) => {
|
||||
this.selectedAward.next(award);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+17
@@ -39,6 +39,23 @@
|
||||
</p-accordion-panel>
|
||||
|
||||
<p-accordion-panel value="2">
|
||||
<p-accordion-header>Awards</p-accordion-header>
|
||||
<p-accordion-content class="h-[40rem] !overflow-y-auto">
|
||||
<div *ngIf="awardBookCount$ | async as awards">
|
||||
<div *ngFor="let award of awards">
|
||||
<p
|
||||
class="cursor-pointer transition-colors duration-200 ease-in-out pb-1"
|
||||
[ngClass]="{'text-[var(--primary-color)]': activeAward === award.award}"
|
||||
(click)="awardClicked(award)">
|
||||
<p-badge [value]="award.bookCount"></p-badge>
|
||||
{{ award.award }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</p-accordion-content>
|
||||
</p-accordion-panel>
|
||||
|
||||
<p-accordion-panel value="3">
|
||||
<p-accordion-header>Publishers</p-accordion-header>
|
||||
<p-accordion-content class="h-[40rem] !overflow-y-auto">
|
||||
<div *ngIf="publisherBookCount$ | async as publishers">
|
||||
|
||||
@@ -30,6 +30,7 @@ export class BookFilterComponent implements OnInit {
|
||||
@Output() authorSelected = new EventEmitter<number | null>();
|
||||
@Output() categorySelected = new EventEmitter<number | null>();
|
||||
@Output() publisherSelected = new EventEmitter<string | null>();
|
||||
@Output() awardSelected = new EventEmitter<string | null>();
|
||||
|
||||
@Input() showFilters: boolean = true;
|
||||
@Input() entity$!: Observable<Library | Shelf | null> | undefined;
|
||||
@@ -38,10 +39,12 @@ export class BookFilterComponent implements OnInit {
|
||||
activeAuthor: number | null = null;
|
||||
activeCategory: number | null = null;
|
||||
activePublisher: string | null = null;
|
||||
activeAward: string | null = null;
|
||||
|
||||
authorBookCount$!: Observable<{ author: Author; bookCount: number }[]>;
|
||||
categoryBookCount$!: Observable<{ category: Category; bookCount: number }[]>;
|
||||
publisherBookCount$!: Observable<{ publisher: string; bookCount: number }[]>;
|
||||
awardBookCount$!: Observable<{ award: string; bookCount: number }[]>;
|
||||
|
||||
bookService = inject(BookService);
|
||||
|
||||
@@ -50,6 +53,7 @@ export class BookFilterComponent implements OnInit {
|
||||
this.authorBookCount$ = this.getAuthorBookCountStream();
|
||||
this.categoryBookCount$ = this.getCategoryBookCountStream();
|
||||
this.publisherBookCount$ = this.getPublisherBookCountStream();
|
||||
this.awardBookCount$ = this.getAwardBookCountStream();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,6 +157,41 @@ export class BookFilterComponent implements OnInit {
|
||||
);
|
||||
}
|
||||
|
||||
private getAwardBookCountStream(): Observable<{ award: string; bookCount: number }[]> {
|
||||
return combineLatest([
|
||||
this.bookService.bookState$,
|
||||
this.entity$ ?? of(null),
|
||||
this.entityType$ ?? of(EntityType.ALL_BOOKS)
|
||||
]).pipe(
|
||||
map(([state, entity, entityType]) => {
|
||||
let filteredBooks = this.filterBooksByEntityType(state.books || [], entity, entityType);
|
||||
|
||||
const awardMap = new Map<string, { award: string; bookCount: number }>();
|
||||
filteredBooks.forEach((book) => {
|
||||
book.metadata?.awards
|
||||
?.filter((award) => award.designation === 'WINNER')
|
||||
.forEach((award) => {
|
||||
if (!awardMap.has(award.name)) {
|
||||
awardMap.set(award.name, {award: award.name, bookCount: 0});
|
||||
}
|
||||
const awardData = awardMap.get(award.name);
|
||||
if (awardData) {
|
||||
awardData.bookCount += 1;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return Array.from(awardMap.values()).sort((a, b) => {
|
||||
if (b.bookCount !== a.bookCount) {
|
||||
return b.bookCount - a.bookCount;
|
||||
}
|
||||
return a.award.localeCompare(b.award);
|
||||
});
|
||||
}),
|
||||
distinctUntilChanged((prev, curr) => JSON.stringify(prev) === JSON.stringify(curr))
|
||||
);
|
||||
}
|
||||
|
||||
private filterBooksByEntityType(books: Book[], entity: any, entityType: EntityType): Book[] {
|
||||
if (entityType === EntityType.LIBRARY && entity && 'id' in entity) {
|
||||
return books.filter((book) => book.libraryId === entity.id);
|
||||
@@ -172,9 +211,11 @@ export class BookFilterComponent implements OnInit {
|
||||
this.activeAuthor = author.author.id;
|
||||
}
|
||||
this.activeCategory = null;
|
||||
this.activeAward = null;
|
||||
this.activePublisher = null;
|
||||
this.categorySelected.emit(null);
|
||||
this.publisherSelected.emit(null);
|
||||
this.awardSelected.emit(null);
|
||||
this.authorSelected.emit(this.activeAuthor);
|
||||
}
|
||||
|
||||
@@ -185,9 +226,11 @@ export class BookFilterComponent implements OnInit {
|
||||
this.activeCategory = category.category.id;
|
||||
}
|
||||
this.activeAuthor = null;
|
||||
this.activeAward = null;
|
||||
this.activePublisher = null;
|
||||
this.authorSelected.emit(null);
|
||||
this.publisherSelected.emit(null);
|
||||
this.awardSelected.emit(null);
|
||||
this.categorySelected.emit(this.activeCategory);
|
||||
}
|
||||
|
||||
@@ -198,9 +241,26 @@ export class BookFilterComponent implements OnInit {
|
||||
this.activePublisher = publisher.publisher;
|
||||
}
|
||||
this.activeAuthor = null;
|
||||
this.activeAward = null;
|
||||
this.activeCategory = null;
|
||||
this.authorSelected.emit(null);
|
||||
this.categorySelected.emit(null);
|
||||
this.awardSelected.emit(null);
|
||||
this.publisherSelected.emit(this.activePublisher);
|
||||
}
|
||||
|
||||
awardClicked(award: { award: string; bookCount: number }) {
|
||||
if (this.activeAward === award.award) {
|
||||
this.activeAward = null;
|
||||
} else {
|
||||
this.activeAward = award.award;
|
||||
}
|
||||
this.activeAuthor = null;
|
||||
this.activeCategory = null;
|
||||
this.activePublisher = null;
|
||||
this.authorSelected.emit(null);
|
||||
this.categorySelected.emit(null);
|
||||
this.publisherSelected.emit(null);
|
||||
this.awardSelected.emit(this.activeAward);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ export interface BookMetadata {
|
||||
coverUpdatedOn?: string;
|
||||
authors: Author[];
|
||||
categories: Category[];
|
||||
awards?: Award[];
|
||||
|
||||
[key: string]: any;
|
||||
}
|
||||
@@ -68,6 +69,13 @@ export interface Category {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface Award {
|
||||
name: string;
|
||||
category: string;
|
||||
designation: string;
|
||||
awardedAt: string;
|
||||
}
|
||||
|
||||
export interface PdfViewerSetting {
|
||||
zoom: string | number;
|
||||
sidebarVisible: boolean;
|
||||
|
||||
Reference in New Issue
Block a user