feat:增加博客草稿功能
This commit is contained in:
@@ -16,8 +16,9 @@ from service.blog_elastic_service import add_blog_elastic
|
||||
from utils.blog_utils import get_blog_summary, get_word_count, get_read_duration
|
||||
|
||||
|
||||
def query_blog_by_page(db: Session, current_page: int = 1, page_size: int = 10) -> PageResult[BlogResponse]:
|
||||
query = select(
|
||||
def query_blog_by_page(db: Session, current_page: int = 1, page_size: int = 10,
|
||||
is_all=False) -> PageResult[BlogResponse]:
|
||||
stmt = (select(
|
||||
Blog.id,
|
||||
Blog.title,
|
||||
Blog.top_value.label("topValue"),
|
||||
@@ -27,23 +28,22 @@ def query_blog_by_page(db: Session, current_page: int = 1, page_size: int = 10)
|
||||
Blog.word_count.label("wordCount"),
|
||||
Blog.read_duration.label("readDuration"),
|
||||
func.count(BlogVisit.id).label("visitCount"),
|
||||
Blog.is_approved.label("isApproved"),
|
||||
Blog.create_time.label("createTime"),
|
||||
Blog.update_time.label("updateTime")
|
||||
).select_from(Blog).outerjoin(
|
||||
BlogCategory, Blog.category_id == BlogCategory.id
|
||||
).outerjoin(
|
||||
BlogVisit, Blog.id == BlogVisit.blog_id
|
||||
).group_by(
|
||||
Blog.id, BlogCategory.name
|
||||
).order_by(
|
||||
desc(Blog.top_value), desc(Blog.update_time)
|
||||
)
|
||||
).outerjoin(BlogCategory, Blog.category_id == BlogCategory.id)
|
||||
.outerjoin(BlogVisit, Blog.id == BlogVisit.blog_id)
|
||||
.group_by(Blog.id, BlogCategory.name)
|
||||
.order_by(desc(Blog.top_value), desc(Blog.update_time)))
|
||||
|
||||
return paginate_query(db, query, current_page, page_size)
|
||||
if not is_all:
|
||||
stmt = stmt.where(Blog.is_approved == 1)
|
||||
|
||||
return paginate_query(db, stmt, current_page, page_size)
|
||||
|
||||
|
||||
def query_blog_by_condition(db: Session, blog_query: BlogQuery) -> List[BlogResponse]:
|
||||
query = select(
|
||||
stmt = (select(
|
||||
Blog.id,
|
||||
Blog.title,
|
||||
Blog.top_value.label("topValue"),
|
||||
@@ -53,13 +53,12 @@ def query_blog_by_condition(db: Session, blog_query: BlogQuery) -> List[BlogResp
|
||||
BlogContent.content.label("content"),
|
||||
Blog.word_count.label("wordCount"),
|
||||
Blog.read_duration.label("readDuration"),
|
||||
Blog.is_approved.label("isApproved"),
|
||||
Blog.create_time.label("createTime"),
|
||||
Blog.update_time.label("updateTime")
|
||||
).select_from(Blog).outerjoin(
|
||||
BlogCategory, Blog.category_id == BlogCategory.id
|
||||
).outerjoin(
|
||||
BlogContent, Blog.content_id == BlogContent.id
|
||||
)
|
||||
).where(Blog.is_approved == 1)
|
||||
.outerjoin(BlogCategory, Blog.category_id == BlogCategory.id)
|
||||
.outerjoin(BlogContent, Blog.content_id == BlogContent.id))
|
||||
|
||||
conditions = []
|
||||
|
||||
@@ -73,11 +72,11 @@ def query_blog_by_condition(db: Session, blog_query: BlogQuery) -> List[BlogResp
|
||||
conditions.append(func.extract('year', Blog.create_time) == blog_query.year)
|
||||
|
||||
if conditions:
|
||||
query = query.where(and_(*conditions))
|
||||
stmt = stmt.where(and_(*conditions))
|
||||
|
||||
query = query.order_by(desc(Blog.update_time))
|
||||
stmt = stmt.order_by(desc(Blog.update_time))
|
||||
|
||||
results = db.execute(query).fetchall()
|
||||
results = db.execute(stmt).fetchall()
|
||||
|
||||
return [BlogResponse.from_orm(result) for result in results]
|
||||
|
||||
@@ -85,7 +84,7 @@ def query_blog_by_condition(db: Session, blog_query: BlogQuery) -> List[BlogResp
|
||||
def query_blog_by_id(db: Session, blog_id: int) -> BlogResponse:
|
||||
check_blog_exist(db, blog_id)
|
||||
|
||||
query = db.query(
|
||||
stmt = (select(
|
||||
Blog.id.label("id"),
|
||||
Blog.title.label("title"),
|
||||
Blog.top_value.label("topValue"),
|
||||
@@ -96,17 +95,15 @@ def query_blog_by_id(db: Session, blog_id: int) -> BlogResponse:
|
||||
Blog.word_count.label("wordCount"),
|
||||
Blog.read_duration.label("readDuration"),
|
||||
func.count(BlogVisit.id).label("visitCount"),
|
||||
Blog.is_approved.label("isApproved"),
|
||||
Blog.create_time.label("createTime"),
|
||||
Blog.update_time.label("updateTime")
|
||||
).select_from(Blog).outerjoin(
|
||||
BlogCategory, Blog.category_id == BlogCategory.id
|
||||
).outerjoin(
|
||||
BlogContent, Blog.content_id == BlogContent.id
|
||||
).outerjoin(
|
||||
BlogVisit, Blog.id == BlogVisit.blog_id
|
||||
).filter(Blog.id == blog_id)
|
||||
).where(Blog.id == blog_id)
|
||||
.outerjoin(BlogCategory, Blog.category_id == BlogCategory.id)
|
||||
.outerjoin(BlogContent, Blog.content_id == BlogContent.id)
|
||||
.outerjoin(BlogVisit, Blog.id == BlogVisit.blog_id))
|
||||
|
||||
blog = query.first()
|
||||
blog = db.execute(stmt).first()
|
||||
|
||||
return BlogResponse.from_orm(blog)
|
||||
|
||||
@@ -122,7 +119,8 @@ def add_blog(db: Session, blog: BlogCreate) -> bool:
|
||||
content_id=add_blog_content(db, blog.content),
|
||||
summary=get_blog_summary(blog.content),
|
||||
word_count=word_count,
|
||||
read_duration=get_read_duration(word_count)
|
||||
read_duration=get_read_duration(word_count),
|
||||
is_approved=blog.isApproved
|
||||
)
|
||||
|
||||
db.add(db_blog)
|
||||
@@ -133,7 +131,8 @@ def add_blog(db: Session, blog: BlogCreate) -> bool:
|
||||
id=db_blog.id,
|
||||
title=db_blog.title,
|
||||
content=blog.content,
|
||||
category=blog.category
|
||||
category=blog.category,
|
||||
isApproved=blog.isApproved
|
||||
)
|
||||
add_blog_elastic(blog_elastic)
|
||||
|
||||
@@ -178,6 +177,7 @@ def update_blog(db: Session, blog_id: int, blog: BlogUpdate) -> bool:
|
||||
db_blog.summary = get_blog_summary(blog.content),
|
||||
db_blog.word_count = word_count
|
||||
db_blog.read_duration = get_read_duration(word_count)
|
||||
db_blog.is_approved = blog.isApproved
|
||||
|
||||
db.commit()
|
||||
db.refresh(db_blog)
|
||||
@@ -224,14 +224,10 @@ def delete_blog_content(db: Session, blog_content_id: int) -> bool:
|
||||
|
||||
|
||||
def query_blog_category(db: Session) -> List[BlogCategoryResponse]:
|
||||
query = select(
|
||||
BlogCategory.name,
|
||||
func.count(Blog.id).label("count")
|
||||
).select_from(Blog).outerjoin(
|
||||
BlogCategory, Blog.category_id == BlogCategory.id
|
||||
).group_by(
|
||||
BlogCategory.name
|
||||
)
|
||||
query = (select(BlogCategory.name, func.count(Blog.id).label("count"))
|
||||
.where(Blog.is_approved == 1)
|
||||
.outerjoin(BlogCategory, Blog.category_id == BlogCategory.id)
|
||||
.group_by(BlogCategory.name))
|
||||
|
||||
results = db.execute(query).fetchall()
|
||||
|
||||
@@ -247,12 +243,8 @@ def query_blog_stats(db: Session) -> BlogStatsResponse:
|
||||
|
||||
|
||||
def query_blog_latest(db: Session) -> List[BlogLatestResponse]:
|
||||
query = select(
|
||||
Blog.id,
|
||||
Blog.title
|
||||
).select_from(Blog).order_by(desc(Blog.update_time)).limit(5)
|
||||
|
||||
results = db.execute(query).fetchall()
|
||||
stmt = select(Blog.id, Blog.title).where(Blog.is_approved == 1).order_by(desc(Blog.update_time)).limit(5)
|
||||
results = db.execute(stmt).fetchall()
|
||||
|
||||
return [BlogLatestResponse.from_orm(result) for result in results]
|
||||
|
||||
@@ -260,19 +252,15 @@ def query_blog_latest(db: Session) -> List[BlogLatestResponse]:
|
||||
def query_blog_adjacent(db: Session, blog_id: int) -> List[BlogAdjacentResponse]:
|
||||
check_blog_exist(db, blog_id)
|
||||
|
||||
prev_result = db.execute(
|
||||
select(Blog.id, Blog.title)
|
||||
.where(Blog.id < blog_id)
|
||||
.order_by(desc(Blog.id))
|
||||
.limit(1)
|
||||
).first()
|
||||
prev_stmt = (select(Blog.id, Blog.title)
|
||||
.where(Blog.id < blog_id, Blog.is_approved == 1)
|
||||
.order_by(desc(Blog.id)))
|
||||
prev_result = db.execute(prev_stmt).first()
|
||||
|
||||
next_result = db.execute(
|
||||
select(Blog.id, Blog.title)
|
||||
.where(Blog.id > blog_id)
|
||||
.order_by(asc(Blog.id))
|
||||
.limit(1)
|
||||
).first()
|
||||
next_stmt = (select(Blog.id, Blog.title)
|
||||
.where(Blog.id > blog_id, Blog.is_approved == 1)
|
||||
.order_by(asc(Blog.id)))
|
||||
next_result = db.execute(next_stmt).first()
|
||||
|
||||
return [
|
||||
BlogAdjacentResponse(
|
||||
@@ -308,7 +296,7 @@ def query_blog_visit(db: Session, current_page: int = 1, page_size: int = 10) ->
|
||||
|
||||
|
||||
def query_blog_comment(db: Session, blog_id: int) -> List[BlogCommentResponse]:
|
||||
query = select(
|
||||
stmt = select(
|
||||
BlogComment.id,
|
||||
BlogComment.blog_id.label("blogId"),
|
||||
BlogComment.parent_id.label("parentId"),
|
||||
@@ -319,8 +307,8 @@ def query_blog_comment(db: Session, blog_id: int) -> List[BlogCommentResponse]:
|
||||
BlogComment.content,
|
||||
BlogComment.is_approved.label("isApproved"),
|
||||
BlogComment.create_time.label("createTime")
|
||||
).select_from(BlogComment).where(BlogComment.blog_id == blog_id)
|
||||
results = db.execute(query).fetchall()
|
||||
).where(BlogComment.blog_id == blog_id, BlogComment.is_approved == 1).order_by(desc(BlogComment.create_time))
|
||||
results = db.execute(stmt).fetchall()
|
||||
|
||||
return [BlogCommentResponse.from_orm(result) for result in results]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user