From e0b065e38b10b5aced4b3f2222c77b627edec0ec Mon Sep 17 00:00:00 2001 From: leefer Date: Thu, 30 Jul 2026 12:07:05 +0800 Subject: [PATCH] refactor(storage): remove duplicate watchlist query --- next/backend/bootstrap/container.py | 5 +++-- next/backend/data/repository.py | 13 ------------- next/backend/features/market/insights/service.py | 10 ++++++++-- next/tests/test_market_insights.py | 8 ++++++-- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/next/backend/bootstrap/container.py b/next/backend/bootstrap/container.py index 4dc2930..a86995b 100644 --- a/next/backend/bootstrap/container.py +++ b/next/backend/bootstrap/container.py @@ -66,6 +66,7 @@ def build_container(settings: Settings) -> ApplicationContainer: credentials = SystemCredentialService(database, credential_repository, cipher) tushare = TushareProvider(lambda: credentials.get("tushare_token")) market_repository = MarketRepository() + review_repository = ReviewRepository() gateway = DataGateway( database, market_repository, @@ -82,7 +83,7 @@ def build_container(settings: Settings) -> ApplicationContainer: market = MarketService( gateway, MarketSnapshotService(database, market_repository, gateway), - MarketInsightService(database, market_repository, gateway), + MarketInsightService(database, market_repository, review_repository, gateway), ) accounts = AccountService(database, account_repository, PasswordHasher(), cipher) memberships = MembershipService(database, account_repository) @@ -110,7 +111,7 @@ def build_container(settings: Settings) -> ApplicationContainer: llm, PROJECT_ROOT / "config" / "heaven" / "iching_zh.json", ) - review = ReviewService(database, ReviewRepository(), gateway, screener, llm) + review = ReviewService(database, review_repository, gateway, screener, llm) jobs = JobService(database, JobRepository()) events = MarketEventService(database, market_repository, gateway) operations = OperationsService( diff --git a/next/backend/data/repository.py b/next/backend/data/repository.py index 1fc83a7..877e991 100644 --- a/next/backend/data/repository.py +++ b/next/backend/data/repository.py @@ -480,19 +480,6 @@ class MarketRepository: (seat_name, alias_name, updated_at, updated_by), ) - def watchlist( - self, connection: sqlite3.Connection, user_id: int - ) -> tuple[sqlite3.Row, ...]: - return tuple( - connection.execute( - """ - SELECT identifier, name, sector FROM watchlist_entries - WHERE user_id = ? ORDER BY created_at, identifier - """, - (user_id,), - ).fetchall() - ) - def save_chart( self, connection: sqlite3.Connection, diff --git a/next/backend/features/market/insights/service.py b/next/backend/features/market/insights/service.py index 69b949d..f2d7f66 100644 --- a/next/backend/features/market/insights/service.py +++ b/next/backend/features/market/insights/service.py @@ -50,6 +50,7 @@ from backend.features.market.insights.support import ( valid_date as _date, ) from backend.features.market.insights.themes import build_theme_detail, build_theme_library +from backend.features.review.repository import ReviewRepository SHANGHAI = ZoneInfo("Asia/Shanghai") @@ -60,10 +61,15 @@ class MarketInsightError(RuntimeError): class MarketInsightService: def __init__( - self, database: Database, repository: MarketRepository, gateway: DataGateway + self, + database: Database, + repository: MarketRepository, + watchlists: ReviewRepository, + gateway: DataGateway, ) -> None: self._database = database self._repository = repository + self._watchlists = watchlists self._gateway = gateway def workspace( @@ -211,7 +217,7 @@ class MarketInsightService: market_rows = list(result.pop("_market_rows", ())) with self._database.read() as connection: watchlist = tuple( - dict(row) for row in self._repository.watchlist(connection, user_id) + dict(row) for row in self._watchlists.watchlist(connection, user_id) ) result["watchlist_rows"] = build_watchlist_rows( market_rows, diff --git a/next/tests/test_market_insights.py b/next/tests/test_market_insights.py index 34ae574..75bf903 100644 --- a/next/tests/test_market_insights.py +++ b/next/tests/test_market_insights.py @@ -17,6 +17,7 @@ from backend.features.market.insights.dragon import build_dragon_list from backend.features.market.insights.popularity import build_popularity from backend.features.market.insights.service import MarketInsightService from backend.features.market.insights.themes import build_theme_detail, build_theme_library +from backend.features.review.repository import ReviewRepository SHANGHAI = ZoneInfo("Asia/Shanghai") @@ -106,7 +107,7 @@ def test_watchlist_rows_are_built_only_from_current_account_entries() -> None: def test_watchlist_repository_isolates_accounts(tmp_path) -> None: database = Database(tmp_path / "watchlists.db") MigrationRunner(database).upgrade(MIGRATIONS) - repository = MarketRepository() + repository = ReviewRepository() with database.transaction() as connection: for user_id, username in ((1, "account-a"), (2, "account-b")): connection.execute( @@ -342,7 +343,10 @@ def insight_service(tmp_path, dynamic_available: bool = True) -> MarketInsightSe payload=prior, ) return MarketInsightService( - database, repository, AuctionGateway(dynamic_available) # type: ignore[arg-type] + database, + repository, + ReviewRepository(), + AuctionGateway(dynamic_available), # type: ignore[arg-type] )