diff --git a/internal/handlers/media.go b/internal/handlers/media.go index cda1a45..56ff196 100644 --- a/internal/handlers/media.go +++ b/internal/handlers/media.go @@ -155,6 +155,12 @@ func (h *MediaHandler) DownloadBook(c *echo.Context) error { return nil } +// ExecuteSearch performs search and returns results with count +// Public wrapper for shared search logic used by both JSON and HTML endpoints +func (h *MediaHandler) ExecuteSearch(ctx context.Context, params services.SearchParams) ([]database.SearchMediaItemsUnifiedRow, int, error) { + return h.searchService.ExecuteSearch(ctx, params) +} + type AddToShelfRequest struct { MediaItemIDs []string `json:"media_item_ids" validate:"required"` ShelfName string `json:"shelf_name"` @@ -1450,13 +1456,12 @@ func (mh *MediaHandler) SearchMediaItems(c *echo.Context) error { "library_id_uuid", libUUID.Valid, "library_id_bytes", libUUID.Bytes) - // Call SearchService instead of DB directly - results, err := mh.searchService.SearchMediaItemsUnified(c.Request().Context(), params) - if err != nil && err != pgx.ErrNoRows { + // Call shared search service + results, _, err := mh.searchService.ExecuteSearch(c.Request().Context(), params) + if err != nil { c.Logger().Error("search error", "error", err.Error()) return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) } - if len(results) == 0 { return c.JSON(http.StatusNotFound, map[string]interface{}{ "error": "no results found", @@ -1464,7 +1469,6 @@ func (mh *MediaHandler) SearchMediaItems(c *echo.Context) error { "results": []interface{}{}, }) } - return c.JSON(http.StatusOK, results) }