diff --git a/backend/app/services/artist_service.py b/backend/app/services/artist_service.py index 271778e..c6aff2b 100644 --- a/backend/app/services/artist_service.py +++ b/backend/app/services/artist_service.py @@ -111,12 +111,22 @@ class ArtistService: ) ).all() + post_count = ( + await self.session.execute( + select(func.count(func.distinct(Post.id))) + .select_from(Post) + .join(Source, Source.id == Post.source_id) + .where(Source.artist_id == aid) + ) + ).scalar_one() + return { "id": artist.id, "name": artist.name, "slug": artist.slug, "is_subscription": bool(artist.is_subscription), "image_count": int(image_count), + "post_count": int(post_count), "date_range": { "min": dmin.isoformat() if dmin else None, "max": dmax.isoformat() if dmax else None, diff --git a/tests/test_api_artist.py b/tests/test_api_artist.py index 599093f..0a61d3b 100644 --- a/tests/test_api_artist.py +++ b/tests/test_api_artist.py @@ -29,6 +29,30 @@ async def test_artist_overview_ok(client, db): body = await resp.get_json() assert body["name"] == "Mira" assert body["image_count"] == 0 + assert body["post_count"] == 0 + + +@pytest.mark.asyncio +async def test_artist_overview_post_count(client, db): + from backend.app.models import Post, Source + + a = Artist(name="Lyra", slug="lyra") + db.add(a) + await db.flush() + s = Source( + artist_id=a.id, platform="patreon", + url="https://patreon.com/cw/lyra", enabled=True, + ) + db.add(s) + await db.flush() + db.add(Post(source_id=s.id, external_post_id="p1")) + db.add(Post(source_id=s.id, external_post_id="p2")) + await db.flush() + await db.commit() + resp = await client.get("/api/artist/lyra") + assert resp.status_code == 200 + body = await resp.get_json() + assert body["post_count"] == 2 @pytest.mark.asyncio