From 02983a882d32113e555fa53a7df47d7cd5a34d54 Mon Sep 17 00:00:00 2001 From: Cxx0822 <1556464090@qq.com> Date: Sun, 26 Oct 2025 20:00:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=9B=B4=E6=96=B0=E5=8D=9A=E5=AE=A2?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/blog_service.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/service/blog_service.py b/service/blog_service.py index e9a39e4..81d4f33 100644 --- a/service/blog_service.py +++ b/service/blog_service.py @@ -1,7 +1,7 @@ from typing import List from fastapi import Request -from sqlalchemy import select, func, desc, and_, asc, delete +from sqlalchemy import select, func, desc, and_, asc, delete, distinct from sqlalchemy.orm import Session from models.blog import Blog, BlogCategory, BlogVisit, BlogContent, BlogComment @@ -235,10 +235,19 @@ def query_blog_category(db: Session) -> List[BlogCategoryResponse]: def query_blog_stats(db: Session) -> BlogStatsResponse: + blog_count_stmt = select(func.count(Blog.id)).where(Blog.is_approved == 1) + blog_count = db.execute(blog_count_stmt).scalar() or 0 + + word_count_stmt = select(func.sum(Blog.word_count)).where(Blog.is_approved == 1) + word_count = db.execute(word_count_stmt).scalar() or 0 + + category_count_stmt = select(func.count(distinct(Blog.category_id))).where(Blog.is_approved == 1) + category_count = db.execute(category_count_stmt).scalar() or 0 + return BlogStatsResponse( - blogCount=db.query(Blog).count(), - categoryCount=db.query(BlogCategory).count(), - wordCount=db.query(func.sum(Blog.word_count)).scalar() + blogCount=blog_count, + categoryCount=category_count, + wordCount=word_count )