feat:更新博客统计模块
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from fastapi import Request
|
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 sqlalchemy.orm import Session
|
||||||
|
|
||||||
from models.blog import Blog, BlogCategory, BlogVisit, BlogContent, BlogComment
|
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:
|
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(
|
return BlogStatsResponse(
|
||||||
blogCount=db.query(Blog).count(),
|
blogCount=blog_count,
|
||||||
categoryCount=db.query(BlogCategory).count(),
|
categoryCount=category_count,
|
||||||
wordCount=db.query(func.sum(Blog.word_count)).scalar()
|
wordCount=word_count
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user