feat:增加查询博客相邻记录功能
This commit is contained in:
@@ -8,11 +8,15 @@ from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from config.elastic import es, BLOG_INDEX
|
||||
from config.setting import settings
|
||||
from models.blog import Blog, BlogCategory, BlogContent
|
||||
from schemas.blog_elastic import BlogElastic, BlogSearch
|
||||
|
||||
|
||||
def sync_all_blog(db: Session) -> bool:
|
||||
if settings.ENVIRONMENT == 'dev':
|
||||
return True
|
||||
|
||||
query = select(
|
||||
Blog.id,
|
||||
Blog.title,
|
||||
@@ -54,6 +58,9 @@ def sync_all_blog(db: Session) -> bool:
|
||||
|
||||
|
||||
def add_blog_elastic(blog: BlogElastic) -> bool:
|
||||
if settings.ENVIRONMENT == 'dev':
|
||||
return True
|
||||
|
||||
now = datetime.now()
|
||||
blog.createTime = now.strftime("%Y-%m-%d %H:%M:%S")
|
||||
blog.updateTime = now.strftime("%Y-%m-%d %H:%M:%S")
|
||||
@@ -64,10 +71,14 @@ def add_blog_elastic(blog: BlogElastic) -> bool:
|
||||
body=blog.model_dump()
|
||||
)
|
||||
|
||||
logger.info(f"成功新增博客 {blog.id} 到 Elasticsearch")
|
||||
return True
|
||||
|
||||
|
||||
def update_blog_elastic(blog: BlogElastic) -> bool:
|
||||
if settings.ENVIRONMENT == 'dev':
|
||||
return True
|
||||
|
||||
now = datetime.now()
|
||||
blog.updateTime = now.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
@@ -77,12 +88,17 @@ def update_blog_elastic(blog: BlogElastic) -> bool:
|
||||
body=blog.model_dump()
|
||||
)
|
||||
|
||||
logger.info(f"成功修改博客 {blog.id} 到 Elasticsearch")
|
||||
return True
|
||||
|
||||
|
||||
def delete_blog(blog_id: int) -> bool:
|
||||
if settings.ENVIRONMENT == 'dev':
|
||||
return True
|
||||
|
||||
es.delete(index=BLOG_INDEX, id=blog_id)
|
||||
|
||||
logger.info(f"成功删除博客 {blog_id} 到 Elasticsearch")
|
||||
return True
|
||||
|
||||
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
from typing import List
|
||||
|
||||
from fastapi import Request
|
||||
from sqlalchemy import select, func, desc, and_
|
||||
from sqlalchemy import select, func, desc, and_, asc, delete
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models.blog import Blog, BlogCategory, BlogVisit, BlogContent, BlogComment
|
||||
from schemas.blog import BlogResponse, BlogQuery, BlogCategoryResponse, BlogStatsResponse, \
|
||||
BlogLatestResponse, BlogCommentCreate, BlogCommentResponse, BlogVisitResponse, BlogCreate, BlogUpdate
|
||||
BlogLatestResponse, BlogCommentCreate, BlogCommentResponse, BlogVisitResponse, BlogCreate, BlogUpdate, \
|
||||
BlogAdjacentResponse
|
||||
from schemas.blog_elastic import BlogElastic
|
||||
from schemas.pagination import PageResult
|
||||
from schemas.paginate_query import paginate_query
|
||||
@@ -82,6 +83,8 @@ 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(
|
||||
Blog.id.label("id"),
|
||||
Blog.title.label("title"),
|
||||
@@ -104,8 +107,6 @@ def query_blog_by_id(db: Session, blog_id: int) -> BlogResponse:
|
||||
).filter(Blog.id == blog_id)
|
||||
|
||||
blog = query.first()
|
||||
if blog[0] is None:
|
||||
raise AppException("博客不存在")
|
||||
|
||||
return BlogResponse.from_orm(blog)
|
||||
|
||||
@@ -140,7 +141,8 @@ def add_blog(db: Session, blog: BlogCreate) -> bool:
|
||||
|
||||
|
||||
def add_blog_category(db: Session, category: str) -> int:
|
||||
db_blog_category = db.query(BlogCategory).filter(BlogCategory.name == category).first()
|
||||
db_blog_category = db.execute(select(BlogCategory).where(BlogCategory.name == category)).scalar_one_or_none()
|
||||
|
||||
if db_blog_category is None:
|
||||
db_new_blog_category = BlogCategory(name=category)
|
||||
db.add(db_new_blog_category)
|
||||
@@ -161,9 +163,7 @@ def add_blog_content(db: Session, content: str) -> int:
|
||||
|
||||
|
||||
def update_blog(db: Session, blog_id: int, blog: BlogUpdate) -> bool:
|
||||
db_blog = db.query(Blog).filter(Blog.id == blog_id).first()
|
||||
if not db_blog:
|
||||
raise AppException("博客不存在")
|
||||
db_blog = check_blog_exist(db, blog_id)
|
||||
|
||||
update_data = blog.model_dump(exclude_unset=True)
|
||||
|
||||
@@ -186,7 +186,7 @@ def update_blog(db: Session, blog_id: int, blog: BlogUpdate) -> bool:
|
||||
|
||||
|
||||
def update_blog_category(db: Session, category: str) -> int:
|
||||
db_blog_category = db.query(BlogCategory).filter(BlogCategory.name == category).first()
|
||||
db_blog_category = db.execute(select(BlogCategory).where(BlogCategory.name == category)).scalar_one_or_none()
|
||||
|
||||
if db_blog_category is None:
|
||||
return add_blog_category(db, category)
|
||||
@@ -195,12 +195,9 @@ def update_blog_category(db: Session, category: str) -> int:
|
||||
|
||||
|
||||
def update_blog_content(db: Session, blog_content_id: int, blog_content: str) -> bool:
|
||||
db_blog_content = db.query(BlogContent).filter(BlogContent.id == blog_content_id).first()
|
||||
if not db_blog_content:
|
||||
raise AppException("博客内容不存在")
|
||||
db_blog_content = check_blog_content_exist(db, blog_content_id)
|
||||
|
||||
db_blog_content.content = blog_content.encode('utf-8')
|
||||
|
||||
db.commit()
|
||||
db.refresh(db_blog_content)
|
||||
|
||||
@@ -208,24 +205,19 @@ def update_blog_content(db: Session, blog_content_id: int, blog_content: str) ->
|
||||
|
||||
|
||||
def delete_blog(db: Session, blog_id: int) -> bool:
|
||||
db_blog = db.query(Blog).filter(Blog.id == blog_id).first()
|
||||
if not db_blog:
|
||||
raise AppException("博客不存在")
|
||||
db_blog = check_blog_exist(db, blog_id)
|
||||
|
||||
delete_blog_content(db, db_blog.content_id)
|
||||
|
||||
db.delete(db_blog)
|
||||
db.execute(delete(Blog).where(Blog.id == blog_id))
|
||||
db.commit()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def delete_blog_content(db: Session, blog_content_id: int) -> bool:
|
||||
db_blog_content = db.query(BlogContent).filter(BlogContent.id == blog_content_id).first()
|
||||
if not db_blog_content:
|
||||
raise AppException("博客内容不存在")
|
||||
check_blog_content_exist(db, blog_content_id)
|
||||
|
||||
db.delete(db_blog_content)
|
||||
db.execute(delete(BlogContent).where(BlogContent.id == blog_content_id))
|
||||
db.commit()
|
||||
|
||||
return True
|
||||
@@ -265,6 +257,35 @@ def query_blog_latest(db: Session) -> List[BlogLatestResponse]:
|
||||
return [BlogLatestResponse.from_orm(result) for result in results]
|
||||
|
||||
|
||||
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()
|
||||
|
||||
next_result = db.execute(
|
||||
select(Blog.id, Blog.title)
|
||||
.where(Blog.id > blog_id)
|
||||
.order_by(asc(Blog.id))
|
||||
.limit(1)
|
||||
).first()
|
||||
|
||||
return [
|
||||
BlogAdjacentResponse(
|
||||
id=prev_result[0] if prev_result else 0,
|
||||
title=prev_result[1] if prev_result else ""
|
||||
),
|
||||
BlogAdjacentResponse(
|
||||
id=next_result[0] if next_result else 0,
|
||||
title=next_result[1] if next_result else ""
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
def add_blog_visit(db: Session, blog_visit: BlogVisit):
|
||||
db.add(blog_visit)
|
||||
db.commit()
|
||||
@@ -305,15 +326,14 @@ def query_blog_comment(db: Session, blog_id: int) -> List[BlogCommentResponse]:
|
||||
|
||||
|
||||
def add_blog_comment(db: Session, request: Request, blog_id: int, blog_comment: BlogCommentCreate) -> bool:
|
||||
blog = db.query(Blog).filter(Blog.id == blog_id).first()
|
||||
if not blog:
|
||||
raise AppException("博客不存在")
|
||||
check_blog_exist(db, blog_id)
|
||||
|
||||
if blog_comment.parentId != 0:
|
||||
parent_comment = db.query(BlogComment).filter(
|
||||
BlogComment.id == blog_comment.parentId,
|
||||
BlogComment.blog_id == blog_id
|
||||
).first()
|
||||
parent_comment = db.execute(
|
||||
select(BlogComment)
|
||||
.where(BlogComment.id == blog_comment.parentId)
|
||||
.where(BlogComment.blog_id == blog_id)
|
||||
).scalar_one_or_none()
|
||||
|
||||
if not parent_comment:
|
||||
raise AppException("父评论不存在")
|
||||
@@ -334,3 +354,20 @@ def add_blog_comment(db: Session, request: Request, blog_id: int, blog_comment:
|
||||
db.refresh(db_comment)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def check_blog_exist(db: Session, blog_id: int) -> Blog:
|
||||
result = db.execute(select(Blog).where(Blog.id == blog_id)).scalar_one_or_none()
|
||||
|
||||
if not result:
|
||||
raise AppException("博客不存在")
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def check_blog_content_exist(db: Session, blog_content_id: int) -> BlogContent:
|
||||
result = db.execute(select(BlogContent).where(BlogContent.id == blog_content_id)).scalar_one_or_none()
|
||||
if not result:
|
||||
raise AppException("博客内容不存在")
|
||||
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user