feat:增加博客处理模块
This commit is contained in:
BIN
service/__pycache__/blog_service.cpython-310.pyc
Normal file
BIN
service/__pycache__/blog_service.cpython-310.pyc
Normal file
Binary file not shown.
@@ -6,10 +6,11 @@ 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
|
||||
BlogLatestResponse, BlogCommentCreate, BlogCommentResponse, BlogVisitResponse, BlogCreate, BlogUpdate
|
||||
from schemas.pagination import PageResult
|
||||
from schemas.paginate_query import paginate_query
|
||||
from middleware.exceptions import AppException
|
||||
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]:
|
||||
@@ -107,6 +108,119 @@ def query_blog_by_id(db: Session, blog_id: int) -> BlogResponse:
|
||||
return BlogResponse.from_orm(blog)
|
||||
|
||||
|
||||
def add_blog(db: Session, blog: BlogCreate) -> bool:
|
||||
word_count = get_word_count(blog.content)
|
||||
|
||||
db_blog = Blog(
|
||||
title=blog.title,
|
||||
top_value=blog.topValue,
|
||||
is_great=blog.isGreat,
|
||||
category_id=add_blog_category(db, blog.category),
|
||||
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)
|
||||
)
|
||||
|
||||
db.add(db_blog)
|
||||
db.commit()
|
||||
db.refresh(db_blog)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def add_blog_category(db: Session, category: str) -> int:
|
||||
db_blog_category = db.query(BlogCategory).filter(BlogCategory.name == category).first()
|
||||
if db_blog_category is None:
|
||||
db_new_blog_category = BlogCategory(name=category)
|
||||
db.add(db_new_blog_category)
|
||||
db.commit()
|
||||
category_id = db_new_blog_category.id
|
||||
else:
|
||||
category_id = db_blog_category.id
|
||||
|
||||
return category_id
|
||||
|
||||
|
||||
def add_blog_content(db: Session, content: str) -> int:
|
||||
db_blog_content = BlogContent(content=content.encode('utf-8'))
|
||||
db.add(db_blog_content)
|
||||
db.commit()
|
||||
|
||||
return db_blog_content.id
|
||||
|
||||
|
||||
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("博客不存在")
|
||||
|
||||
update_data = blog.model_dump(exclude_unset=True)
|
||||
|
||||
update_blog_content(db, db_blog.content_id, blog.content)
|
||||
|
||||
db_blog.title = update_data["title"]
|
||||
db_blog.top_value = update_data["topValue"]
|
||||
db_blog.is_great = update_data["isGreat"]
|
||||
db_blog.category_id = update_blog_category(db, blog.category)
|
||||
|
||||
word_count = get_word_count(blog.content)
|
||||
db_blog.summary = get_blog_summary(blog.content),
|
||||
db_blog.word_count = word_count
|
||||
db_blog.read_duration = get_read_duration(word_count)
|
||||
|
||||
db.commit()
|
||||
db.refresh(db_blog)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def update_blog_category(db: Session, category: str) -> int:
|
||||
db_blog_category = db.query(BlogCategory).filter(BlogCategory.name == category).first()
|
||||
|
||||
if db_blog_category is None:
|
||||
return add_blog_category(db, category)
|
||||
else:
|
||||
return db_blog_category.id
|
||||
|
||||
|
||||
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.content = blog_content.encode('utf-8')
|
||||
|
||||
db.commit()
|
||||
db.refresh(db_blog_content)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
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("博客不存在")
|
||||
|
||||
delete_blog_content(db, db_blog.content_id)
|
||||
|
||||
db.delete(db_blog)
|
||||
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("博客内容不存在")
|
||||
|
||||
db.delete(db_blog_content)
|
||||
db.commit()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def query_blog_category(db: Session) -> List[BlogCategoryResponse]:
|
||||
query = select(
|
||||
BlogCategory.name,
|
||||
@@ -147,6 +261,21 @@ def add_blog_visit(db: Session, blog_visit: BlogVisit):
|
||||
db.refresh(blog_visit)
|
||||
|
||||
|
||||
def query_blog_visit(db: Session, current_page: int = 1, page_size: int = 10) -> PageResult[BlogVisitResponse]:
|
||||
query = select(
|
||||
BlogVisit.ip,
|
||||
BlogVisit.os,
|
||||
BlogVisit.browser,
|
||||
BlogVisit.uri,
|
||||
Blog.title,
|
||||
BlogVisit.create_time.label("visitTime")
|
||||
).select_from(BlogVisit).outerjoin(
|
||||
Blog, Blog.id == BlogVisit.blog_id
|
||||
).order_by(desc(BlogVisit.create_time))
|
||||
|
||||
return paginate_query(db, query, current_page, page_size)
|
||||
|
||||
|
||||
def query_blog_comment(db: Session, blog_id: int) -> List[BlogCommentResponse]:
|
||||
query = select(
|
||||
BlogComment.id,
|
||||
|
||||
Reference in New Issue
Block a user