feat:更新数据模型定义

This commit is contained in:
2025-12-10 19:50:13 +08:00
parent 8650f92259
commit 49d634b877
3 changed files with 198 additions and 160 deletions

View File

@@ -1,7 +1,7 @@
from typing import List
from fastapi import Request
from sqlalchemy import select, func, desc, and_, asc, delete, distinct
from sqlalchemy import select, func, desc, and_, asc
from sqlalchemy.orm import Session
from models.blog import Blog, BlogCategory, BlogVisit, BlogContent, BlogComment
@@ -22,16 +22,16 @@ def query_blog_by_page(db: Session, current_page: int = 1, page_size: int = 10,
stmt = (select(
Blog.id,
Blog.title,
Blog.top_value.label("topValue"),
Blog.is_great.label("isGreat"),
Blog.top_value,
Blog.is_great,
BlogCategory.name.label("category"),
Blog.summary,
Blog.word_count.label("wordCount"),
Blog.read_duration.label("readDuration"),
Blog.word_count,
Blog.read_duration,
func.count(BlogVisit.id).label("visitCount"),
Blog.is_approved.label("isApproved"),
Blog.create_time.label("createTime"),
Blog.update_time.label("updateTime")
Blog.is_approved,
Blog.create_time,
Blog.update_time
).outerjoin(BlogCategory, Blog.category_id == BlogCategory.id)
.outerjoin(BlogVisit, Blog.id == BlogVisit.blog_id)
.group_by(Blog.id, BlogCategory.name)
@@ -53,17 +53,17 @@ def get_query_blog_by_condition_stmt(blog_query: BlogQuery):
stmt = (select(
Blog.id,
Blog.title,
Blog.top_value.label("topValue"),
Blog.is_great.label("isGreat"),
Blog.top_value,
Blog.is_great,
BlogCategory.name.label("category"),
Blog.summary,
BlogContent.content.label("content"),
Blog.word_count.label("wordCount"),
Blog.read_duration.label("readDuration"),
BlogContent.content,
Blog.word_count,
Blog.read_duration,
func.count(BlogVisit.id).label("visitCount"),
Blog.is_approved.label("isApproved"),
Blog.create_time.label("createTime"),
Blog.update_time.label("updateTime")
Blog.is_approved,
Blog.create_time,
Blog.update_time
).where(Blog.is_approved == 1)
.outerjoin(BlogCategory, Blog.category_id == BlogCategory.id)
.outerjoin(BlogContent, Blog.content_id == BlogContent.id)
@@ -99,17 +99,17 @@ def query_unapproved_blog(db: Session) -> List[BlogResponse]:
stmt = ((select(
Blog.id,
Blog.title,
Blog.top_value.label("topValue"),
Blog.is_great.label("isGreat"),
Blog.top_value,
Blog.is_great,
BlogCategory.name.label("category"),
Blog.summary,
BlogContent.content.label("content"),
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")
BlogContent.content,
Blog.word_count,
Blog.read_duration,
func.count(BlogVisit.id),
Blog.is_approved,
Blog.create_time,
Blog.update_time
).where(Blog.is_approved == 0)
.outerjoin(BlogCategory, Blog.category_id == BlogCategory.id)
.outerjoin(BlogContent, Blog.content_id == BlogContent.id))
@@ -126,19 +126,19 @@ def query_blog_by_id(db: Session, blog_id: int) -> BlogResponse:
check_blog_exist(db, blog_id)
stmt = (select(
Blog.id.label("id"),
Blog.title.label("title"),
Blog.top_value.label("topValue"),
Blog.is_great.label("isGreat"),
Blog.id,
Blog.title,
Blog.top_value,
Blog.is_great,
BlogCategory.name.label("category"),
Blog.summary.label("summary"),
BlogContent.content.label("content"),
Blog.word_count.label("wordCount"),
Blog.read_duration.label("readDuration"),
Blog.summary,
BlogContent.content,
Blog.word_count,
Blog.read_duration,
func.count(BlogVisit.id).label("visitCount"),
Blog.is_approved.label("isApproved"),
Blog.create_time.label("createTime"),
Blog.update_time.label("updateTime")
Blog.is_approved,
Blog.create_time,
Blog.update_time
).where(Blog.id == blog_id)
.outerjoin(BlogCategory, Blog.category_id == BlogCategory.id)
.outerjoin(BlogContent, Blog.content_id == BlogContent.id)
@@ -146,7 +146,7 @@ def query_blog_by_id(db: Session, blog_id: int) -> BlogResponse:
blog = db.execute(stmt).first()
return BlogResponse.from_orm(blog)
return BlogResponse.model_validate(blog)
def add_blog(db: Session, blog: BlogCreate) -> bool:
@@ -154,16 +154,18 @@ def add_blog(db: Session, blog: BlogCreate) -> bool:
db_blog = Blog(
title=blog.title,
top_value=blog.topValue,
is_great=blog.isGreat,
top_value=blog.top_value,
is_great=blog.is_great,
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),
is_approved=blog.isApproved
is_approved=blog.is_approved
)
# 级联新增
db_blog.content = BlogContent(content=blog.content.encode('utf-8'))
db.add(db_blog)
db.commit()
db.refresh(db_blog)
@@ -194,26 +196,17 @@ def add_blog_category(db: Session, category: str) -> int:
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 = check_blog_exist(db, blog_id)
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.title = blog.title
db_blog.top_value = blog.topValue
db_blog.is_great = blog.isGreat
db_blog.category_id = update_blog_category(db, blog.category)
# 级联更新
db_blog.content = BlogContent(content=blog.content.encode('utf-8'))
word_count = get_word_count(blog.content)
db_blog.summary = get_blog_summary(blog.content),
db_blog.word_count = word_count
@@ -235,30 +228,11 @@ def update_blog_category(db: Session, category: str) -> int:
return db_blog_category.id
def update_blog_content(db: Session, blog_content_id: int, blog_content: str) -> bool:
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)
return True
def delete_blog(db: Session, blog_id: int) -> bool:
db_blog = check_blog_exist(db, blog_id)
delete_blog_content(db, db_blog.content_id)
db.execute(delete(Blog).where(Blog.id == blog_id))
db.commit()
return True
def delete_blog_content(db: Session, blog_content_id: int) -> bool:
check_blog_content_exist(db, blog_content_id)
db.execute(delete(BlogContent).where(BlogContent.id == blog_content_id))
# 级联删除
db.delete(db_blog)
db.commit()
return True
@@ -341,15 +315,15 @@ 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]:
stmt = select(
BlogComment.id,
BlogComment.blog_id.label("blogId"),
BlogComment.parent_id.label("parentId"),
BlogComment.blog_id,
BlogComment.parent_id,
BlogComment.name,
BlogComment.website,
BlogComment.ip_address.label("ipAddress"),
BlogComment.user_agent.label("userAgent"),
BlogComment.ip_address,
BlogComment.user_agent,
BlogComment.content,
BlogComment.is_approved.label("isApproved"),
BlogComment.create_time.label("createTime")
BlogComment.is_approved,
BlogComment.create_time
).where(BlogComment.blog_id == blog_id, BlogComment.is_approved == 1).order_by(desc(BlogComment.create_time))
results = db.execute(stmt).fetchall()
@@ -377,7 +351,7 @@ def add_blog_comment(db: Session, request: Request, blog_id: int, blog_comment:
content=blog_comment.content,
ip_address=request.client.host,
user_agent=request.headers.get("user-agent"),
is_approved=1
is_approved=True
)
db.add(db_comment)