diff --git a/internal/handlers/collections.go b/internal/handlers/collections.go index 6e522e8b..997b6317 100644 --- a/internal/handlers/collections.go +++ b/internal/handlers/collections.go @@ -254,6 +254,7 @@ func (h *CollectionHandler) GetCollection(c *echo.Context) error { } isSmart := h.collectionService.IsSmartCollection(collection.AutoAssignRules) + libTypes := LibraryTypeMap(c.Request().Context(), h.db) if isSmart { rules := h.collectionService.ParseRules(collection.AutoAssignRules) visibleIDs, err := h.userVisibleLibraryIDs(c) @@ -267,19 +268,7 @@ func (h *CollectionHandler) GetCollection(c *echo.Context) error { if evalErr != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": evalErr.Error()}) } - bookList = make([]BookInfo, 0, len(matches)) - for _, item := range matches { - itemUUID, _ := uuid.FromBytes(item.ID.Bytes[0:16]) - libTypes := LibraryTypeMap(c.Request().Context(), h.db) - bookList = append(bookList, BookInfo{ - MediaItemID: itemUUID.String(), - Title: item.Title, - Author: textToString(item.Author), - CoverImagePath: utils.ResolveMediaURL(item.LibraryID, item.CoverImagePath), - FormatGroup: item.FormatGroup, - Badge: MediaBadgeKind(item.FormatGroup, libTypes[uuid.UUID(item.LibraryID.Bytes).String()]), - }) - } + bookList = BookCardsFromMediaItems(matches, libTypes) } else if collection.IsSystemCollection.Bool && collection.QueryType.Valid && collection.QueryType.String != "" && collection.QueryType.String != "filter" { user := c.Get("user").(database.Users) userUUID := uuid.UUID(user.ID.Bytes) @@ -290,17 +279,7 @@ func (h *CollectionHandler) GetCollection(c *echo.Context) error { } for _, section := range sections { if section.CollectionID == collectionID { - bookCards := make([]BookInfo, len(section.Items)) - for i, item := range section.Items { - itemUUID, _ := uuid.FromBytes(item.ID.Bytes[0:16]) - bookCards[i] = BookInfo{ - MediaItemID: itemUUID.String(), - Title: item.Title, - Author: textToString(item.Author), - CoverImagePath: utils.ResolveMediaURL(item.LibraryID, item.CoverImagePath), - } - } - bookList = bookCards + bookList = BookCardsFromMediaItems(section.Items, libTypes) break } } @@ -312,7 +291,7 @@ func (h *CollectionHandler) GetCollection(c *echo.Context) error { Limit: pgtype.Int4{Int32: 10000, Valid: true}, }) if collErr != nil { - bookList = []BookInfo{} + bookList = BookCardsFromCollectionDashRows(nil, libTypes) } else { var validItems []database.GetCollectionItemsForDashboardRow for _, item := range collItems { @@ -320,38 +299,14 @@ func (h *CollectionHandler) GetCollection(c *echo.Context) error { validItems = append(validItems, item) } } - libTypes := LibraryTypeMap(c.Request().Context(), h.db) - bookCards := make([]BookInfo, len(validItems)) - for i, item := range validItems { - itemUUID, _ := uuid.FromBytes(item.ID.Bytes[0:16]) - bookCards[i] = BookInfo{ - MediaItemID: itemUUID.String(), - Title: item.Title, - Author: textToString(item.Author), - CoverImagePath: utils.ResolveMediaURL(item.LibraryID, item.CoverImagePath), - FormatGroup: item.FormatGroup, - Badge: MediaBadgeKind(item.FormatGroup, libTypes[uuid.UUID(item.LibraryID.Bytes).String()]), - } - } - bookList = bookCards + bookList = BookCardsFromCollectionDashRows(validItems, libTypes) } } else { books, booksErr := h.GetCollectionBooksData(c, collectionID) if booksErr != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": booksErr.Error()}) } - bookList = make([]BookInfo, 0, len(books)) - for _, book := range books { - libTypes := LibraryTypeMap(c.Request().Context(), h.db) - bookList = append(bookList, BookInfo{ - MediaItemID: uuid.UUID(book.MediaItemID.Bytes).String(), - Title: book.Title, - Author: textToString(book.Author), - CoverImagePath: utils.ResolveMediaURL(book.LibraryID, book.CoverImagePath), - FormatGroup: book.FormatGroup, - Badge: MediaBadgeKind(book.FormatGroup, libTypes[uuid.UUID(book.LibraryID.Bytes).String()]), - }) - } + bookList = BookCardsFromCollectionItemRows(books, libTypes) } var viewSettings map[string]interface{}